Explanation of an execution of maven-resources-plugin - maven

I have inherited a project from another developer and there is a bit in the pom.xml that I don't quite understand. It's as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter</id>
<phase>generate-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
Can someone shed some light as to what it's trying to do? I think it's saying, "do filtering when running ANY generate-resources phase". Is this correct?

No, your interpretation is incorrect.
This is declaring an execution for the maven-resources-plugin. This execution has an id of filter but this is just a technical identifier and it has no involvement in the build itself. This execution is bound to the generate-resources phase of the default life cycle and it invokes the resources goal of that plugin.
Having said that, this is really strange and may be completely useless. By default, the resources goal of the maven-resources-plugin is already invoked in the default lifecycle in the process-resources phase (which is right after the generate-resources phase). What this goal does is to copy all of the resources of the project (i.e. what is under src/main/resources by default) to the main output directory (i.e. the target folder by default). Hence, this task will be performed twice: once in the generate-resources phase (per the execution in your POM) and another time in the process-resources phase by default.

Related

What is the difference between executions and configurations in a maven plugin?

I found this description but it does not seem comprehensive. Could someone explain in detail what is the difference between executions and configurations in a maven plugin?
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.
You can define <configuration>s on the <plugin> level or the <execution> level. The former is globally valid for all executions, the latter is specific to the execution.
Example for global an execution-specific configurations:
Suppose, you have to compile your project with Java 1.7 but you want to early adopt Java 9 Jigsaw features and add a module-info.java to your project. This Java file will not compile using source level 1.7. What you can do is define two executions of the maven-compiler-plugin, one that compiles everything except module-info.java with source level 1.7 and one that does only compile module-info.java with source level 1.9:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<!-- Global plugin configuration for source and target levels. -->
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<!-- Compile all code except module-info.java with the configured source level -->
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<excludes>
<exclude>module-info.java</exclude>
</excludes>
</configuration>
</execution>
<!-- Compile module-info.java with source level 1.9 -->
<execution>
<id>compile-module-info-java</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<source>1.9</source>
<target>1.9</target>
<includes>
<include>module-info.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
I think answer from #Stefan is already quite clear. I would like to be even a bit more verbose in case it helps.
"execution" under plugin is declaring "what should be done at when". Basically, an execution usually at least contains: phase and goal (I know you don't always see it in config, but conceptually they are there), which you can see it as: When the build process reached phase, then the goal action of the plugin will be executed.
Of course, you can have multiple executions for a plugin, so that different/same goals can be run in different/same phases.
Then come to configuration. Sometimes you need to tell the plugin extra detail on how the plugin should act on because the plugin may not be able to guess what you want to do by default. configuration is doing such work. You can refer to document of plugin's goal to see what kind of configuration they accept.
Plugin level configuration will be applied to all executions of the plugin, while you can also define configuration under each execution, which serve as execution-specific configuration. Plugin-level configuration + execution-level configuration is the "real" configuration received by an execution.
A <configuration> section outside an <execution> block affects the plugin's behavior in general way. For instance, plugins that are either executed directly through the CLI or have a default phase that they bind to will use this type of configuration. An example of such a plugin would be the compiler plugin.
On the other hand, a <configuration> section inside an <execution> block only applies to that specific execution.
As always, a more specific configuration can override a general configuration. So if a general configuration (outside an execution block) says <doCheck>false</doCheck>, an execution might choose to only override this by doing <doCheck>true</doCheck>.
Another feature of general configurations is that their parameters will be inherited by all executions of that plugin.

what does having two goals in same plugin mean in maven?

Here is an example of pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<configuration>
......
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<phase>integration-test</phase>
</execution>
</executions>
</plugin>
</plugins>
what does having two goals here mean?
Does verify goal (bound to verify phase) will be executed in integration-test phase?
Does both integration-test and verify goal execute during integration-test and in what order?
when I run mvn clean verify, does verify goal executed twice? one during integration-test and one during verify?
EDIT
Given the answer below and the linked documentation, does it mean the plugin is invoked twice when I run mvn verify - one in integration-test phase and in verify phase.
First your given configuration does not make sense, cause the documentation says different:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18.1</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
The subtle difference is giving the phase or not. In your case you have given phase which means binding both goals integration-test and verify to the same life cycle phase. If you omit the phase as in my example (copied from the documentation) it means the two goals will be bound to the life cycle phases the plugin developer have thought it would be useful. This means binding the integration-test goal to the integration-test life cycle phase and the verify goal to the verify life cycle phase.
You can see the phase to which a goal is bound within the documentation like here (excerpt from the doc):
Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: test.
The goal is thread-safe and supports parallel builds.
Binds by default to the lifecycle phase: integration-test.
And the last line will give you the information to which life cycle phase the plugin developers have decided to bind the plugin goal by default.
Now answering your questions:
Given them in that way means executing those two goal within the same life cycle phase (integration-test). The order is given by the order in the pom file.
Does verify goal (bound to verify phase) will be executed in
integration-test phase?
No. Based on the given configuration verify goal is NOT bound to verify life cycle phase, cause it's bound to integration-test phase.

How do I know what are different goals available for plug-in in maven?

I recently started using maven. so this question might sound basic.
This question came up when I was browsing through some code using cargo plug-in.
In the following snippet of maven plugin in pom.xml, that i extracted from here,
my understanding is as follows:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
[Cargo plugin configuration goes in here]
</configuration>
</plugin>
This plug in bound to pre-integration-test and post-integration-test phase of build LifeCycle, which also means when I run mvn install this will be executed.
The goals (start and stop) of this plug-in gets executed during these phases respectively
Q1:: Does the <id>start-container</id> has any relevance? what's its purpose & importance?
Q2:: How do I know what are the different goals available for a plug-in. In this case for cargo plug-in I see in one of the codes in my work, <goal>redeploy</goal> is used. so I am wondering how to find information on these specific goals and other goals available. I did look at online documentation. I did not find any. possible that I did not search in the right place.
A1: the id doesn't change how the execution works, it's just a way of giving it a name.
A2: The best way is to read the documentation. Maven3 is also considerably better than maven2 in this aspect. If you call a plugin with an invalid goal, it will print out all the valid goals, but it won't print what are the different parameters that can be passed to the plugin (and some plugins use different parameter names for command line and pom parameters)
The documentation of cargo is a bit odd, most other plugins have their documentation set up in a different way, which makes it easier to find the goals and the parameters that can be set.
By the way, both your points 1 and 2 are correct.

When plugin goal is executed: before or after declared phase?

I have a question about maven pom. I have this pom.xml
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Hello world!</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
when the goal run is executed? Before or after package phase?
(for Maven lifecycle see Maven lifecycle)
Plugin goal is execute after beginning of declared phase (package), and before beginning of next phase (pre-integration-test).
In this scenerio maven command
mvn package
should print Hello World
Multiple executions in one phase:
Note: In Maven 2.0.5 and above, multiple goals bound to a phase are
executed in the same order as they are declared in the POM, however
multiple instances of the same plugin are not supported. Multiple
instances of the same plugin are grouped to execute together and
ordered in Maven 2.0.11 and above).
and
When multiple executions are given that match a particular phase, they
are executed in the order specified in the POM, with inherited
executions running first.
Source: Introduction to the Build Lifecycle
In the case you have given it will be executed in the package phase, cause you explicit defined it to be executed during the package phase. If you like to get it executed before the package phase you need to use the prepare-package phase instead package.

How do plugin goals tie into build phases in maven

How do plugins add to build phases.
I realize that maven has a list of goals that it executes by default but when we add a plugin node to the
pom.xml,
For example, as per the maven documentation if we include the following plugin
<plugin>
<groupId>org.codehaus.modello</groupId>
<artifactId>modello-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<configuration>
<models>
<model>src/main/mdo/maven.mdo</model>
</models>
<version>4.0.0</version>
</configuration>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
</plugin>
Q1. What build phase does it tie into by default ?
Q2. Does it get executed in addition to the 'default' goal?, for example if i have a plugin that just echos 'hello'
and it gets tied to the compile phase, do i get a echo of 'hello' in addition to the compilation?
Thanks
Venu
You can see that in the documentation of the plugin which says the java goal is bound to generate-sources.
What you are talking about default goal does not exist. It makes more sense in the meaning of a default binding between maven-plugin:goal and the life-cycle phase in relationship with the packaging type of a project. There you have a kind of default binding which is defined in the Maven super pom which defines the goals and their execution in the life-cyclce.

Resources