Can a Maven plugin see the "configuration" tag from an "execution" section automatically?

3.5k Views Asked by At

I'm analyzing a Maven plugin that I can configure inside the configuration section of plugin:

<plugin>
     ...
     <executions>...</executions>
     <configuration>
         <!-- items placed here are visible to the MOJO -->
     </configuration>
</plugin>

The plugin completely ignores any configuration items for an execution, though:

<plugin>
     ...
     <executions>
         <execution>
             <id>execution1</id>
             <phase>test</phase>
             <goals><goal>test</goal></goals>
             <configuration>
                <!-- items placed here are ignored -->
             </configuration>
         </execution>
     </executions>
</plugin>

I run Maven with mvn test. I'm sure that the execution takes place, as Maven prints its id correctly, but the plugin is not configured -- prints warnings about incorrect settings that are not present when the <configuration> section is moved outside of <executions>.

The question: is it the way the plugin is implemented, that it accepts only "top level" configuration? I've studied its source code and it seemed to me that it's Maven that invokes setters on a MOJO class and it's transparent to the plugin which section the options came from.

The MOJO is annotated with:

* @component
* @goal test
* @phase test
* @execute phase="jasmine-process-test-resources"
2

There are 2 best solutions below

3
On BEST ANSWER

The plugin in question is forking a custom lifecycle.

The forked custom lifecycle will have the execution with the specified id (execution1) removed (as it is a forked lifecycle)

Thus any of the plugin's goals that are performed by the forked lifecycle will be missing their configuration. The main mojo itself should be getting the configuration, but what is going wrong is the forked lifecycle executions.

I am guessing which plugin it is, if my guess is right, this is the custom lifecycle and the warnings you are seeing are coming from e.g. other mojos with text like

JavaScript source folder was expected but was not found. Set configuration property
`jsSrcDir` to the directory containing your JavaScript sources. Skipping 
jasmine:resources processing.

With a situation like this you will need to either put the <configuration> section in the outer block or configure the executions for the lifecycle.

Configuring the executions for the lifecycle will require adding executions with ids that are of the magic format. I am not 100% certain, but in your case you would be defining an additional execution with an ids of either default-resources or jasmine-lifecycle-resources in order to ensure that the configuration takes.

The less verbose way is just to put the configuration in the outer section and be done with it.

0
On

I had this issue with the base maven-install-plugin:2.5.2 using the maven:3.6.3-jdk-8 docker image.

Thanks to the accepted answer for putting me on the right track.

I don't fully understand this note in the documentation (at the end of the section), but it seems that you can give the phase goal an execution id forcing it to use your configuration:

Note: Configurations inside the element used to differ from those that are outside in that they could not be used from a direct command line invocation because they were only applied when the lifecycle phase they were bound to was invoked. So you had to move a configuration section outside of the executions section to apply it globally to all invocations of the plugin. Since Maven 3.3.1 this is not the case anymore as you can specify on the command line the execution id for direct plugin goal invocation. Hence if you want to run the above plugin and it's specific execution1's configuration from the command-line, you can execute:

mvn myqyeryplugin:queryMojo@execution1

My final working docker command:

docker run -it --rm --name parser -v "$(shell pwd)":/usr/src/parser -w /usr/src/parser maven:3.6.3-jdk-8 mvn -X install:install-file@install-my-jar-file

Where install-my-jar-file is my executions id <execution><id>install-my-jar-file</id>...