Question

I've written a Maven plugin that is only designed to run on parent modules. It is basically a customised archetype plugin that has some addition steps, hence it is run from the command line and not part of a lifecycle.

Currently it fails when run on a child module; it suceeds on the parent module from where the command is executed (the bit I care about) but then fails when it then iterates over child modules. Now the one workaround I can think of is the -N flag - this fixes the problem. The problem is that this plugin is going to be run from the command line by lots of different people and figure I want it to be as simple as possible.

I've checked out this page and tried out likely suspects; @inheritByDefault=false but that makes no difference.

Any suggestions?

I suppose I could check in the plugin code whether the project object has at least one module, if so execute otherwise skip...doesn't seem that nice though.

Was it helpful?

Solution

I usually solve that problem by comparing the execution root directory from maven session with the module's base directory:

public class ReactorRootRunnerMojo extends AbstractMojo {

  /**
   * @parameter expression="${session}"
   */
  private MavenSession session; 

  public void execute() throws MojoExecutionException, MojoFailureException {
    if (!isReactorRootProject()) {
        return;
    }
    // your code
  }

  private boolean isReactorRootProject() throws MojoExecutionException {
    try {
      String executionRootPath = new File(session.getExecutionRootDirectory()).getCanonicalFile().getAbsolutePath();
      String basedirPath = basedir.getCanonicalFile().getAbsolutePath();
      return executionRootPath.equals(basedirPath);
    } catch (IOException e) {
      throw new MojoExecutionException(e);
    }
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top