Maven cargo plugin with Surefire + Jacoco report - jacoco

I have a project that fires up tomcat using the cargo plugin and then runs integration tests against that tomcat instance. My goal is get integration test coverage reports of the code that runs in tomcat (not coverage of my integration tests).
The question is, how do I get code coverage results of code running in tomcat (separate JVM)?
I have been able to get coverage reports of the integration tests itself and java classes within the test module, however these are rather worthless.
In the jacoco-sessions.html file, I can only see classes that are available in the test module's classpath. Classes that are running in the tomcat server are not present.

In addition to the answer of Godin you also have to make sure that the coverage data of the test classes itself doesn't use the same output file as the coverage data of the code running in the cargo container. I think in my case the coverage-of-the-tests was overwriting the coverage-of-the-code-under-test.
Note that I am using jacoco.exec for the coverage data of the code-under-integration-test. Normally this is used for the unit tests, but there are no unit tests in my module. This way I don't need to configure SonarQube for an extra filename, but if you like you can use another filename here.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent-integration-cargo</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>argLineCargo</propertyName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<!-- ... -->
</container>
<configuration>
<properties>
<cargo.start.jvmargs>${argLineCargo}</cargo.start.jvmargs>
<!-- ... -->
</properties>
</configuration>
<!-- ... -->
</configuration>
<!-- ... -->
</plugin>
And the JaCoCo configuration in the parent POM:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<includes>
<include>my/project/package/**/*</include>
</includes>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>reports</id>
<goals>
<goal>report</goal>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>#{argLine}</argLine>
</configuration>
</plugin>
</plugins>
</build>

According to documentation of jacoco-maven-plugin http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html :
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.
argLine property affects maven-surefire-plugin and maven-failsafe-plugin that start JVM with tests - see http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine and http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#argLine respectively, but this property has no effect on cargo-maven2-plugin that starts JVM with Tomcat.
According to https://stackoverflow.com/a/38435778/244993 and https://codehaus-cargo.github.io/cargo/Tomcat+9.x.html you need to pass property set by jacoco-maven-plugin to cargo-maven2-plugin as cargo.jvmargs.

Related

Unit Test Case reporting for WSO2 EI

I am trying to integrate Unit Test cases in my existing wso2 code base.
Is there any way we can have a reporting structure like Surefire reports for our Unit Test Cases.
You need to add the plugin synapse-unit-test-maven-plugin in your pom.xml
<plugin>
<groupId>org.wso2.maven</groupId>
<artifactId>synapse-unit-test-maven-plugin</artifactId>
<version>5.2.27</version>
<executions>
<execution>
<id>synapse-unit-test</id>
<phase>test</phase>
<goals>
<goal>synapse-unit-test</goal>
</goals>
</execution>
</executions>
<configuration>
<server>
<testServerType>${testServerType}</testServerType>
<testServerHost>${testServerHost}</testServerHost>
<testServerPort>${testServerPort}</testServerPort>
<testServerPath>${testServerPath}</testServerPath>
</server>
<testCasesFilePath>${project.basedir}/test/${testFile}</testCasesFilePath>
<mavenTestSkip>${maven.test.skip}</mavenTestSkip>
</configuration>
</plugin>
The content of the tag <testCasesFilePath> is the path where units tests are saved

Jacoco.exec is not getting created for all modules in multi-module maven project

We are using JaCoCo for unit test code coverage in our multi-module maven project and able to get the coverage info(jacoco.exec) for most of the modules where unit tests are available. The only issue that i'm seeing is, jacoco.exec is not getting generated for some of the classes in few modules even though unit tests are available (note that surefire-reports are generated).
Able to get coverage info for the same unit tests when used Cobertura.
The following is the parent pom.xml where I've added jacoco-maven-plugin info.
Please help if there are any issues with pom.xml.
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>module4</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<argLine>#{argLine} -Xmx500m -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>
</plugins>
</build>
Thanks in advance!!

Changing the order of maven plugins execution

