what does having two goals in same plugin mean in maven? - 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.

Related

Can we execute exec-maven-plugin before maven-surefire-plugin?

Is it possible to run exec-maven-plugin before maven-surefire-plugin, what I observe during my run is maven-surefire-plugin is executing first even though the sequence in tag is second. My scenario is to execute JAVA CLASS (using exec-maven-plugin ) which generates my testng.xml and can run that using (maven-surefire-plugin).
First of all, if you have an execution of the exec-maven-plugin bound to the test phase, it is normal that this execution is performed after the one of the maven-surefire-plugin. The reason is that you're likely dealing with a project of packaging jar, which has a default binding of the Surefire Plugin to the test phase. This default execution is always the first one invoked, regardless of where the plugin is declared in the POM. In the logs, you will spot this execution with an id of default-test.
There is a way to perform actions before the tests are run by leveraging the phases invoked before the phase test. In your case, your goal is to generate a test resource, the testng.xml, so it would be appropriate to use the generate-test-resources phase, whose purpose is to create resources that are needed for the tests. Therefore, you just need to specify
<phase>generate-test-resources</phase>
to the execution of the exec-maven-plugin generating the testng.xml.
Then, you can use the generated testng.xml with the suiteXmlFiles element, see Using Suite XML Files
This is how I have implemented:
I have added test script/java main class, I want to execute before Cucumber Test Suite, in following folder:
enter image description here
Added following in POM.xml in ...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java/BeforeSuite</source> <!-- source folder where Before Suite scripts are saved -->
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>before-test-suite-scripts</id>
<phase>generate-test-resources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>BeforeSuite.HelloBeforeSuiteScript</mainClass> <!-- <packagename>.<className> -->
</configuration>
</execution>
</executions>
</plugin>
When running mvn clean verify, before test suite script will run prior the test suite execution.
enter image description here

Explanation of an execution of maven-resources-plugin

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.

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 can I skip tests in maven install goal, while running them in maven test goal?

