Jacoco和Jetty Maven插件获得0%的覆盖率 [英] Jacoco and jetty maven plugin gets 0% coverage

查看:75
本文介绍了Jacoco和Jetty Maven插件获得0%的覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置jacoco以获得集成测试的内容. 我正在对码头进行集成测试(使用maven插件). 但是,即使我在启动码头服务器时在jam args中传递了代理,jacoco报告仍显示0%.这是我的pom.xml

I'm trying to setup jacoco to get the coverage for my integration tests. I'm running my integration tests against jetty (using the maven plugin). But even if i pass the agent in the jam args when starting up the jetty server the jacoco report shows 0%. Here is my 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代理,但是没有任何事情……

As you can see i'm running the jetty in fork mode and I do pass the jacoco agent in the params, but nothing...

我需要添加一些额外的东西吗?

Is there something extra I need to add?

推荐答案

阅读了帮助并回答了问题的人们有很多答案,而他们对使它真正发挥作用需要什么却一无所知.

There are lots answers out there from people that have read the help and replied without any idea of what really takes to make it work.

所以我们去了.

第一件事. Maven根据事物绑定到的阶段来运行事物,但是,如果您将两个事物绑定到同一阶段,则Maven将按照它们出现在POM文件中的顺序执行它们,因此在这种情况下,顺序很重要.

First things first. Maven run things based on the phases those things are bound to, however if you have two things bound to the same phase Maven will execute them in the order they appear on the POM file, so in this case order matters.

这是我的POM文件的样子:

Here is how my POM file looks like:

        <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配置.现在,让我们转到其中的重要细节:

That is an actual working POM.xml configuration. Now lets go to the important details on that:

  1. Jacoco插件必须继续进行Jetty插件,否则javaagent配置将没有属性
  2. 您必须运行Jetty分支,否则Jetty不会采用javaagent配置.
  3. 由于运行了Jetty fork,因此无法使用<webApp>...</webApp>定义上下文路径.您将需要一个context.xml文件.我正在将其中一个用于Jetty,将另一个用于Tomcat(生产).请参阅下面的内容.
  4. Jacoco关于集成测试的报告不能绑定到集成后测试阶段,否则您将在实际收集结果之前报告结果. (假设您希望Jacoco报告,如果您不跳过的话).因此,您必须将其绑定到下一个可用的阶段,即 verify .
  5. maxStartupLines 用于让Jetty知道在假定存在初始化错误之前,它可以忽略多少个初始化行.您可能需要对其进行自定义.
  1. Jacoco plugin must proceed Jetty plugin, otherwise you will have no property with the javaagent configuration
  2. You must run Jetty forked, otherwise Jetty wont take the javaagent configuration.
  3. Since you run Jetty forked you cant use <webApp>...</webApp> to define the context path. You will need a context.xml file for that. I'm using one for Jetty and another for Tomcat (production). See contents below.
  4. Jacoco reports on integration tests cannot be bound to post-integration-test phase, otherwise you will report results before actually collecting them. (Assuming you want Jacoco to report, if you don't skip this). So you have to bind it to the next available phase that would be verify.
  5. The maxStartupLines is used to let Jetty know how many initialization lines it may ignore before assuming there was an initialization error. You may need to customize that.

Context.xml(jetty_context.xml):

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:

In case you want to also report you code coverage to 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

If everything else is correct then you should be able to run: mvn clean install sonar:sonar

这篇关于Jacoco和Jetty Maven插件获得0%的覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