What is the proper way to use Cobertura with Maven 3.0.2

自古美人都是妖i 提交于 2019-11-30 06:06:14

I successfully integrated Cobertura in my projects with adding this:

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <instrumentation>
            <includes>
              <include>foo/bar/**/*.class</include>
            </includes>
          </instrumentation>
        </configuration>
    <executions>
      <execution>
        <id>clean</id>
        <phase>pre-site</phase>
        <goals>
          <goal>clean</goal>
        </goals>
      </execution>
      <execution>
        <id>instrument</id>
        <phase>site</phase>
        <goals>
          <goal>instrument</goal>
          <goal>cobertura</goal>
        </goals>
      </execution>
    </executions>
      </plugin>
    </plugins>
  </build>

  <reporting>
    <plugins>
      <plugin>
    <!-- use mvn cobertura:cobertura to generate cobertura reports -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.4</version>
    <configuration>
      <formats>
        <format>html</format>
        <format>xml</format>
      </formats>
     </configuration>
      </plugin>
    </plugins>
  </reporting>

If you run mvn cobertura:cobertura the reports will be generated in target\site\cobertura. See also maven cobertura plugin.


Today I analyze projects with SonarQube. It has an easy installation step (if you are not interested in using an enterprise database) and also includes a code coverage analysis (using JaCoCo) among many other metrics.

avandeursen

In maven 3.0.3 (not yet out when you asked the question), simply use maven's site plug-in and configure it such that it uses cobertura, as follows:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <reportPlugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>cobertura-maven-plugin</artifactId>
                        <version>2.5.1</version>
                        <configuration>
                            <formats>
                                <format>html</format>
                                <format>xml</format>
                            </formats>
                        </configuration>
                    </plugin>
                </reportPlugins>
            </configuration>
        </plugin>
....

You can also integrate the Cobertura plugin in the <reporting> section of your webapp:

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
    <plugins>
      <!-- Maven Project Info Reports Plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
        </configuration>
      </plugin>
      <!-- Cobertura Code Coverage Plugin -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <instrumentation>
            <ignoreTrivial>true</ignoreTrivial>
          </instrumentation>
          <formats>
            <format>html</format>
            <format>xml</format>
          </formats>
        </configuration>
      </plugin>
    </plugins>
  </reporting>

If you run mvn site, then your report will be available in target/site/cobertura/index.html within the target directory of your application.

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