Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a Maven Plugin/Mojo: how do you make a goal force the execution of other goals?

Note: this thread is about writing a custom Mojo, not using a plugin.

I maintain a testing plugin for Maven. Unfortunately, for almost a year now, I've let this particular unknown linger and I'd really like to know how to deal with it so that its users can have a simpler configuration.

Let's say we have two goals in our plugin:

  • prepare (phase: generate-sources)
  • do (phase: compile)

I want to configure the do Mojo to require prepare to have been executed in the earlier phase of the build. However, nothing in the descriptor documentation suggests I can.

The user probably doesn't care or understand the point of the prepare goal, so I don't want to force them to specify it in their POM. Of course, I could execute the Mojo directly from do, but then the prepare goal will have run at a later phase than is intended.

(I looked into custom lifecycles, but that makes it appear that everyone who already has the prepare goal in their POMs will have it executed twice upon running do.)

like image 447
Justin Searls Avatar asked Jul 02 '11 14:07

Justin Searls


People also ask

How do I run a specific goal in Maven?

From the main menu, select Run | Edit Configurations to open the run/debug configuration for your project. In the list that opens, select Run Maven Goal. In the Select Maven Goal dialog, specify a project and a goal that you want to execute before launching the project. Click OK.

What is executions in Maven?

An <execution> causes the plugin to be executed during the maven build lifecycle, i.e. during your build. The <configuration> allows you to configure the plugin for how it should behave during execution. Many Maven plugins provide documentation about their configuration options, e.g. the maven-compiler-plugin.

What is the correct syntax for executing a Maven plugin?

Usage of a Maven Plugin xml you can use the shorthand notation to execute the plugin: mvn <prefix>:<goal> , commonly the “prefix” is the artifact ID minus the “-maven-plugin”. For example mvn example:version .

What is Maven plugin goal?

A Maven plugin is a group of goals; however, these goals aren't necessarily all bound to the same phase. As we can see, the Failsafe plugin has two main goals configured here: integration-test: run integration tests. verify: verify all integration tests passed.


1 Answers

You could have something like the below (taken from the CompilerMojo):

/**
 * @author <a href="mailto:[email protected]">Jason van Zyl </a>
 * @version $Id: CompilerMojo.java 941498 2010-05-05 21:24:11Z krosenvold $
 * @since 2.0
 * @goal compile
 * @phase compile
 * @threadSafe
 * @requiresDependencyResolution compile
 */

By setting this over your class, it will execute during the compile phase (in this example). The compile phase requires all the previous phases to have executed first (validate, generate-sources, process-sources, generate-resources, process-resources...).

Basically, pick a phase after the one you need (or even the same one) and it should work.

like image 120
carlspring Avatar answered Sep 18 '22 12:09

carlspring