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

前端 未结 7 1628
傲寒
傲寒 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:49

    Check out maven-monitor API

    You can add an EventMonitor to the dispatcher, and then trap the END of the 'reactor-execute' event: this is dispatched after everything is completed, i.e. even after you see the BUILD SUCCESSFUL/FAILED output.

    Here's how I used it recently to print a summary right at the end:

    /**
     * The Maven Project Object
     *
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    protected MavenProject project;
    
    
    /**
     * The Maven Session.
     *
     * @parameter expression="${session}"
     * @required
     * @readonly
     */
    protected MavenSession session;
    
    ...
    
    
    @Override
    public void execute() throws MojoExecutionException, MojoFailureException
    {
        //Register the event handler right at the start only
        if (project.isExecutionRoot())
            registerEventMonitor();
        ...
    }
    
    
    /**
     * Register an {@link EventMonitor} with Maven so that we can respond to certain lifecycle events
     */
    protected void registerEventMonitor()
    {
        session.getEventDispatcher().addEventMonitor(
                new EventMonitor() {
    
                    @Override
                    public void endEvent(String eventName, String target, long arg2) {
                        if (eventName.equals("reactor-execute"))
                            printSummary();
                    }
    
                    @Override
                    public void startEvent(String eventName, String target, long arg2) {}
    
                    @Override
                    public void errorEvent(String eventName, String target, long arg2, Throwable arg3) {}
    
    
                }
        );
    }
    
    
    /**
     * Print summary at end
     */
    protected void printSummary()
    {
        ...
    }
    

提交回复
热议问题