How to run a Maven plugin after mvn gatling:test - maven

I need to run a Maven plugin after Gatling(gatling-maven-plugin) runs to do some cleanup of my target dir.
From the following article it is called out that mvn gatling:test is bound to the integration-test phase of Maven
https://github.com/gatling/gatling/blob/65524556b1aaee7d129cc9c4794977468493ad34/src/sphinx/extensions/maven_plugin.rst#usage
Knowing this and that plugins tied to the same phase run sequentially I added a plugin after gatling-maven-plugin tied to the same integration-test phase.
Unfortunately that plugin never runs. Appreciate any advice on how to get a plugin to run after gatling-maven-plugin runs.
Snippet of what i'm trying to do below. Appreciate any advice. I do something similar and successfully with the maven-surefire-plugin by adding a plugin after that tied to the test phase and it works perfect. Scratching my head on this one.
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<simulationsFolder>src/test/java</simulationsFolder>
<includes>
<include>perf.Test</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>myId</id>
<phase>integration-test</phase>
<goals>
<goal>run</goal>
<configuration>
<target>
<echo message="running something after gatling"/>
</target>
</configuration>
</execution>
</executions>
</plugin>

I find it strange that the plugin never runs.
Anyway, for cleaning up, I would use the post-integration-test phase.

same issue with teardown. Managed to do it within in gatling itself, but doesnt seem as perfect cleanup achieved! also interaction between the gatling and other plugins not possible during post-integration-phase. once gatling tests are started then the entire execution control ends with the same plugin.

Related

Can Sonar Scan be triggered as part of regular maven Build command like " mvn build"?

Im looking to see any maven configuration which will enable me to run Sonar Scan on my code for every maven build. I dont want to use a separate goal but somehow enforce it as part of users regular build commands.
You can attach Sonar to a phase (e.g. verify) like this:
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.5.0.1254</version>
<executions>
<execution>
<id>verify-sonar</id>
<phase>verify</phase>
<goals>
<goal>sonar</goal>
</goals>
</execution>
</executions>
</plugin>
This also works with other phases like compile or package.

Running Serenity -Cucumber Test cases in parallel

