Java – how to force Maven mojo to be executed once at the end of the build?

I have a mojo that I want to execute once, once only after the test phase of the last project in the reactor

use:

if (!getProject().isExecutionRoot()) {
        return ;
}

At the beginning of the execute () method, it means that my mojo is executed once before all other sub modules, but in the initial stage of construction

Solution

The best solution I have found 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 seems to apply to the small hierarchy I tested

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>