Maven cargo in parent project with multiple submodules: tomcat port number is in use - maven

I use maven cargo plugin and maven failsafe plugin to run my integration tests.
I have a parent project and two submodule projects:
IT-parent
--> IT-submodule1
--> IT-submodule2
My problem is Maven Cargo on parent project starts the tomcat, then also the submodules start and stop tomcat.
Parent pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.15</version>
<configuration>
<container>
<containerId>tomcat7x</containerId>
<type>installed</type>
<home>${CATALINA_HOME}</home>
</container>
<configuration>
<type>existing</type>
<home>${CATALINA_HOME}</home>
<properties>
<cargo.servlet.port>7080</cargo.servlet.port>
<cargo.tomcat.ajp.port>7009</cargo.tomcat.ajp.port>
<cargo.jvmargs>-Xms128m -Xmx512m -XX:MaxPermSize=256m</cargo.jvmargs>
</configuration>
<deployables>
...
</deployables>
</configuration>
<executions>
<!-- start server before integration tests -->
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>deployer-deploy</goal>
<goal>start</goal>
</goals>
</execution>
<!-- stop server after integration tests -->
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>deployer-undeploy</goal>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
Submodule pom.xml has no <build> section.
Is there a way to start tomcat with maven cargo once at parent project and submodules run only integration tests in that container. So I wouldn't have a series of start/stop tomcat equals to the number of submodules.

1.Parent pom.xml
Move maven-failsafe-plugin to plugin management:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
..
..
2.Parent pom.xml
+ IT-submodule1
+ IT-submodule2
Activate maven-failsafe-plugin in IT-submodule1 =>
Add declaration
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<plugin>
</plugins>

Related

Jacoco Report Aggregate Coverage Report issue on Sonar

I have a multi-module maven project. I am facing issues while generating the sonar report for the jacoco report-aggregate coverage.
I have 2 modules (A and B) for which I want to generate the report.
I have created another module (jacoco-report-aggr) under the parent module for generating the report-aggregate of jacoco.
When I run "mvn clean install", the jacoco index.html fille generated in the jacoco-report-aggr module shows the coverage for both modules A and modules B. But when I run "mvn sonar:sonar" the sonar report generated shows
the coverage for only module B but not module A.
Attaching the corresponding parts of the xml files.
I have the parent pom.xml as below.
<sonar.java.coveragePlugin>jacoco-maven-plugin</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
<sonar.language>java</sonar.language>
<jacoco.version>0.8.5</jacoco.version>
<surefire.plugin.version>2.19.1</surefire.plugin.version>
<aggregate.report.dir>jacoco-report-aggr/target/site/jacoco-aggregate/jacoco.xml</aggregate.report.dir>
<sonar.coverage.jacoco.xmlReportPaths>/jacoco-report-aggr/target/site/jacoco/jacoco.xml,/jacoco-report-aggr/target/site/jacoco-aggregate/jacoco.xml,
${basedir}/../${aggregate.report.dir},
${basedir}/../../${aggregate.report.dir},
${basedir}/../../${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
<!--Jacoco Code Coverage Plugin. Only start the agent-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
<configuration>
<propertyName>jacoco.agent.argLine</propertyName>
<destFile>target/jacoco.exec</destFile>
</configuration>
</execution>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<!--Maven Surefire Plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
</plugin>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.7.0.1746</version>
</plugin>
The child modules pom looks like this.
<properties>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.dir}</sonar.coverage.jacoco.xmlReportPaths>
</properties>
The pom.xml file of the jacoco-report-aggr module looked like this
<dependencies>
<dependency>"Module A"</dependency>
<dependency>"Module B"</dependency>
</dependencies>
<properties>
<sonar.coverage.jacoco.xmlReportPaths>${basedir}/../${aggregate.report.dir}
</sonar.coverage.jacoco.xmlReportPaths>
</properties>
<build>
<plugins>
<!--Maven Surefire Plugin-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
</plugin>
<!--Jacoco Aggregate Report plugin over dependencies-->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Unit tests are getting executed twice when performing mvn install