I am new to maven, I would like to change the order of the maven plugins execution.
In my pom.xml, I have maven-assembly-plugin and maven-ant-plugin.
maven-assembly-plugin for creating a zip file.
maven-ant-plugin for copying the zip file from target to some other directory.
When I run pom.xml, maven-ant-plugin got triggered, and looking for zip file finally I got the error saying zip file not found.
Please suggest to me the way how to run maven-assembly-plugin first before maven-ant-plugin so that it will find and copy the zip file to the corresponding directory.
Since you say you are very new to Maven....Maven builds are executions of an ordered series of phases. These phases are determined by the lifecycle that is appropriate to your project based on its packaging.
Therefore, you control when a plugin's goal is executed by binding it to a particular phase.
Hope that helps.
EDIT: Also, since Maven 3.0.3, for two plugins bound to the same phase, the order of execution is the same as the order in which you define them. For example:
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
...
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-2</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
...
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-3</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>generate-resources</phase>
...
</execution>
</executions>
</plugin>
In the above instance, the execution order would be:
maven-plugin-3 (generate-resources)
maven-plugin-1 (process-resources)
maven-plugin-2 (process-resources)
Plugins in the same phase are executed in the declared order.
In the case of pom hierachy, you have to re-declare the plugins from the parent pom (just its groupId and its artifactId) into the child pom to specify the execution order :
Parent pom.xml
<plugins>
<plugin>
<groupId>groupid.maven.1</groupId>
<artifactId>maven-plugin-1</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
Child pom.xml
<plugins>
<plugin>
<groupId>groupid.maven.2</groupId>
<artifactId>maven-plugin-2</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>package</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>groupid.maven.1</groupId>
<artifactId>maven-plugin-1</artifactId>
</plugin>
</plugins>
Then the execution is :
maven.plugin.2
maven.plugin.1
In Maven 3.0.3 and later, there are two rules
Plugin executions are ordered according to their phases. See
https://maven.apache.org/ref/current/maven-core/lifecycles.html for
the order of phases.
For example, here mavin-plugin-1 is executed before maven-plugin-2
because the process-resources phase is defined as taking place before
the compile phase.
<plugin>
<artifactId>maven-plugin-2</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>compile</phase>
...
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>process-resources</phase>
...
</execution>
</executions>
</plugin>
If multiple executions have the same phase, then the first one to be
executed will be the built-in one (e.g. maven-compiler-plugin) whose id
is default-something, then the other executions will take place in the
order they appear in your pom file.
For example, if you have this somewhere in your pom
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<id>my-compile</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-plugin-2</artifactId>
<version>4.5.6</version>
<executions>
<execution>
<id>my-compile-2</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
and this anywhere in your effective pom
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>**default-compile**</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
...
</executions>
</plugin>
then maven-compiler-plugin will execute maven-compiler-plugin followed by maven-plugin-1, and maven-plugin-2.
If you want maven-compiler-plugin:compile goal to execute after maven-plugin-1 then you could do this
<plugin>
<artifactId>maven-plugin-1</artifactId>
<version>1.2.3</version>
<executions>
<execution>
<id>my-compile</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>something-other-than-**default-compile**</id>
<phase>compile</phase>
</execution>
<execution>
<id>**default-compile**</id>
<phase>none</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
This might be an old questions and the answers provided are correct, just adding an extra point regarding the case for inheritance because I found myself in this situation and could not figure it out.
There seem to be 2 rules for plugin order execution when no inheritance is provided:
The phases to which the plugins are bound to are executed in the order provided in the documentation: see "Default Lifecycle" on Introduction to the Build Lifecycle
Plugin executions are ordered according to their phases.
"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" - source
IN THE CASE OF INHERITANCE: "Parent POM bindings execute AFTER child bindings.". So if your plugins are not executed in the order you expect them to (by following the previous two points mentioned in this answer), you should maybe look if they are defined in the parent POM. - Incorrect execution order of plugins in the same phase.
I could not find this being clarely documented anywhere, however there is an open ticket to document the algorithm calculating the order of plugin executions.

Running tests after packaging

One of my projects needs a pretty complex setup for the resulting JAR file, so I'd like to run a test after the package phase to make sure the JAR contains what it should.
How do I do that with Maven 2?
You can use the surefire-plugin for this. what you need to do is associate a phase with an execution (see below). You will need to change the phase to be whatever you want it to be in your case one after the package phase.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>unittests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
<includes>
<include>**/**/**/*Test.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Convert your project into a multi-module build. In the first module, build your original project. In the second module, add a dependency to the first.
This will add the first JAR to the classpath.
Update by OP: This works but I had to add this to my POM:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.maven-surefire-plugin}</version>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
The important part is <useSystemClassLoader>false</useSystemClassLoader>. Without this, my classpath only contained a couple of VM JARs plus the surefire bootstrap JAR (which contains the test classpath in the MANIFEST.MF). I have no idea why this test classpath isn't visible from the classes loaded from it.

Using maven2 to build autotools-based C/C++ package

I am working on a collection MATLAB, Java, and C/C++ components that all inter-operate, but have distinctly different compilation/installation steps. We currently don't compile anything for MATLAB, use maven2 for our Java build and unit tests, and use autotools for our C/C++ build and unit tests.
I would like to move everything to a single build and unit test system, using maven2, but have not been able to find a plugin that will allow the C/C++ codestream to remain autotools-based and simply wrap it in a maven build. Having to rip out autotools support and recreate all the dependencies in maven is most likely a deal-breaker, so I'm looking for a way for maven and autotools to play nicely together, rather than having to choose between the two.
Is this possible or even desirable? Are there resources out there that I have overlooked?
I don't really know autotools, but can't you use the maven exec plugin, that lets you execute system commands (or Java programs)? For example:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>exec-one</id>
<phase>compile</phase>
<configuration>
<executable>autogen</executable>
<arguments>
<argument>-v</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>exec-two</id>
<phase>compile</phase>
<configuration>
<executable>automake</executable>
<arguments>
<argument>-v</argument>
<argument>[other arguments]</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I didn't test the pom fragment above, but it gives you some hints about how to proceed.
You did overlook the maven cbuild parent suite. take a look at the "make-maven-plugin" section for more details.