I have a multi-module maven project with both integration and unit tests in the same folder (src/test/java). Integration tests are marked with #Category(IntegrationTest.class). I want to end up with the following setup:
If I run mvn install, I want all tests to compile, but I do not want to execute any.
If I run mvn test, I want all tests to compile, but execute only unit tests.
If I run mvn integration-test, I want to compile and execute all tests.
The important point is, I want this configured in the pom.xml without any extra commandline arguments.
Currently I came up with the following setup in my parent pom.xml, where the only problem is #1, where all tests are executed:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${project.java.version}</source>
<target>${project.java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
<excludedGroups>cz.cuni.xrg.intlib.commons.IntegrationTest</excludedGroups>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14.1</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.14.1</version>
</dependency>
</dependencies>
<configuration>
<groups>cz.cuni.xrg.intlib.commons.IntegrationTest</groups>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
All child modules have the following plugin configuration in their pom.xml, which I believe should inherit from the parent pom:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
</plugins>
</build>
I tried using <skipTests>true</skipTests>, but it disables test execution for all goals, which is not what I want (violates #2 and #3). It is also quite weird, that mvn test honors the skipTests=true option...why would I want to run it in the first place??
After hours of googling and trying different combinations, I am hesitant whether it is even possible to not run tests in mvn install, while at the same time run them in mvn test. I hope someone proves this wrong. ;)
I am also willing to accept a solution, where mvn install would execute only unit tests, but I don't think it makes much difference.
It sounds like you didn't understand the concept of the build life-cycle in Maven. If you run mvn install all life-cycle phases (including the install phase itself) run before the install phase. This means running the following phases:
validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
prepare-package
package
pre-integration-test
integration-test
post-integration-test
verify
install
which means in other words the test as well as integration-test life-cycle phases are included. So without any supplemental information it's not possible to change the behaviour as you wish it.
It could be achieved by using a profile in Maven:
<project>
[...]
<profiles>
<profile>
<id>no-unit-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
[...]
</project>
So your first requirement:
If I run mvn install, I want all tests to compile, but I do not want to execute any.
can be achieved by using the following:
mvn -Pno-unit-test test
If I run mvn test, I want all tests to compile, but execute only unit tests.
This can simply achieved by using the plain call:
mvn test
cause the integration tests phase is not run (see the build life cycle).
If I run mvn integration-test, I want to compile and execute all tests.
This means running the default which includes running the test phase which will run the unit tests (maven-surefire-plugin) and furthermore running the integration test which are handled by the maven-failsafe-plugin. But you should be aware that if you like to call the integration tests you should using the following command:
mvn verify
instead, cause you missed the post-integration-test phase in your previous call.
Apart from the above you should follow the naming conventions for unit and integration tests where unit tests should be named like the following:
<includes>
<include>**/*Test*.java</include>
<include>**/*Test.java</include>
<include>**/*TestCase.java</include>
</includes>
and integration tests should be named like the following:
<includes>
<include>**/IT*.java</include>
<include>**/*IT.java</include>
<include>**/*ITCase.java</include>
</includes>
I hope you have configured the maven-failsafe-plugin like the following which is needed to bound the maven-failsafe-plugin to the correct life-cycle-phases:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
as you correctly did, but you should be aware that the include tags work on the source code (.java) and not on the compiled names (.class). I wouldn't use the Category annotation, just simply using the naming conventions makes the pom simpler and shorter.
According to the Failsafe Plugin documentation
mvn install -DskipITs
is what you want.
What OP stated in his question:
If I run mvn install, I want all tests to compile, but I do not want
to execute any.
If I run mvn test, I want all tests to compile, but execute only unit tests.
If I run mvn integration-test, I want to compile and execute all tests.
is perfectly valid and extremely easy to achieve.
EDIT: except first condition, which acts againts the maven nature. The best way here would be simply do mvn install -DskipTests
All you need is following snippet in pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
and to stick to the maven naming conventions for unit and integration tests (as #khmarbaise already stated). So generally name you integration tests with IT suffix (for example MyIntegrationTestIT.java) and let maven-failsafe do its job.
In that way, you do not even need JUnit categories (although sometimes they can be quite useful).
That's it :)
mvn test executes only unit tests
mvn integration-test executes all tests
mvn failsafe:integration-test runs only integration tests
mvn clean verify when you want to be sure, that whole project just works
Some personal advices
Keeping integration tests separately from unit tests lets you easily run within your IDE all tests in some package. Usually additional directory called test-integration (or integrationtest) is used for this purpose.
This is also easy to achieve with maven:
<plugin>
<!-- adding second test source directory (just for integration tests) -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-integration-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test-integration/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
And then move your integration tests to that directory. It should look like:
src
main
test
test-integration
Integration tests usually needs more memory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
...
<configuration>
<argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>
This post explains how to skip integration tests, no matter what plugin you are using for these tests.
Basically, what you do is define a profile and put all your integration-tests related xml code inside that profile. Than you activate it when a property -DskipIntegrationTests is missing.
You can do the same for unit tests: write a profile and activate it when -DskipUnitTests is missing.
Then, you could do:
mvn install -DskipIntegrationTests -DskipUnitTests # (runs install without any tests)
mvn test # (runs unit tests)
mvn post-integration-test # (runs all tests)
The maven-failsafe-plugin docs has a section titled "Skipping by Default."
Sadly, the steps that page describes don't work as written. However, a slight change to those steps will make it work:
In the properties section of pom.xml, add this:
<skipITs>true</skipITs>
Then add the skipTests property to the plugin section of maven-failsafe-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skipTests>${skipITs}</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
So now, an mvn install by default will execute unit tests, but not integration tests.
But an mvn install -DskipITs=false will execute both unit tests and integration tests.
Footnote: Bad documentation played a big part on why Maven was so disliked for such a long time.
mvn test-compile does exactly what you are looking for. You can simply replace mvn install with mvn test-compile and you are done. No need to customise the pom file or anything. The below linked question is similar around #1:
Maven - How to compile tests without running them ?
mvn test-compile should be accepted as the best answer as Maven supports exactly what you want to do natively and without any magic. You would end up with this:
If I run mvn test-compile, I want all tests to compile, but I do not want to execute any.
If I run mvn test, I want all tests to compile, but execute only unit tests.
If I run mvn integration-test, I want to compile and execute all tests.
Don't specify the execution step(s) in the configuration of the failsafe plugin. E.g.
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
</plugins>
Now, you specifically need to call mvn failsafe:integration-test to run the integration tests; they will be skipped in other mvn targets.

Can maven sortpom plugin affect a project's build result?

Can the Maven Sortpom Plugin affect the result of a project's build ?
Is it possible to have a project build fail just because the sortpom plugin was added ?
Normally the order of the elements in a pom.xml file does not matter, so reordering elements should not affect the build.
But I know of two exceptions to this rule:
Maven reads dependencies according to the order in the pom-file when compiling. Rearranging that order may affect the compilation output.
If two plugins executes in the same phase, the order in pom-file will determine which plugin to execute first. Sorting the plugins may cause the compilation to fail if the result of one plugin is dependent on another.
The sortpom plugin does not sort either dependencies nor plugins by default. So I would say that the sortpom plugin should not affect the result of a projects build.
It can fail a build:
[ERROR] Failed to execute goal com.google.code.sortpom:maven-sortpom-plugin:2.3.0:sort (default) on project data-extractor: scm.team.company.corp: Unknown host scm.team-project.company.corp -> [Help 1]
The if the file isn't found due to network problems, even when running with -o
Yes.
For example, you use:
org.codehaus.mojo:build-helper-maven-plugin's reserve-network-port goal in phase pre-integration-test
org.apache.tomcat.maven:tomcat7-maven-plugin's run goal also in phase pre-integration-test
Now, sortpom:sort orders them around, and in maven-3, the order of the plugins are important. So if you configure a random port for tomcat through the portName feature of reserve-network-port, the system property won't be filled (at the point it is needed), as after the sort, the build-helper artifact is executed AFTER the run goal is invoked.
Example after a sorting:
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<!-- ... -->
</executions>
<configuration>
<fork>true</fork>
<port>${tomcat.http.port}</port><!-- Oops, not set (yet)! -->
</configuration>
</plugin>
<!-- ... -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper.version}</version>
<executions>
<execution>
<id>reserve-tomcat-port</id>
<phase>pre-integration-test</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
<configuration>
<portNames>
<portName>tomcat.http.port</portName><!-- Too late -->
</portNames>
</configuration>
</execution>
</executions>
</plugin>

Resources