如何在maven中添加JaCoCo

时间:2014-08-25 10:28:10

标签: java maven junit jacoco

我已经使用JUnit编写了一个单元测试用例现在我想在我的构建工具中添加JaCoCo 3.2.1。我是Maven的新手。在添加它时,我不得不怀疑我想在依赖项或插件中添加它吗?两者都可用,如下所示

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.2-SNAPSHOT</version>
</plugin>

<dependency>
   <groupId>org.codehaus.sonar.plugins</groupId>
   <artifactId>sonar-jacoco-plugin</artifactId>
   <version>3.2.1</version>
</dependency>

我希望将它附加到依赖项中,这对插件来说是否足够?

请任何机构澄清它

4 个答案:

答案 0 :(得分:2)

您需要在<build><plugins>

中添加以下内容
    <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-report</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>
            <execution>
                <id>default-check</id>
                <goals>
                    <goal>check</goal>
                </goals>
                <configuration>
                    <rules>
                        <rule>
                            <element>BUNDLE</element>
                            <limits>
                                <limit>
                                    <counter>COMPLEXITY</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>0.20</minimum>
                                </limit>
                            </limits>
                        </rule>
                    </rules>
                </configuration>
            </execution>
        </executions>   
    </plugin>

使用target/site/jacoco

构建项目时,应该在mvn clean install site生成覆盖率报告

请注意,在我的示例插件配置中,COVEREDRATIO限制非常低,您可能希望设置更高的值,如80左右。如果覆盖率低于该限制,那么想法是让构建失败。

答案 1 :(得分:2)

JaCoCo Java Code Coverage Library JaCoCo是一个免费的Java代码覆盖库,由EclEmma团队根据多年来使用和集成现有库的经验教训创建。 Example

<build>
  <plugins>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <version>0.7.9</version>
      <configuration>
        <skip>false</skip>
        <check/>
        <rules>
          <rule>
            <element>CLASS</element>
            <excludes>
                  <exclude>*Test</exclude>
            </excludes>
            <limits>
                  <limit>
                    <counter>LINE</counter>
                    <value>COVEREDRATIO</value>
                    <minimum>0.50</minimum>
                  </limit>
            </limits>
          </rule>
        </rules>
      </configuration>
      <executions>
        <execution>
          <id>prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>report</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>report</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/jacoco</outputDirectory>
          </configuration>
        </execution>
        <execution>
          <id>check</id>
          <goals>
            <goal>check</goal>
          </goals>
          <configuration>
            <check>
              <instructionRatio>100</instructionRatio>
              <branchRatio>95</branchRatio>
              <lineRatio>90</lineRatio>
              <methodRatio>90</methodRatio>
              <classRatio>90</classRatio>
            </check>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

GitHUb存储库JaCoCo

答案 2 :(得分:0)

将这 2 个插件添加到 pom.xml 文件中。在添加这些插件之前,请确保添加依赖项“junit-jupiter-api”和“junit-jupiter-engine”。您可以在 target > site > jacoco > index.html 上找到生成的 HTML 文件。

<plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.0.1</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-check</id>
                    <phase>test</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule>
                                <element>PACKAGE</element>
                                <limits>
                                    <limit>
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>

答案 3 :(得分:-1)

这是一个完整的pom,可以帮助你:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>dummyJar</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>dummyJar</name>
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
  </dependencies>
  <build>
  <plugins>
    <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-report</id>
                <phase>prepare-package</phase>
                <goals>
                    <goal>report</goal>
                </goals>
            </execution>            
        </executions>
    </plugin>
  </plugins>
  </build>
</project>

报告将在以下位置: /target/site/jacoco/index.html

查看所有目标及其配置参数的位置: http://www.eclemma.org/jacoco/trunk/doc/maven.html