Tomcat7 Maven and JaCoCo plugin

Is there a way to get code coverage using JaCoCo with the tomcat7-maven-plugin inline instance?

The jacoco-maven plugin is configured in my WAR POM to test my unit tests, but I'm not sure how to connect the jacoco agent to the built-in Tomcat instance to test my integration tests that run against Tomcat. Given that the Tomcat instance is built-in, I'm not sure if this approach is possible. Is there any other way to do this? Perhaps I can switch from using the Tomcat Maven Plugin module to using Cargo to get coverage, but I would prefer to stick with the Tomcat plugin if possible.

Here are some 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>

: Maven 3.0.4, Tomcat Maven Plugin 2.1, Jacoco 0.6.2.201302030002, Java 7

+16
6

, , , , . . , tomcat jacoco, , . , Tomcat7-maven-plugin javaagent jacoco, JAVA_OPTS. , .

<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>

:

  • <plugin><groupId>org.jacoco</groupId> ...<propertyName>jacoco.agent.itArgLine</propertyName>
  • <cargo.jvmargs>${jacoco.agent.itArgLine},output=tcpserver,port=6300 </cargo.jvmargs>

jacoco-, ${projectbase}/target/site/jacoco-it/index.html . soapui-maven, selenium-maven-plugin.

+7

JAVA_OPTS tomcat embedded, maven-failafe-plugin ( maven-surefire-plugin) . , tomcat maven-failafe-plugin.

, jacoco-maven-plugin prepare-agent, argLine, maven-failafe-plugin .

, 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> 
+1

JaCoCo Tomcat, , JaCoCo Tomcat ( ) jacoco-agent.properties.

:

http://burkond.blogspot.de/2014/05/selenium-in-sonar-code-coverage-metrics.html

0

, , , MAVEN_OPTS Maven (, Jenkins):

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

jacoco tomcat, destfile.

-, , JAR- jacoco . Maven .m2:

mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:get -Dartifact=org.jacoco:org.jacoco.agent:0.7.4.201502262128:jar:runtime

-, , jacoco.exec . jacoco.exec , unit test. append=true .

0

, :

  • / jvm, (jacoco-agent). , , , ...
  • jvm jacoco - jacoco (duh!), - jacoco-report - ... ( tcpdump .. )
  • /
  • phantomjs ;)
  • jacoco -- - ( )

'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>
0
source

Source: https://habr.com/ru/post/1648613/


All Articles