Unit tests are getting executed twice.
When i am removing goal report and phase prepare-package from maven plugins in pom, test are getting executed once but then coverage are not getting generated in the console.
But when i am adding goal report and phase prepare-package from maven plugins in pom,i am getting coverage in the console but unit tests are getting executed twice.
I need to have goal report and phase prepare-package in my pom in order to get coverage but need to run test cases only once. What is the way to to get the test case executed only once with coverage as well.
<plugins>
<!-- Configure maven-compiler-plugin to use the desired Java version -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<!-- Use build-helper-maven-plugin to add Scala source and test source
directories -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/scala</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Use scala-maven-plugin for Scala support -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<goals>
<!-- Need to specify this explicitly, otherwise plugin won't be called
when doing e.g. mvn compile -->
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- disable surefire -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>${scoverage.plugin.version}</version>
<configuration>
<aggregate>true</aggregate>
<highlighting>true</highlighting>
<scalacPluginVersion>1.3.0</scalacPluginVersion>
<minimumCoverage>30</minimumCoverage>
<failOnMinimumCoverage>false</failOnMinimumCoverage>
</configuration>
<executions>
<execution>
<goals>
<goal>report</goal> <!-- or integration-check -->
</goals>
<phase>prepare-package</phase> <!-- or any other phase -->
</execution>
</executions>
</plugin>
<!-- enable scalatest -->
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}"/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar
</argLine>
<forkMode>once</forkMode>
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
<junitxml>.</junitxml>
<filereports>WDF TestSuite.txt</filereports>
<!-- The scalatest-maven-plugin seems broken for the spanScaleFactor,
so pass it via system property, instead -->
<!-- <spanScaleFactor>${scalatest.span.scale.factor}</spanScaleFactor> -->
<systemProperties>
<spanScaleFactor>${scalatest.span.scale.factor}</spanScaleFactor>
</systemProperties>
</configuration>
<executions>
<execution>
<id>test</id>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>enter code here
You need to do 2 things:
Add following line in 'scoverage-maven-plugin' plugin's 'configuration' section.
<additionalForkedProjectProperties>skipTests=false</additionalForkedProjectProperties>
Set following property in your root pom.xml
<properties>
<!-- Add other properties here... -->
<!-- skipTests is set to 'true' here to avoid duplicate runs of all test cases. It's set to false in scoverage-maven-plugin below. -->
<skipTests>true</skipTests>
</properties>

How to run embedded Tomcat 9 inside Maven 3 for integration testing purposes?

I am trying to run embedded Tomcat 9 inside Maven 3 for integration testing purposes. I was led to cargo-maven2-plugin by other SO answers.
So, attempting to follow the instructions found here:
https://codehaus-cargo.github.io/cargo/Static+deployment+of+WAR.html
I have this fragment in a simple POM:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.7.6</version>
<configuration>
<container>
<containerId>tomcat9x</containerId>
<type>embedded</type>
</container>
<deployables>
<deployable>
<type>war</type>
<properties>
<file>path/to/myapp.war</file>
</properties>
</deployable>
</deployables>
</configuration>
</plugin>
</plugins>
</build>
Which I try to execute with mvn org.codehaus.cargo:cargo-maven2-plugin:run
It fails with the error:
[INFO] [en2.ContainerRunMojo] Resolved container artifact
org.codehaus.cargo:cargo-core-container-tomcat:jar:1.7.6 for container
tomcat9x [WARNING] The defined deployable has the same groupId and
artifactId as your project's main artifact but the type is different.
You've defined a [war] type wher eas the project's packaging is [pom].
This is possibly an error and as a consequence the plugin will try to
find this deployable in the project's dependencies.
How can I make this work? I just want to launch the given WAR in an embedded tomcat9, from within Maven.
After trying many permutations, this finally worked for me:
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.7.9</version>
<configuration>
<container>
<systemProperties>
<myvar1>${myEnvVar}</myvar1>
<myvar2>... stuff ...</myvar2>
</systemProperties>
<containerId>tomcat9x</containerId>
<zipUrlInstaller>
<url>https://repo.maven.apache.org/maven2/org/apache/tomcat/tomcat/9.0.29/tomcat-9.0.29.zip</url>
</zipUrlInstaller>
</container>
<deployables>
<deployable>
<groupId>org.codehaus.cargo</groupId>
<artifactId>simple-war</artifactId>
<type>war</type>
<location>path/to/myapp.war</location>
<properties>
<context>myapp</context>
</properties>
</deployable>
</deployables>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
Use the failsafe plugin to automatically run the Integration Tests between the start and stop:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.21.0</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>

Issue Code Coverage for Maven MultiModule Project

Currently we are trying to get code coverage for multi-level module maven project . Below is the snippet of the same
parent
- child1
- sub-child1
- sub-child2
- child2
- sub-child1
- sub-child2
pom.xml
We have used the Jacoco plugin and tried generating the code coverage but it down't seem to be generating any jacoco executable file .
Any guidance over the same would be really helpful
Tried referring and executing the same
https://github.com/acntech/jacoco-multimodule-maven
<properties>
<jacoco.version>0.7.9</jacoco.version>
<sonar.jacoco.reportPaths>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
<sonar.language>java</sonar.language>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>agent-for-ut</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${sonar.jacoco.reportPaths}</destFile>
</configuration>
</execution>
<execution>
<id>agent-for-it</id>
<phase>package</phase>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<append>true</append>
<destFile>${sonar.jacoco.itReportPath}</destFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I expect the jacoco plugin to generate a single exe file and push the results to sonarqube

Maven and emma plugin

How to configure maven build to fail if some of bundles to instrument are misspelled or no target/coverage/coverage.xml file report is generated?
I did not check, try:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-verifier-plugin</artifactId>
<version>1.0</version>
<configuration>
<verificationFile>target/coverage/coverage.xml</verificationFile>
</configuration>
<executions>
<execution>
<id>main</id>
<phase>package</phase>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources