【发布时间】:2011-10-19 08:52:24
【问题描述】:
过去几天我一直在尝试使用 Maven 3.0.2 运行 Cobertura 2.4,但没有成功。我们有一个非常大的项目,其中包含许多模块(子项目)。 我发现文档基本上不存在或完全错误。我能找到的所有教程都不适用于 Maven 3.x(它们可以构建,但 Cobertura 要么不运行,要么无法生成报告)。
这里有人能够使它工作吗?任何有用的提示/示例? 谢谢。
【问题讨论】:
过去几天我一直在尝试使用 Maven 3.0.2 运行 Cobertura 2.4,但没有成功。我们有一个非常大的项目,其中包含许多模块(子项目)。 我发现文档基本上不存在或完全错误。我能找到的所有教程都不适用于 Maven 3.x(它们可以构建,但 Cobertura 要么不运行,要么无法生成报告)。
这里有人能够使它工作吗?任何有用的提示/示例? 谢谢。
【问题讨论】:
我通过添加以下内容成功地将 Cobertura 集成到我的项目中:
<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>
如果您运行mvn cobertura:cobertura,报告将在target\site\cobertura 中生成。另见maven cobertura plugin。
今天我用SonarQube分析项目。它有一个简单的installation 步骤(如果您对使用企业数据库不感兴趣),还包括代码覆盖率分析(使用JaCoCo)以及许多其他指标。
【讨论】:
mvn cobertura:cobertura 来生成它。到目前为止,您的回答是最好的。
在 maven 3.0.3 中(当你问这个问题时还没有出来),只需使用 maven 的站点插件并对其进行配置,使其使用 cobertura,如下所示:
<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>
....
【讨论】:
您还可以将 Cobertura 插件集成到您的 web 应用程序的 <reporting> 部分:
<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>
如果您运行mvn site,那么您的报告将在应用程序目标目录中的target/site/cobertura/index.html 中可用。
【讨论】: