How do you force a maven MOJO to be executed only once at the end of a build?

前端 未结 7 1617
傲寒
傲寒 2021-02-08 13:06

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().isExecut         


        
7条回答
  •  不知归路
    2021-02-08 13:48

    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.

提交回复
热议问题