How to add JaCoCo in maven

若如初见. 提交于 2019-12-31 02:08:09

问题


I have written a unit test case using JUnit now I want to add JaCoCo in my build tool that is moving 3.2.1.I am new to Maven. While adding it, I have to doubt that I want to add it in the dependency or plugin ? There are both are available,such that is following

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

I desire to append it in the dependency is it enough for the plugin?

Please any body clarify it


回答1:


You need to add something like the below to your <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>

That should generate you coverage reports in target/site/jacoco when you build your project with i.e. mvn clean install site

Note in my example plugin configuration the COVEREDRATIO limit is very low, you might want to set a higher value like 80 or so. The idea is to let a build fail if coverage is below that limit.




回答2:


JaCoCo Java Code Coverage Library JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years. 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 rpository JaCoCo

  • Projects that use JaCoCo
  • Maven Surefire Plugin
  • Maven Release Plugin



回答3:


Here is a complete pom that will help you:

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

The report will be at this location: /target/site/jacoco/index.html

And see this location for all goals and their config params: http://www.eclemma.org/jacoco/trunk/doc/maven.html



来源:https://stackoverflow.com/questions/25483859/how-to-add-jacoco-in-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!