How to generate Jacoco integration test report using offline instrumentation? - maven

Build tool: Maven
Reason to use offline instrumentation: Removing Powermock is not an option
Problem:
Both failsafe and surefire are run and reports are generated. However, jacoco.exec is generate but jacoco-it.exec is not. Apart from IT , offline instrumentation, coverage and reporting works fine.
This is the maven plugin configuration I use:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
To run the test i use maven clean install.
At the end of the test execution I get the following output:
[INFO]
[INFO] --- maven-failsafe-plugin:2.15:integration-test (integration-tests) # elune ---
[INFO] Skipping execution of surefire because it has already been run for this configuration
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:report (default-report) # elune ---
[INFO] Loading execution data file C:\Projects\elune\target\jacoco.exec
[INFO] Analyzed bundle 'elune' with 4 classes
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:report-integration (default-report-integration) # elune ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
Another possible pointer might be that the de-instrumentation of the classes happens after the unit tests are run but before the integration tests. But I don't know if this is right or wrong:
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:restore-instrumented-classes (default-restore-instrumented-classes) # elune ---
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) # elune ---
[INFO] Building jar: C:\Projects\elune\target\elune-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) # elune ---
[INFO]
[INFO] --- maven-failsafe-plugin:2.15:integration-test (default) # elune ---
[INFO] Failsafe report directory: C:\Projects\elune\target\failsafe-reports
Any idea why the jacoco-it.exec is not showing up?

I think the instrumentation of Integration Tests with the failsafe plugin are just not baked in. The restore-instrumented-classes goal defaults to the prepare-package phase which runs before the integration test phase: http://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html - so it may be enough to move that goal into the post-integration-test phase:
<execution>
<id>default-restore-instrumented-classes</id>
<phase>post-integration-test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
that may be enough. Otherwise you may be able to change the includes pattern of the surefire plugin to also include the integration tests (if that seems suitable in your case).

#rhinoceros.xn this is the plugin configuration that did the trick for me. But I use mvn clean install to run this
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<phase>post-integration-test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
</executions>
</plugin>

The jacoco-it.exec is not showing up after I set the phrase.
command line is:
mvn clean install -Djacoco.version=0.7.8 -DfailOnError=false -Dmaven.test.failure.ignore=true
I finally found the problem was that I didn't add configuration jacoco-agent.destfile in maven-failsafe-plugin.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.15</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco-it.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<id>integration-tests</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<phase>post-integration-test</phase>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

Related

How to make reserve-network-port work with maven spring project

I am trying to implement reserve-network-port with my maven spring project.
My POM
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-network-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<portNames>
<portName>tomcat.http.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.18</version>
<executions>
<execution>
<goals>
<goal>verify</goal>
</goals>
<configuration>
<systemProperyVariables>
<build.profile.url>${build.profile.url}</build.profile.url>
<test.server.port>${tomcat.http.port}</test.server.port>
</systemProperyVariables>
<skipTests>${skip.integration.tests}</skipTests>
</configuration>
</execution>
</executions>
</plugin>
${tomcat.http.port} is returning null for me.
I am also getting the prot no reserved when my maven build is started
[INFO] --- build-helper-maven-plugin:1.10:reserve-network-port (reserve-network-port) # account-service ---
[INFO] Reserved port 58140 for tomcat.http.port

Jacoco Report are not generated in the site