I'm new to Serenity and BDD. I've a small demo project based on Serenity-Cucumber and Page Based model. Below is the structure of the project:
The Login and Logout features have around 8 scenarios.
I want to be able to run the feature files in parallel. What is the easiest and most effective way to achieve this?
So far I have
Created separate Runner class for each feature and then used failsafe or surefire plugin - This is something I don't want as I don't want a new runner for each feature file.
Used the "cucumber-vm-parallel-plugin". I copy pasted below code in my pom file. Nothing happened.
<plugin>
<groupId>com.github.temyers</groupId>
<artifactId>cucumber-jvm-parallel-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>generateRunners</id>
<phase>validate</phase>
<goals>
<goal>generateRunners</goal>
</goals>
<configuration>
<glue>com.automationrhapsody.cucumber.parallel.tests</glue>
<featuresDirectory>src/test/resources/com</featuresDirectory>
<cucumberOutputDir>target/cucumber-parallel</cucumberOutputDir>
<format>json,html</format>
<tags>"~#ignored"</tags>
</configuration>
</execution>
</executions>
Looked into Serenity documentation and ran my program using following parameters, but could not achieve parallel execution.
mvn verify -Dthucydides.batch.count=2 -Dthucydides.batch.number=2
I'm stuck over here. Any help (easy and effective) will be appreciated.
Also, please suggest how options 2 and 3 above can be done correctly
Thanks.
You also need to add below plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<includes>
<include>**/*IT.class</include>
</includes>
</configuration>
</plugin>

Getting "Skipping JaCoCo execution due to missing execution data file" upon executing JaCoCo

I'm using Maven 3.0.3, JUnit 4.8.1, and Jacoco 0.6.3.201306030806, and I am trying to create test coverage reports.
I have a project with unit tests only, but I can't get reports to run, I'm repeatedly getting the error: Skipping JaCoCo execution due to missing execution data file when I run:
mvn clean install -P test-coverage
Here is how my pom is configured:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>-Xmx2048m</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<reuseForks>true</reuseForks>
<argLine>-Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
...
<profile>
<id>test-coverage</id>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<configuration>
<destfile>${basedir}/target/coverage-reports/jacoco-unit.exec</destfile>
<datafile>${basedir}/target/coverage-reports/jacoco-unit.exec</datafile>
</configuration>
<executions>
<execution>
<id>prepare-unit-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-integration-tests</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<propertyName>itCoverageAgent</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
All my tests run successfully. Here is some of the output from Maven:
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:prepare-agent (prepare-unit-tests) # myproject ---
[INFO] argLine set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
[INFO]
...
Tests run: 14, Failures: 0, Errors: 0, Skipped: 0
[INFO]
...
[INFO]
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:prepare-agent (prepare-integration-tests) # myproject ---
[INFO] itCoverageAgent set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
[INFO]
[INFO] --- maven-failsafe-plugin:2.14.1:integration-test (default) # myproject ---
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-failsafe-plugin:2.14.1:verify (default) # myproject ---
[INFO] Failsafe report directory: /Users/davea/Dropbox/workspace/myproject/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- jacoco-maven-plugin:0.6.2.201302030002:report (jacoco-site) # myproject ---
[INFO] Skipping JaCoCo execution due to missing execution data file
[INFO]
Any ideas what configuration I'm missing?
jacoco-maven-plugin:0.7.10-SNAPSHOT
From jacoco:prepare-agent that says:
One of the ways to do this in case of maven-surefire-plugin - is to
use syntax for late property evaluation:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine} -your -extra -arguments</argLine>
</configuration>
</plugin>
Note the #{argLine} that's added to -your -extra -arguments.
Thanks Slava Semushin for noticing the change and reporting in the comment.
jacoco-maven-plugin:0.7.2-SNAPSHOT
Following jacoco:prepare-agent that says:
[org.jacoco:jacoco-maven-plugin:0.7.2-SNAPSHOT:prepare-agent] Prepares a property pointing to the JaCoCo runtime agent that can be
passed as a VM argument to the application under test. Depending on
the project packaging type by default a property with the following
name is set:
tycho.testArgLine for packaging type eclipse-test-plugin and
argLine otherwise.
Note that these properties must not be overwritten by the
test configuration, otherwise the JaCoCo agent cannot be attached. If
you need custom parameters please append them. For example:
<argLine>${argLine} -your -extra -arguments</argLine>
Resulting
coverage information is collected during execution and by default
written to a file when the process terminates.
you should change the following line in maven-surefire-plugin plugin configuration from (note the ${argLine} inside <argLine>):
<argLine>-Xmx2048m</argLine>
to
<argLine>${argLine} -Xmx2048m</argLine>
Make also the necessary changes to the other plugin maven-failsafe-plugin and replace the following (again, notice the ${argLine}):
<argLine>-Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
to
<argLine>${argLine} -Xmx4096m -XX:MaxPermSize=512M ${itCoverageAgent}</argLine>
I faced a bit of a different issue that returned the same error.
Skipping JaCoCo execution due to missing execution data /target/jacoco.exec
The truth is, this error is returned for many, many reasons.
We experimented with the different solutions on Stack Overflow, but found this resource to be best. It tears down the many different potential reasons why Jacoco could be returning the same error.
For us, the solution was to add a prepare-agent to the configuration.
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
I would imagine most users will be experiencing it for different reasons, so take a look at the aforementioned resource!
One can also get "Skipping JaCoCo execution due to missing execution data file" error due to missing tests in project. For example when you fire up new project and have no *Test.java files at all.
There might a case where some other argline setup or plugin in pom may be overriding jacoco execution order setup.
argLine set to -javaagent:/Users/davea/.m2/repository/org/jacoco/org.jacoco.agent/0.6.2.201302030002/org.jacoco.agent-0.6.2.201302030002-runtime.jar=destfile=/Users/davea/Dropbox/workspace/myproject/target/jacoco.exec
One of the example
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<forkCount>5</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Dnet.sf.ehcache.disabled=true</argLine>
</configuration>
</plugin>
After getting rid of argLine from these plugins, jacoco started to work normally.
I know this question is pretty old but if someone like me comes here looking for an answer then this might help. I have been able to overcome the above error with this.
1) Remove the below piece of code from the plugin maven-surefire-plugin
<reuseForks>true</reuseForks>
<argLine>-Xmx2048m</argLine>
2) Add the below goal:
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
I've tried all answers but only the following combination of advice has worked for me. Why? I had very specific requirements:
JaCoCo generates report when build is run from command line: mvn clean verify (Maven 3.6.0)
Intellij IDEA (2019.01) runs my tests as well
It all works in presence of another javaagent defined in surefire plugin
Solution - prepend argLine value in surefire configuration with "late replacement" maven property #{...} as explained in surefire FAQ (my fixed configuration)
How do I use properties set by other plugins in argLine?
Maven does property replacement for
${...}
values in pom.xml before any plugin is run. So Surefire would never see the place-holders in its argLine property.
Since the Version 2.17 using an alternate syntax for these properties,
#{...}
allows late replacement of properties when the plugin is executed, so properties that have been modified by other plugins will be picked up correctly.
Failed first try - define jaCoCoArgLine property in prepare-agent goal configuration of jacoco - the scenario failed my second requirement, IntelliJ IDEA couldn't figure out agent for jmockit I use in the project for static method mocking
When using the maven-surefire-plugin or maven-failsafe-plugin you must not use a forkCount of 0 or set the forkMode to never as this would prevent the execution of the tests with the javaagent set and no coverage would be recorded.
ref: https://www.eclemma.org/jacoco/trunk/doc/maven.html
this is my gist
I was having the same issue. I updated the Jacoco version from 0.6 to 0.8 and updated surefire plugin as well. The following command generated Jacoco reports in site/jacoco/ folder:
mvn clean jacoco:prepare-agent install jacoco:report
My maven configurations are as follows:
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>PACKAGE</element>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>0.0</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<forkedProcessExitTimeoutInSeconds>60</forkedProcessExitTimeoutInSeconds>
<forkCount>1</forkCount>
</configuration>
</plugin>
</plugins>
Came accross the same problem just now.
I have a class named HelloWorld, and I created a test class for it named HelloWorldTests, then I got the output Skipping JaCoCo execution due to missing execution data file.
I then tried to change my pom.xml to make it work, but the attempt failed.
Finally, I simply rename HelloWorldTests to HelloWorldTest, and it worked!
So I guess that, by default, jacoco only recognizes test class named like XxxTest, which indicates that it's the test class for Xxx. So simply rename your test classes to this format should work!
FWhat tdrury said:
change your plugin configuration into this:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
<executions>
<!-- prepare agent for measuring integration tests -->
<execution>
<id>prepare-integration-tests</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
Edit:
Just noticed one important thing, destFile and dataFile seems case sensitive so it's supposed to be destFile, not destfile.
I struggled for days. I tried all the different configurations suggested in this thread. None of them works. Finally, I find only the important configuration is the prepare-agent goal. But you have to put it in the right phase. I saw so many examples put it in the "pre-integration-test", that's a misleading, as it will only be executed after unit test. So the unit test won't be instrumented.
The right config should just use the default phase, (don't specify the phase explicitly). And usually, you don't need to mass around maven-surefire-plugin.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Try to use:
mvn jacoco:report -debug
to see the details about your reporting process.
I configured my jacoco like this:
<configuration>
<dataFile>~/jacoco.exec</dataFile>
<outputDirectory>~/jacoco</outputDirectory>
</configuration>
Then mvn jacoco:report -debug shows it using the default configuration, which means jacoco.exec is not in ~/jacoco.exec. The error says missing execution data file.
So just use the default configuration:
<execution>
<id>default-report</id>
<goals>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
And everything works fine.
I know this is late, but just for anyone else who has this issue and nothing seems to fix it (like I had). Make sure that in you pom, your configuration for jacoco inside plugins is not in turn inside pluginManagement. It seems some sort of default for maven is to put the plugins inside pluginManagement. This is almost unnoticeable until you try to add detailed configurations like for jacoco. In order to add these, you need to add them outside of the pluginManagement, otherwise they are effectively ignored. See my poms below for details.
My old pom (that gave the "Skipping jacoco ..." message):
<project>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
My new pom (that now compiles a working jacoco report):
<project>
...
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
It happens if the path of your project has blank spaces anywhere, such as:
/home/user/my projects/awesome project
the report is not generated. If that is the case, remove those spaces from directory names, such as:
/home/user/my-projects/awesome-project
or move the project to a directory that doesn't have spaces along the way.
About the plugin configuration, I just needed the basic as below:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Jacoco Execution data file is a jacoco.exec file which is generated when prepare agent goal is running. When It isn't generated or the correct path isn't set in configuration, you will get that error message. Because Jacoco use It to build test coverage. This usually occur when you use jacoco maven plugin and surfire or failsafe. To ensure that the jacoco.exec file is generated, you have to add argline in you pom.xml file, not in the surfire configuration but inside a properties tag in you pom.xml file.
<properties>
<argLine>-Xmx2048m</argLine>
</properties>
The execution says it's putting the jacoco data in /Users/davea/Dropbox/workspace/myproject/target/jacoco.exec but your maven configuration is looking for the data in ${basedir}/target/coverage-reports/jacoco-unit.exec.
I have added a Maven/Java project with 1 Domain class with the following features:
Unit or Integration testing with the plugins Surefire and Failsafe.
Findbugs.
Test coverage via Jacoco.
Where are the Jacoco results? After testing and running 'mvn clean', you can find the results in 'target/site/jacoco/index.html'. Open this file in the browser.
Enjoy!
I tried to keep the project as simple as possible. The project puts many suggestions from these posts together in an example project. Thank you, contributors!
My response is very late but for others users
In your case you have to configure failsafe pluging to use the command line agent configuration saved in itCoverageAgent variable.
For exemple
<configuration>
<argLine>${itCoverageAgent}</argLine>
</configuration>
In your maven configuration, jacoco prepare the command line arguments in prepare-agent phase, but failsafe plugin doesn't use it so there is no execution data file.
Sometimes the execution runs first time, and when we do maven clean install it doesn't generate after that.
The issue was using true for skipMain and skip properties
under maven-compiler-plugin of the main pom File.
Remove them if they were introduced as a part of any issue or suggestion.
In my case, the prepare agent had a different destFile in configuration, but accordingly the report had to be configured with a dataFile, but this configuration was missing. Once the dataFile was added, it started working fine.
I faced similar error because tests execution were skipped.
I ran my build with similar system parameter : -Dmaven.test.skip.exec=true
Turning this system parameter to false solved the issue.
I just removed following two lines from properties tag
<jacoco.ut.reportPath>${project.basedir}/../target/jacoco.exec</jacoco.ut.reportPath>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
mvn install -Psonar by default creates jacoco.exec in target directory, so explicit path was not needed in my case.
For others that met similar problem, here is another possible solution:
Our project was running in a Traditional Chinese version of Windows, and when we checked prepare-agent section in maven log, we found that it tried to read the project folder which we put on desktop, through \桌面\...\project\... instead of \Desktop\...\project\.... I think UTF-8 symbol in paths make Jacoco go weird. We moved the project into other place and the issue was fixed.
TL;DR:
Check prepare-agent logs too as jacoco.exec was prepared during that.
2 Other Possibilities
JaCoCo's Maven plugin could be better integrated with Maven to provide more information about it's invocation and incorrect invocations. Nonetheless.
Possibility #1: Custom Arguments In Surefire plugin
Using JPMS module for my project with standard Maven directory layout, basic JSE 11 program, JUnit 5 & JaCoCo, with Eclipse and single module-info.java file (Eclipse 4.13 won't allow 2 module-info.java files in the project's root of the classpath). In order for Surefire to see my test cases I had to use the single <argLine> tag to allow Surefire to gain access using: --add-opens for all of the packages having unit tests:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>
#{argLine}
--add-opens module_name/com.myco.project=ALL-UNNAMED
--add-opens module_name/com.myco.project.more=ALL-UNNAMED
--add-opens module_name/com.myco.project.others=ALL-UNNAMED
</argline>
</configuration>
...
</plugin>
According to Jacoco documentation, you have to directly pass the Jacoco arguments to Surefire since specifying any Surefire arguments using <argLine> overrides Jacoco's defaults. Jacoco's online Maven documentation specifies using #{argLine} to pass Jacoco's arguments to Surefire (https://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html).
Possibility #2: Intermingling Plugins
I also use the maven-javadoc plugin in my <reporting/> section. Incidentally, during the Javadoc reporting phase, it manages to invoke the prepare-agent goal of JaCoCo and gives the OP's error message shortly thereafter (according to maven debug, specifically while the maven-project-info-reports-plugin is configuring the reports to begin generating for maven site) - this despite the fact that my Test phase has already run and dumped a proper jacoco.exec file in the build output directory. It may be safe to ignore this warning, my JaCoCo report renders fine in my Maven Site. However, if you're seeing it during the execution of a JaCoCo goal, it probably shouldn't be ignored.
Tips
If you're having doubts about the file getting created, watch the directory where the file is supposed to appear during the build. Generating the file is a fairly slow process. See my other answer here: https://stackoverflow.com/a/75465187/2336934
Do your best to keep to all the defaults as much as possible, simpler is better.

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.

JAXB2 Maven Plugin not reading Configuration

I am trying to generate some JAXB classes via my schema. I have my jaxb2 maven plugin configured the following way.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.3.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>aces.soa.schema</packageName>
<generateDirectory>${project.build.directory}/generated-sources</generateDirectory>
<schemaDirectory>${basedir}/src/main/resources/schema/ea</schemaDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
When running the mvn jaxb2:xjc it complains that it cannot find the Schema, which is in the src/main/resources/schema/ea directory. When executing mvn -X jaxb2:xjc I see that the variables are not getting set at all. Is there something else I need to do to configure the plugin propoerly?
There's an issue with running this plugin with configuration elements in the execution elements, when the plugin is being called using:
mvn jaxb2:xjc
A workaround for me was using:
mvn generate-sources
Actually no. Having configuration outside executions configures the plugin globally. Here is the doc: http://mojo.codehaus.org/jaxb2-maven-plugin/usage.html
So, you should be able to configure each execution but it does not work some reason :( The question is still valid

Resources