Maven surefire plugin is not using argLine property provided by Jacoco - maven

This question is more focused on understanding maven life circle than in solving a real problem.
We have a project with several maven modules. Both Jacoco and Surefire plugins are configured in the parent pom.xml as follows:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
This configuration works well and the jacoco.exec files are created on the target directory when common goals are executed, for instance:
mvn clean install
or
mvn test
But if we execute the following commands the jacoco.exec files are not created:
mvn clean install -DskipTests
#other actions here...
mvn jacoco:prepare-agent surefire:test
Analyzing the logs with the -X option, surefire plugin indicates that it is going to use the argLine property as it is expected:
<argLine>${surefireArgLine}</argLine>
The goal jococo:prepare-agent generates the value for this variable correctly:
argLine set to -javaagent:s:\\m2_repo\\org\\jacoco\\org.jacoco.agent\\0.8.0\\org.jacoco.agent-0.8.0-runtime.jar=destfile=S:\\sources\\sofia2-s4c\\config\\services\\target\\jacoco.exec
But when surefire:test is executed it does not use the argLine property:
[DEBUG] (s) additionalClasspathElements = []
argLine SHOULD BE HERE!!!!
[DEBUG] (s) basedir = S:\sources\sofia2-s4c\config\services
We have solved this executing:
mvn clean install -DskipTests
#other actions here...
mvn test
Due to mvn test detect that there are no changes in the compiled classes this is efficient. But I would like to understand why the first approach does not work.

The problem is the <argLine> of the surefire plugin configuration.
Solutions from https://github.com/jacoco/jacoco/issues/964:
Define user property <argLine>...</argLine>. surefire will pick it up. No need to use ${surefireArgLine}
Keep the <argLine> in the surefire plugin configuration, but write it as <argLine>#{argLine} ${surefireArgLine}</argLine>
See also https://stackoverflow.com/a/23605812/510583

Related

Skip maven-cucumber-reporting plugin when integration test is skipped

This is a basic level question about maven plugin lifecycle.
When I run the command mvn clean install -Dskip.integration.tests=false then cucumber report is generated by the following plugin:
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>${project.name}</projectName>
<inputDirectory>${project.build.directory}/cucumber</inputDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<jsonFiles>
<jsonFile>cucumber.json</jsonFile>
</jsonFiles>
</configuration>
</execution>
</executions>
</plugin>
If I skip the integration tests using the maven command mvn clean install -Dskip.integration.tests=true then the maven build fails with the following error:
Execution execution of goal net.masterthought:maven-cucumber-reporting:5.5.0:generate failed: basedir app/target/cucumber does not exist
To what phase should the plugin be configured so that the maven build does not fail when integration test is skipped?
Add the skip variable to the plugin config whose value is based on whether the integration tests are skipped or not.
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<skip>${skip.integration.tests}</skip>
<projectName>${project.name}</projectName>
<inputDirectory>${project.build.directory}/cucumber</inputDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<jsonFiles>
<jsonFile>cucumber.json</jsonFile>
</jsonFiles>
</configuration>
</execution>
</executions>
</plugin>
in root pom.xml in the properties section add:
<skip.integration.tests>true</skip.integration.tests>
Now the plugin generates cucumber-html-reports folder when you run the maven command mvn clean install -Dskip.integration.tests=false. If you skip integration tests using the maven command mvn clean install -Dskip.integration.tests=true then the cucumber-html-reports folder will not be generated but maven build would still succeed.

antrun never copies jars

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>deploy</phase>
<configuration>
<target>
<copy file="${project.build.directory}/${project.artifactId}-${project.version}.jar" todir="${project.basedir}/../server/plug
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
After mvn install, I never see the jarfile copied from target/project.jar to ../server/plugins/project.jar.
Why isn't ant running?
Please see the Maven Lifecycle docs to see a list of the phases in Maven's default lifecycle. Note that deploy phase is after install.
The above POM shows the plugin execution is bound to the deploy phase, but the command run was mvn install. So the execution doesn't run.
You will either need to run mvn deploy, or change the phase to install or earlier phase, to see this command running.

maven-site-plugin not generating apidocs folder with javadocs

I inherited a project that is supposed to build javadoc files and place them in the site directory. This is not being done. I have looked at all the examples I can find and I can't figure out where the configuration is broken.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<reportPlugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</plugin>
</reportPlugins>
</configuration>
</execution>
</executions>
</plugin>
Any and all help is greatly appreciated.
You have bound maven site plugin's site goal to prepare-package phase. You have configured javadoc generation in this plugin configuration.
As such, if you run maven's default lifecycle goals like mvn package or mvn install you should get site report with javadocs.
If you ran mvn site, it would skip prepare-package phase to which your plugin configuration is bound and hence would not generate javadoc.

Cobertura with Maven - fail if coverage below threshold, but still generate site

I'm using Cobertura with Maven.
I'd like the build to fail if the coverage is below a given threshold, but I would like the site (including the Cobertura report) to still be generated. This is because developers will need to refer to the coverage report to see where they can add more coverage to fix the failed build.
Currently my pom looks like:
<project>
<build>
...
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
<configuration>
<check>
<totalLineRate>${cobertura.check.totalLineRate}</totalLineRate>
</check>
</configuration>
<executions>
<execution>
<goals>
<goal>clean</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>${cobertura.version}</version>
</plugin>
...
</plugins>
</reporting>
</project>
If I run mvn clean verify site, then it generates the HTML coverage report if the coverage target is met, but it fails the build without generating the report if the coverage target is not met. How can I change it to always generate the report?
Instead of failing the build if the code coverage target isn't met, is it possible to set it to mark the build as unstable instead? I know there are ways to do this through the Jenkins CI server, but I'm not sure if it's possible to accomplish this through pom.xml. Then again, "unstable" builds might be more Jenkins-specific, and might not exist as a possibility only through your pom file.
A quick workaround: remove the check goal:
<executions>
<execution>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
then run
mvn clean verify site cobertura:check
If you are using Hudson/Jenkins remove all checks from the pom.xml and install the Cobertura plugin to Hudson and configure the checks in the Hudson/Jenkins plugin.

Maven is not using failsafe plugin for integration tests

When I run mvn clean install, for the integration-test phase it does not use the failsafe plugin.
However if i explicilty call the plugin to run the integration test, it works (mvn failsafe:integration-test).
How can I make maven use the failsafe plugin when I run mvn clean install during the integration-test phase?
Quote from official documentation:
To use the Failsafe Plugin, you need to add the following configuration
to your pom.xml
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.11</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
Is this what you looking for?

Resources