Jacoco reports are not generated when mvn builds the site.
Jacoco reports are generated in my target folder at target\site\jacoco , but somehow I am not able to see "Jacoco Tests" option under "Project Reports" when I try to build the jar site
The only thing I see in the log while generating the site is as below :
[INFO] <<< clirr-maven-plugin:2.6.1:clirr # data-binding <<<
[INFO] configuring report plugin com.cerner.engineering:maven-cerner-ml-dependency-plugin:1.1.1
[INFO] configuring report plugin org.jacoco:jacoco-maven-plugin:0.7.4.201502262128
[INFO] Skipping JaCoCo execution due to missing execution data file:C:\MSVCWorkspace\br-hla-april-data-binding\target\jacoco.exec
I am using the command mvn clean install site
This is how I have configure jacoco in my pom.xml in my build section :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<excludes>
<exclude>**/*Suite*.java</exclude>
</excludes>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<argLine>-Xms64m -Xmx512m -XX:MaxPermSize=256M -enableassertions -XX:+HeapDumpOnOutOfMemoryError ${jacoco.argLine}</argLine>
<junitArtifactName>junit:junit-dep</junitArtifactName>
</configuration>
</plugin>
Jacoco plugin is called like :
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jacoco.argLine</propertyName>
</configuration>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
I am not sure what is causing this issue , not to generate Jacoco reports on my site.
Although I am able to see the below in my logs
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.4.201502262128:prepare-agent (default-cli) # data-binding ---
[INFO] argLine set to -javaagent:C:\\_repository\\org\\jacoco\\org.jacoco.agent\\0.7.4.201502262128\\org.jacoco.agent-0.7.4.201502262128-runtime.jar=destfile=C:\\target\\coverage-reports\\jacoco-unit.exec
Thanks !!!
I was able to fix by calling plugin as below
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jacoco.argLine</propertyName>
</configuration>
</execution>
</executions>
</plugin>

Maven: ant-run plugin echo does not seem to execute

I have a maven project that is using the ant-run plugin. I added an echo message but it does not seem to execute:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>ast-application-deployment-kit</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="Starting ant-run target configuration."/>
But when I mvn clean package, I see this
[INFO] --- maven-antrun-plugin:1.3:run (my-app) # my-app ---
[INFO] Executing tasks
[INFO] Executed tasks
But I do not see the echo. What am I doing wrong?
Consider upgrading your plugin version to the latest (1.8):
This POM snippet prints echo:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<echo>NOW YOU CAN SEE ME!</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

All maven flyway plugins are executed with the last user

I have two oracle users and I am creating different schema for them. I mean each schema has different tables, types etc.
I wanted to create both schemas by flyway maven plugin, first I had two maven plugins, but then I tried also to have two separate profiles:
<profiles>
<profile>
<id>database</id>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<configuration>
<url>jdbc:oracle:thin:#devdb2:1521:ZOOMUTF</url>
<table>SCHEMA_UPDATES</table>
<outOfOrder>true</outOfOrder>
<locations>
<location>db/callrec</location>
</locations>
<user>cr_5199_mensik_mvn</user>
<password>callrec</password>
<serverId>callrec</serverId>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>database_wbsc</id>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.flyway</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<configuration>
<url>jdbc:oracle:thin:#devdb2:1521:ZOOMUTF</url>
<table>SCHEMA_UPDATES</table>
<outOfOrder>true</outOfOrder>
<locations>
<location>db/wbsc</location>
</locations>
<user>sc_5199_mensik_mvn</user>
<password>wbsc</password>
<serverId>wbsc</serverId>
</configuration>
<executions>
<execution>
<id>wbsc_compile</id>
<phase>compile</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
<execution>
<id>wbsc_clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then I execute:
mvn clean -Pdatabase,database_wbsc
But result is that only second profile is executed twice:
[INFO] [clean:clean {execution: default-clean}]
[INFO] [flyway:clean {execution: clean}]
[INFO] Cleaned schema "sc_5199_mensik_mvn" (execution time 00:00.023s)
[INFO] [flyway:clean {execution: wbsc_clean}]
[INFO] Cleaned schema "sc_5199_mensik_mvn" (execution time 00:00.018s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
If I switch order of profiles in xml (not in maven execution command) then the second user is used.
How can I execute both profiles but with their configuration?
try providing the configuration at the execution level rather than the plugin level.
<plugin>
<executions>
<execution>
<id>execution1</id>
<configuration>
</configuration>
</execution>
<execution>
<id>execution2</id>
<configuration>
</configuration>
</execution>
</executions>
</plugin>

Why does jetty server hangs after server start and doesn't run integration test?

I am running integration test using maven-jetty-plugin and maven-failsafe-plugin. Here's my configuration:
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.7.1</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.26</version>
<configuration>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>3600000</maxIdleTime>
</connector>
</connectors>
<contextPath>/</contextPath>
<scanIntervalSeconds>3</scanIntervalSeconds>
<scanTargetPatterns>
<scanTargetPattern>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>**/*.jsp</exclude>
<exclude>**/*.html</exclude>
</excludes>
<includes>
<include>**/*.page</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
When i run mvn clean install, jetty server starts and after that nothing happens (it gets stuck). Last line in my logs is : [INFO] Started Jetty Server. When i press control-c, here is what it prints:
2013-04-25 15:24:16.315:INFO::Shutdown hook executing
2013-04-25 15:24:16.317:INFO::Stopped SelectChannelConnector#0.0.0.0:8080
2013-04-25 15:24:16.821:INFO:/ca-app:Shutting down log4j
2013-04-25 15:24:16.821:INFO:/ca-app:Closing Spring root WebApplicationContext
2013-04-25 15:24:22.108:INFO::Shutdown hook complete[INFO]
Jetty server exiting.
[INFO]
[INFO] --- maven-failsafe-plugin:2.7.1:integration-test (default) # my-app ---
[INFO] Failsafe report directory: my-app/target/failsafe-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
There are no tests to run.
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-jetty-plugin:6.1.26:stop (stop-jetty) # my-app ---
[INFO]
[INFO] --- maven-failsafe-plugin:2.7.1:verify (default) # my-app ---
[INFO] Killing Jetty
[INFO] Failsafe report directory: my-app/target/failsafe-reports
[WARNING] File encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) # my-app ---
[INFO] Installing my-app/target/ca-app-0.1.5-SNAPSHOT.war to ~/.m2/....../my-app/0.1.5-SNAPSHOT/my-app-0.1.5-SNAPSHOT.war
[INFO] Installing my-app/pom.xml to ~/.m2/....../my-app/0.1.5-SNAPSHOT/my-app-0.1.5-SNAPSHOT.war
[INFO]
[INFO] --- maven-dependency-plugin:2.1:sources (install) # my-app ---
Why does it get stuck? When i press control-c, why does it perform rest of the steps? How can i fix it?
Try using start instead of run-war.
From javadoc on org\eclipse\jetty\jetty-maven-plugin\9.3.0.M2\jetty-maven-plugin-9.3.0.M2.jar!\org\eclipse\jetty\maven\plugin\AbstractJettyMojo.nonblocking
Determines whether or not the server blocks when started. The default behavior (false) will cause the server to pause other processes while it continues to handle web requests. This is useful when starting the server with the intent to work with it interactively. This is the behaviour of the jetty:run, jetty:run-war, jetty:run-war-exploded goals.
If true, the server will not block the execution of subsequent code. This is the behaviour of the jetty:start and default behaviour of the jetty:deploy goals.
So in order to start the jetty add run:
<goals>
<goal>stop</goal>
<goal>start</goal>
</goals>
And the whole:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.10.v20150310</version>
<configuration>
<war>webapp/target/MyWar.war</war>
<stopKey>fooKey</stopKey>
<stopPort>8081</stopPort>
<stopWait>1</stopWait>
<httpConnector>
<port>8888</port>
</httpConnector>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>stop</goal>
<goal>start</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<stopKey>STOP</stopKey>
<stopPort>8866</stopPort>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<stopKey>STOP</stopKey>
<stopPort>8866</stopPort>
</configuration>
</execution>
</executions>
</plugin>

Resources