我正在尝试设置jacoco以获得我的集成测试的覆盖范围。 我正在运行针对jetty的集成测试(使用maven插件)。 但即使我在启动jetty服务器时通过jam args中的代理,jacoco报告显示0%。这是我的pom.xml
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-forked</goal>
</goals>
<configuration>
<waitForChild>false</waitForChild>
<jvmArgs>-Denv=it -Djetty.port=8081 ${failsafeArgLine}</jvmArgs>
<webApp>
<contextPath>/myContext</contextPath>
</webApp>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<stopPort>8082</stopPort>
<stopKey>test</stopKey>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<argLine>${failsafeArgLine}</argLine>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<propertyName>failsafeArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>post-integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
正如你所看到的那样我在叉子模式下运行码头而且我确实通过了参数中的jacoco代理,但没有...
我需要添加一些额外的东西吗?
答案 0 :(得分:4)
那些阅读过帮助的人有很多答案,并且在不知道如何使其发挥作用的情况下回复。
我们去了......
首先要做的事情。 Maven基于这些事物所绑定的阶段运行事物,但是如果你有两个事物绑定到同一个阶段,Maven将按照它们在POM文件中出现的顺序执行它们,所以在这种情况下,顺序很重要。
以下是我的POM文件的样子:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<argLine>${surefireArgLine}</argLine>
<skipTests>${skip.unit.tests}</skipTests>
<excludes>
<exclude>**/*TestSuite*</exclude>
<exclude>**/*ITest*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<configuration>
<skipTests>${skip.integration.tests}</skipTests>
<includes>
<include>**/*TestSuite*</include>
<include>**/*ITest*</include>
</includes>
<systemPropertyVariables>
... //Whatever you need to configure on your integration test, if any.
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<excludes>
<exclude>**/*Test*</exclude>
</excludes>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<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>
<execution>
<id>pre-integration-test</id>
<phase>pre-integration-test</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<excludes>
<exclude>**/*Test*</exclude>
</excludes>
<propertyName>jacoco.agent.itArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-integration-test</id>
<phase>verify</phase>
<goals>
<goal>report</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.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.10.v20150310</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
<httpConnector>
<port>${it.server.port}</port>
</httpConnector>
<contextXml>jetty_context.xml</contextXml>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-forked</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<waitForChild>false</waitForChild>
<maxStartupLines>200</maxStartupLines>
<jvmArgs>${jacoco.agent.itArgLine} -Djetty.port=${it.server.port}</jvmArgs>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
这是一个实际工作的POM.xml配置。现在让我们来看看有关的重要细节:
<webApp>...</webApp>
来定义上下文路径。您将需要一个context.xml文件。我正在使用一个用于Jetty而另一个用于Tomcat(生产)。见下面的内容。Context.xml(jetty_context.xml):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/myApp</Set>
<Set name="extraClasspath">comma-separated list of additional classpaths, such as resources</Set>
</Configure>
如果您还要向SonarQube报告代码覆盖率:
<properties>
<sonar.jacoco.reportPath>${project.build.directory}/coverage-reports/jacoco-ut.exec</sonar.jacoco.reportPath>
<sonar.jacoco.itReportPath>${project.build.directory}/coverage-reports/jacoco-it.exec</sonar.jacoco.itReportPath>
</properties>
如果其他一切都正确,那么你应该能够运行:
mvn clean install sonar:sonar
答案 1 :(得分:1)
我做了一些工作,在过去几天自己设置了这个,并在这里有一个有效的例子:
https://github.com/daniellundmark/jetty-jacoco-example
也许这可以帮到你。
运行上面的示例时,我收到此错误:
[INFO] Forked process starting
[INFO] Forked process startup errors
通过更改为准备代理集成,报告集成以及在pom文件中的jetty插件之前移动插件,我的示例正常工作(有一些记录的覆盖率)。
运行mvn -X预集成测试以查看发生的情况非常有用。但是看看上面的例子。希望这可以帮到你。
以下是我在更改示例时在pom.xml文件中最终得到的插件:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-forked</goal>
</goals>
<configuration>
<waitForChild>false</waitForChild>
<jvmArgs>${failsafeArgLine} -Denv=it -Djetty.port=8081</jvmArgs>
<webApp>
<contextPath>/myContext</contextPath>
</webApp>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
<configuration>
<stopPort>8082</stopPort>
<stopKey>test</stopKey>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<argLine>${failsafeArgLine}</argLine>
</configuration>
</execution>
</executions>
</plugin>