How to configure Maven to run tests after it starts tomcat server? - java

When I run my maven project, it starts with the tests, and they all fail because tomcat server hasn't started yet and the war hasn't been deployed?
How can I configure maven to when testing to:
Start the server/application --> then run the tests --> then stop the server

You can use Tomcat Maven Plugin to run tomcat during build.
Try following configuration:
<build>
<plugins>
<!-- excludes tests that require application -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/TomcatPingTest.java</exclude>
</excludes>
</configuration>
</plugin>
<!-- starts tomcat before test execution and stops after-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>run-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>shutdown</goal>
</goals>
</execution>
</executions>
<configuration>
<fork>true</fork>
<port>5555</port>
<path>/app</path>
</configuration>
</plugin>
<!-- runs tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/TomcatPingTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>

Related

launch4j-generated exe starts slowly at first time

We are using launch4j-maven-plugin to generate an exe, used is Java 1.8.
The jar is generated with onejar because it contains jars as dependencies.
However the jar is starting immediately, only the exe needs 30s - 1m to start at first.
If I close the application after loaded completely and restart, then the application starts immediately. Maybe, because the dependencies of the jar are already loaded?!
I tried to generate with the maven-shade-plugin (only to check) and the same problem with the start occurs. Moreover I tried to start the application in a debugger and it also starts immediately. Starting the exe with --l4j-debug-all doesn't show anything.
Is there a possibility to speed-up?
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
</plugin>
<plugin>
<groupId>com.jolira</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<mainClass>${mainclass}</mainClass>
<attachToBuild>true</attachToBuild>
<filename>${project.artifactId}-${project.version}-jar-with-dependencies.${project.packaging}</filename>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.7.25</version>
<executions>
<execution>
<id>l4j-clui</id>
<phase>package</phase>
<goals>
<goal>launch4j</goal>
</goals>
<configuration>
<headerType>console</headerType>
<jar>${project.build.directory}/${project.artifactId}-${project.version}-jar-with-dependencies.${project.packaging}</jar>
<outfile>${project.build.directory}/App-${project.version}.exe</outfile>
<classPath>
<mainClass>com.simontuffs.onejar.Boot</mainClass>
<preCp>anything</preCp>
</classPath>
<jre>
<minVersion>1.8.0</minVersion>
<initialHeapSize>1024</initialHeapSize>
<maxHeapSize>1024</maxHeapSize>
</jre>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In my case, a simple reason: the Live-Antivirus-Scan in the background slowed it down.

How to use JPA-Entities as a dependency with maven

I got in a multimodule-maven-project an entities project in which there are some Entity and DAO classes. Also there are some integration-tests which I'd like to seperate in an own project called entities-IT.
Now I've put the entities-project as an dependency to the entites-IT-project but if I run the build there occurs an error message like this:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project entities-IT: Compilation failure
[ERROR] /some/path/to/project/entities-IT/src/it/java/com/example/persistence/dao/ExampleDaoIT.java:[25,38] cannot access com.example.persistence.ExampleEntity
[ERROR] bad class file: /some/path/to/project/persistence/ExampleEntity.class
[ERROR] illegal start of class file
[ERROR] Please remove or make sure it appears in the correct subdirectory of the classpath.
This is strange because in the target folder there are all of mentioned .class files and the folderstructure is also correct.
Is there anything to take notice if using Entities as a dependency in another project?
In the pom.xml of the entities-project there is some code-generation going on:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<version>2.2.4</version>
<executions>
<execution>
<id>process</id>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/metamodel</outputDirectory>
<compilerArguments>
-Aeclipselink.persistencexml=${project.basedir}/src/main/java/META-INF/persistence.xml
</compilerArguments>
<processors>
<processor>org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
</processor>
</processors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
and then I have the dependency set in the entities-IT-project:
<dependency>
<groupId>example</groupId>
<artifactId>entities</artifactId>
<version>${parent.version}</version>
</dependency>
and run the tests with the failsafe-plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/it/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

How to run surefire tests in parallel while forcing some to run sequentially?

