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