I have a MOJO I would like executed once, and once only after the test phase of the last project in the reactor to run.
Using:
if (!getProject().isExecutionRoot()) {
return ;
}
at the start of the execute() method means my mojo gets executed once, however at the very beginning of the build - before all other child modules.
The best solution I have found for this is:
/**
* The projects in the reactor.
*
* @parameter expression="${reactorProjects}"
* @readonly
*/
private List reactorProjects;
public void execute() throws MojoExecutionException {
// only execute this mojo once, on the very last project in the reactor
final int size = reactorProjects.size();
MavenProject lastProject = (MavenProject) reactorProjects.get(size - 1);
if (lastProject != getProject()) {
return;
}
// do work
...
}
This appears to work on the small build hierarchies I've tested with.
The best solution is relying on a lifecycle extension by extending your class from org.apache.maven.AbstractMavenLifecycleParticipant
(see also https://maven.apache.org/examples/maven-3-lifecycle-extensions.html) which got a method afterSessionEnd
added with https://issues.apache.org/jira/browse/MNG-5640 (fixed in Maven 3.2.2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With