I'm working on maven project with thousands of tests that run very slowly (takes 2h to run all the tests). So I tried to run the tests in parallel by configuring surefire plugin as follow:
<configuration>
<failIfNoTests>false</failIfNoTests>
<reuseForks>false</reuseForks>
<reuseForks>true</reuseForks>
<forkCount>2C</forkCount>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<parallel>suites</parallel>
<threadCount>12</threadCount>
</configuration>
But then some of the tests fail in their #Before and #After methods where we initialize and clean up some resources (it seems to be related to ports conflicts).
I tried to add this annotation #net.jcip.annotations.NotThreadSafe to the failing tests as described in surefire documentation to run them sequentially and avoid conflicts. However this didn't work and these tests still fail!!!
Any pointer on how to force some surefire tests to run sequentially on same JVM process and same thread while the rest will run in parallel (potentially on different JVM processes)?
EDIT 1
Now, I tried to split the configuration of surefire into two: one for sequential tests and another one for parallel tests. However this approach does not seem to enhance the execution time as I still have tests that fail and it still takes 2h to run all tests.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<!-- Sequential tests -->
<configuration>
<includes>**/*Sequential.java</includes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>false</reuseForks>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
<executions>
<!-- Parallel tests -->
<execution>
<id>parallel-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>**/*.java</includes>
<excludes>
<exclude>**/*Sequential.java</exclude>
</excludes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>true</reuseForks>
<forkCount>2C</forkCount>
<parallel>suites</parallel>
<threadCount>12</threadCount>
</configuration>
</execution>
</executions>
</plugin>
I'd go with your "split the configuration of surefire into two" approach.
However your configuration seems to be faulty because you only have one execution defined.
Might want to try something like this (define 2 executions with their own configurations):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<executions>
<!-- Sequential tests -->
<execution>
<id>sequential-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>**/*Sequential.java</includes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>false</reuseForks>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</execution>
<!-- Parallel tests -->
<execution>
<id>parallel-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>**/*.java</includes>
<excludes>
<exclude>**/*Sequential.java</exclude>
</excludes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>true</reuseForks>
<forkCount>2C</forkCount>
<parallel>suites</parallel>
<threadCount>12</threadCount>
</configuration>
</execution>
</executions>
</plugin>
Here is my final maven configuration for running the tests as wanted:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<!-- Sequential tests -->
<configuration>
<failIfNoTests>false</failIfNoTests>
<reuseForks>false</reuseForks>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
<executions>
<!-- Default tests -->
<execution>
<id>default-test</id>
<configuration>
<skip>true</skip>
</configuration>
</execution>
<!-- Parallel tests -->
<execution>
<id>parallel-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>**/*.java</includes>
<excludes>
<exclude>**/*Sequential.java</exclude>
</excludes>
<failIfNoTests>false</failIfNoTests>
<reuseForks>true</reuseForks>
<forkCount>2C</forkCount>
<parallel>suites</parallel>
<threadCount>12</threadCount>
</configuration>
</execution>
<!-- Sequential tests -->
<execution>
<id>sequential-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>${skip.sequential.tests}</skip>
<includes>**/*Sequential.java</includes>
<reuseForks>false</reuseForks>
</configuration>
</execution>
</executions>
</plugin>

How can I build two repackaged jars with Spring Boot

I'm trying to get spring-boot-maven-plugin to build two repackaged jars for two different main classes. I'added two execution blocks with their specific mainClass parameter in the configuration block but it seems that the plugin does not respect it because the configuration block is inside the execution block and not on plugin level and I always get
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.mystuff.tools.b4commandline.Application, com.mystuff.tools.loadtester.Application]
here's the plugin section of maven-spring-boot-maven plugin of the pom.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>${artifactId}</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.mystuff.tools.loadtester.Application</mainClass>
<finalName>${artifactId}</finalName>
</configuration>
</execution>
<execution>
<id>b4-commandline</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.mystuff.tools.b4commandline.Application</mainClass>
<finalName>b4-commandline</finalName>
</configuration>
</execution>
</executions>
</plugin>
Any help on this would be great. I saw recipes on stackoverflow for the maven-plugin but those do not apply to spring-boot.
You can do this with <classifier>. Example:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<executions>
<execution>
<id>Pack application 1</id>
<phase>package</phase>
<configuration>
<finalName>application</finalName>
<mainClass>com.test.Application1</mainClass>
<outputDirectory>target/application1</outputDirectory>
<classifier>1</classifier>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Pack application 2</id>
<phase>package</phase>
<configuration>
<finalName>application</finalName>
<mainClass>com.test.Application2</mainClass>
<outputDirectory>target/application2</outputDirectory>
<classifier>2</classifier>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
I could not make it work both within a single build (I'm not saying it is not possible though), however one option is to define 2 maven profiles:
<profiles>
<profile>
<id>one</id>
<activation>
<property>
<name>one</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.xyz.LauncherOne</mainClass>
<finalName>one</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>two</id>
<activation>
<property>
<name>two</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.xyz.LauncherTwo</mainClass>
<finalName>two</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then, build it twice using both profiles (activated with a property in my code sample):
$ mvn -Done clean install
$ mvn -Dtwo clean install
It creates: one.jar and two.jar
Assuming its doable, doing what you are asking would mean that the 2 jars produced would be identical except their manifest's main-class attribute. I would rather suggest you to package a single jar and use spring profiles to launch it:
$ java -jar -Dspring.profiles.active=profile1 YourApp.jar
$ java -jar -Dspring.profiles.active=profile2 YourApp.jar
This allows you to define 2 application-${profile}.properties, and #Conditional configuration classes, all based on the profile name.
It seems that you must consider first execution without id and another executions with id; Then it is working.
for me below working fine:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<!--<id>Batch 1(Don't put this one)</id>-->
<phase>package</phase>
<configuration>
<classifier>1</classifier>
<finalName>Core</finalName>
<mainClass>com.mainclass1</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Batch 2</id>
<phase>package</phase>
<configuration>
<classifier>2</classifier>
<finalName>BatchA</finalName>
<mainClass>com.mainclass2</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Batch 3</id>
<phase>package</phase>
<configuration>
<classifier>3</classifier>
<finalName>BatchB</finalName>
<mainClass>com.mainclass3</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

Tomcat7 Maven Plugin and JaCoCo

Is there any way to get code coverage using JaCoCo with the tomcat7-maven-plugin embedded instance?
The jacoco-maven-plugin is configured in my WAR's POM to instrument my unit tests, but I'm not sure how to attach the jacoco agent to the embedded Tomcat instance to instrument my integration tests that run against Tomcat. Given that the Tomcat instance is embedded, I'm not sure if this approach is possible. Is there any other way to accomplish this? I can probably switch from using the Tomcat Maven Plugin to using Cargo to get coverage, but I'd prefer to stick with the Tomcat plugin if possible.
Here are a few relevant snippets from my POM:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.14</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<systemProperties>
<!-- as expected, this system property doesn't work since Tomcat is embedded, but this is the type of config I'm looking for -->
<JAVA_OPTS>-javaagent:${project.build.directory}/${jacoco.jar}=destfile=${project.build.directory}/jacoco.exec,append=true</JAVA_OPTS>
</systemProperties>
</configuration>
<executions>
<execution>
<id>tomcat-startup</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
Versions: Maven 3.0.4, Tomcat Maven Plugin 2.1, Jacoco 0.6.2.201302030002, Java 7
I know its been awhile since the question was posted but I don't feel the answer really addressed the root of the problem. Code coverage may work with failsafe or surefire if you are running tests within those plugins. However, if you just want to monitor tomcat with jacoco to get a coverage report current information doesn't provide that. I found that the tomcat7-maven-plugin doesn't allow you to inject the -javaagent for jacoco by simply providing JAVA_OPTS. Switching to cargo I was able to do that like so.
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.2.201409121644</version>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<dataFile>${sonar.jacoco.reportPath}</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
<classDumpDir>${project.reporting.outputDirectory}/jacoco-it/classes</classDumpDir>
<skip>${skipITs}</skip>
</configuration>
<executions>
<execution>
<id>jacoco-agent</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<propertyName>jacoco.agent.itArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-report</id>
<phase>post-integration-test</phase>
<goals>
<goal>dump</goal>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.11</version>
<configuration>
<skip>${skipITs}</skip>
<container>
<containerId>tomcat7x</containerId>
<zipUrlInstaller>
<url>http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip
</url>
<downloadDir>${project.build.directory}/downloads</downloadDir>
<extractDir>${project.build.directory}/extracts</extractDir>
</zipUrlInstaller>
<dependencies>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc6</artifactId>
</dependency>
</dependencies>
</container>
<configuration>
<home>${project.build.directory}/catalina-base</home>
<properties>
<cargo.jvmargs>${jacoco.agent.itArgLine},output=tcpserver,port=6300 -Drunmode=TEST</cargo.jvmargs>
<cargo.servlet.port>9090</cargo.servlet.port>
</properties>
<configfiles>
<configfile>
<file>${basedir}/src/test/conf/context.xml</file>
<todir>conf/Catalina/localhost/</todir>
<tofile>context.xml.default</tofile>
</configfile>
</configfiles>
</configuration>
</configuration>
<executions>
<execution>
<id>start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-tomcat</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
the important parts are:
<plugin><groupId>org.jacoco</groupId> ...<propertyName>jacoco.agent.itArgLine</propertyName>
<cargo.jvmargs>${jacoco.agent.itArgLine},output=tcpserver,port=6300 </cargo.jvmargs>
When report target is run on the jacoco plugin it will create a directory in ${projectbase}/target/site/jacoco-it/index.html with your coverage report. I use this with the soapui-maven-plugin but it could be used with selenium-maven-plugin also.
You don't need pass JAVA_OPTS to tomcat embedded if you use maven-failsafe-plugin (or maven-surefire-plugin) to run yours integration test. It is because tomcat embedded run in the same process of maven-failsafe-plugin.
So when jacoco-maven-plugin execute prepare-agent it sets argLine that maven-failsafe-plugin uses too.
I created a project to test this, below part of pom:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.2.201302030002</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<includes>
<include>my.project.package.only.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>tomcat-startup</id>
<goals>
<goal>run-war-only</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<fork>true</fork>
</configuration>
</execution>
<execution>
<id>tomcat-shutdown</id>
<goals>
<goal>shutdown</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start-server</goal>
</goals>
<configuration>
<background>true</background>
<logOutput>true</logOutput>
<multiWindow>true</multiWindow>
</configuration>
</execution>
<execution>
<id>stop</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-server</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
you can try to take a look at a workaround at this post: http://dougonjava.blogspot.co.il/2013/07/integration-testing-using-maven-tomcat.html
I resolved problems when setting up JaCoCo agent with embedded Tomcat by instrumenting classes offline and then just placing JaCoCo agent on Tomcat classpath (plugin dependency) and adding file jacoco-agent.properties.
I put the working configuration on my blog:
http://burkond.blogspot.de/2014/05/selenium-in-sonar-code-coverage-metrics.html
I had the exact same problem and the only solution I found was to set the MAVEN_OPTS previously to the Maven build (for example on the command line or in the configuration of a Jenkins job):
export MAVEN_OPTS=-javaagent:~/.m2/repository/org/jacoco/org.jacoco.agent/0.7.4.201502262128/org.jacoco.agent-0.7.4.201502262128-runtime.jar=destfile=./target/jacoco.exec,append=true
This will attach the jacoco agent to the embedded tomcat instance, which will report back the coverage results into the given destfile.
First, it is important that the path to the jacoco runtime JAR is correct. You can manually download and refer to it or use another Maven command to download it into your local .m2 repository:
mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.jacoco:org.jacoco.agent:0.7.4.201502262128:jar:runtime
Second, make sure that the path to the jacoco.exec file is correct. In my case, I already have an existing jacoco.exec file in the target folder, which contains unit test results. The append=true makes sure that unit and integration tests are combined.
I managed to do it and it involves some tinkering with finicky stuff:
server/container needs to be on a separate jvm that can receive arguments (jacoco-agent). Cargo using embedded containers did not seem to work and was a pain to debug...
jvm with jacoco-it needs to stop before the jacoco analysis (duh!) but registering container-stop and jacoco-report on post-integration-test does not guarantee this... (the tcpdump, etc option in a previous answer had this problem)
defining random ports for the server/container makes this easy to integrate with continuous integration
phantomjs is an extra ;)
jacoco should be used as prepare-agent-integration and report-integration for integration-test (does not really make a difference)
Should be run as 'mvn clean verify'
Pom:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<executions>
<!-- unit test coverage -->
<execution>
<id>jacoco-pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<propertyName>jacoco.ut.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
<!-- integration test coverage -->
<execution>
<id>jacoco-pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<propertyName>jacoco.it.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report-integration</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>2.6</version>
</plugin>
<!-- Installs PhantomJS so it doesn't have to be pre-installed -->
<plugin>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>0.4</version>
<executions>
<execution>
<!-- should be post-integration-test ? -->
<phase>test</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.9.7</version>
</configuration>
</plugin>
<!-- Get two free ports for our test server to use -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<portNames>
<portName>jetty.port</portName>
<portName>jetty.port.stop</portName>
</portNames>
</configuration>
<executions>
<execution>
<id>reserve-port</id>
<phase>test</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Run tests (UT) -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>${jacoco.ut.argLine}</argLine>
<skipTests>${skip.unit.tests}</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<excludes>
<!-- no UT execution, to test only IT
<exclude>**/<remove this>*Test.java</exclude> -->
</excludes>
</configuration>
</plugin>
<!-- Use failsafe to run our integration tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.17</version>
<configuration>
<systemPropertyVariables>
<!-- pass these values to the test classes -->
<phantomjs.binary>${phantomjs.binary}</phantomjs.binary>
<jetty.port>${jetty.port}</jetty.port>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.4.16</version>
<configuration>
<skip>${skipITs}</skip>
<container>
<containerId>tomcat8x</containerId>
<zipUrlInstaller>
<!-- did not work with 'https'. Certificate problem? -->
<url>http://archive.apache.org/dist/tomcat/tomcat-8/v8.0.26/bin/apache-tomcat-8.0.26.zip</url>
<downloadDir>${project.build.directory}/downloads</downloadDir>
<extractDir>${project.build.directory}/extracts</extractDir>
</zipUrlInstaller>
</container>
<configuration>
<home>${project.build.directory}/catalina-base</home>
<properties>
<cargo.jvmargs>${jacoco.it.argLine}</cargo.jvmargs>
<cargo.servlet.port>${jetty.port}</cargo.servlet.port>
<!-- <cargo.logging>high</cargo.logging> -->
</properties>
</configuration>
</configuration>
<executions>
<execution>
<id>cargo-start-tomcat</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>cargo-stop-tomcat</id>
<phase>integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

Categories