package com.github.phuonghuynh;

import com.github.phuonghuynh.io.SuffixDirectoryFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.shared.invoker.*;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

/**
 * Build maven projects by name
 *
 * @author phuonghqh
 */
@Mojo(name = "named-mvn")
public class NamedMvn extends AbstractMojo {

  public static final String SUFFIX = "suffix";
  public static final String PREFIX = "prefix";

  private final Log log;

  /**
   * Working directory, default is current directory
   */
  @Parameter(property = "workingDirectory", required = false)
  private String workingDirectory;

  /**
   * Name of maven projects
   */
  @Parameter(property = "name", required = true)
  private String name;

  /**
   * Type of name, value should be "prefix", "suffix" (default value)
   */
  @Parameter(property = "type", required = false)
  private String type;

  public NamedMvn() {
    log = getLog();
  }

  /**
   * Main processing
   *
   * @return
   * @throws {@link MojoExecutionException}
   * @throws {@link MojoFailureException}
   */
  public void execute() throws MojoExecutionException, MojoFailureException {
    try {

      if (StringUtils.isBlank(workingDirectory)) {
        workingDirectory = ".";
      }

      if (!PREFIX.equalsIgnoreCase(type)) {
        type = SUFFIX;
      }

      File workingDir = new File(workingDirectory);
      String workingDirPath = workingDir.getCanonicalPath();
      String[] projectDirs = SUFFIX.equalsIgnoreCase(type) ? workingDir.list(new SuffixDirectoryFilter(name)) : new String[]{};

      for (int i = 0; i < projectDirs.length; i++) {
        try {
          log.debug("Execute project " + projectDirs[i]);
          InvocationRequest request = new DefaultInvocationRequest();
          request.setPomFile(new File(String.format("%s/%s/pom.xml", workingDirPath, projectDirs[i])));
          request.setGoals(Arrays.asList("clean", "install"));

          Invoker invoker = new DefaultInvoker();
          InvocationResult invocationResult = invoker.execute(request);
          if (invocationResult.getExitCode() != 0) {
            throw new MavenInvocationException("Build failed.", invocationResult.getExecutionException());
          }
        }
        catch (MavenInvocationException e) {
          log.error("Failed execute project " + projectDirs[i], e);
          throw new IllegalStateException("Failed execute project " + projectDirs[i], e);
        }
      }
    }
    catch (IOException e) {
      log.error("Invalid working directory", e);
    }
  }
}

@@ -1510,16 +1510,60 @@
   try %7B%0A
+      log.debug(%22Running named-mvn plugin%22);
 %0A      i
@@ -1719,24 +1719,196 @@
 X;%0A      %7D%0A%0A
+      log.debug(%22Configuration: %22);%0A      log.debug(%22=%3E workingDirectory: %22 + workingDirectory);%0A      log.debug(%22=%3E type: %22 + type);%0A      log.debug(%22=%3E name: %22 + name);%0A%0A
       File w
@@ -2135,16 +2135,79 @@
 ing%5B%5D%7B%7D;
+%0A      log.debug(%22Found %22 + projectDirs.length + %22 projects.%22);
 %0A%0A      

Add more log