Jacoco Jenkins插件工作,但maven报告显示0%的覆盖率

时间:2014-08-07 19:49:09

标签: maven code-coverage jacoco

我希望使用JaCoCo为基于Restassured的集成测试收集覆盖率指标,这些测试是针对在Maven 3中编排的所有内部JVM进程运行的。

我有以下设置:

  1. 预集成测试阶段执行java进程通过ant在它自己的进程中分叉,传递jacoco代理arg
  2. 集成后测试阶段exec是一个杀死该进程的pid的脚本
  3. jacoco的javaco-it.exec文件位于mvn运行结束的目标目录中,但html报告显示0%覆盖率(Sessions显示它收集的所有类。)
  4. 当使用Jacoco插件在Jenkins中运行时,那里的报告看起来很好。
  5. 我看到很多人在网上和网络上提出类似的问题,但大多数人都在使用Sonar,他们的解决方案似乎都是以声纳为中心的。我真的需要maven插件版本(sans-Sonar)才能工作。

    以下是我的pom.xml的相关部分:

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.1.201405082137</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-prepare-agent-integration</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>com.mydomain.*</include>
                            </includes>
                            <classDumpDir>${project.build.directory}/jacoco-it-classes</classDumpDir>
                            <propertyName>jacocoArgLine</propertyName>
                        </configuration>
                    </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>
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>METHOD</element>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>
                        <id>default-cli</id>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <tasks>
                                <java fork="true" classpathref="maven.runtime.classpath"
                                      className="com.mydomain.MainApp" spawn="true">
                                    <jvmarg value="${jacocoArgLine}"/>
                                </java>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.1</version>
                <executions>
                    <execution>
                        <id>kill-app</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>bash</executable>
                    <commandlineArgs>killApp.sh</commandlineArgs>
                </configuration>
            </plugin>
    

    通过“mvn clean install”运行时,我在输出中看到以下内容:

    [INFO] --- jacoco-maven-plugin:0.7.1.201405082137:report (default-report) @ integration-tests ---
    [INFO] Analyzed bundle 'integration-tests' with 0 classes
    [INFO]
    [INFO] --- jacoco-maven-plugin:0.7.1.201405082137:report-integration (default-report-integration) @ integration-tests ---
    [INFO] Analyzed bundle 'integration-tests' with 0 classes
    

    有一点需要注意:我必须使用classDumpDir,因为VM中存在jar依赖项,我希望覆盖统计信息不属于此Maven项目。是否有一些我在文档中没有看到的设置来告诉关于该类目录的报告目标?

1 个答案:

答案 0 :(得分:0)

我的解决方案:不要在maven外面运行jacoco。一起运行并配置如下的收集数据:

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.6.3.201306030806</version>
            <configuration>
                <includes>
                    <include>com/quantaconsultoria/**/*</include>
                </includes>
                <excludes>
                    <exclude>br/com/infotec/**/*</exclude>
                    <exclude>br/com/taxisimples/**/*</exclude>
                    <exclude>com/cocento/**/*</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>prepare-agent-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report-unit-test</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-unit-test</outputDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>report-integration-test</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

目标报告和阶段(验证和准备包)将收集所有数据并编写适当的报告。