4

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
  • What do you want to do? Have code analyzed in sonar or you want jacoco create analysis reports? Commented Aug 25, 2014 at 10:52

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have four modules. So In which pom file I want to add it, I mean root module or sub modules? And If I add this plug in Root module then other pom files are showing error
As Jogesh_D suggests I would add the plugin to pluginManagement of your parent POM. You could create a parent POM with all your plugins and common dependencies and reuse it for all your Maven projects.
3

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

Comments

0

Add this 2 plugins to pom.xml file. Before adding these plugins make sure you add the dependencies "junit-jupiter-api" and "junit-jupiter-engine". You can find generated HTML file on target > site > jacoco > index.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>

Comments

-1

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

2 Comments

I have four modules. So In which pom file I want to add it, I mean root module or sub modules? And If I add this plug in Root module then other pom files are showing error...
If you want to run this for all sub modules add it in the root pom. If you only have a subset of your modules needing to run this then, have a look at plugin management maven.apache.org/pom.html#Plugin_Management

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.