Java Jacoco Maven 多模块项目覆盖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33078745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jacoco Maven multi module project coverage
提问by Reddy
Seems like there are couple of questions, which are quite old and things changed from Java 8 support of Jacoco.
似乎有几个问题,这些问题已经很老了,而且 Java 8 对 Jacoco 的支持发生了变化。
My Project contains following structure
我的项目包含以下结构
pom.xml
|
|
-----sub module A pom.xml
|
|
-----sub module B pom.xml
|
|
-----sub module C pom.xml
I have configured the main pom
like this
我已经配置了main pom
这样的
Main POM.xml
主 POM.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>jacoco-multi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>projectA</module>
<module>projectB</module>
</modules>
<properties>
<jacoco.version>0.5.7.201204190339</jacoco.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.10</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.5.201505241946</version>
<configuration>
<destFile>${project.basedir}/../target/jacoco.exec</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
<executions>
<execution>
<id>default-integration-test</id>
<goals>
<goal>integration-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
</plugins>
</build>
</project>
A Pom.xml
一个 Pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>jacoco-multi</artifactId>
<groupId>com.test</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>projectA</artifactId>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
B pom.xml
B pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>jacoco-multi</artifactId>
<groupId>com.test</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>projectB</artifactId>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>projectA</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
</plugin>
</plugins>
</pluginManagement>
</build>
I am executing this command mvn clean package
. I can see jacoco.exec is getting generated, however I am not able to see any HTML reports are being to verify the data.
我正在执行这个命令mvn clean package
。我可以看到 jacoco.exec 正在生成,但是我看不到任何 HTML 报告正在验证数据。
Please help me with this.
Another point is, my configuration is correct for Multi-Module
projects?
请帮我解决一下这个。还有一点是,我的配置对Multi-Module
项目是否正确?
Update
更新
Identified issue.
<destFile>${project.basedir}/../target/jacoco.exec</destFile>
changed to
<destFile>${project.basedir}/target/jacoco.exec</destFile>
确定的问题。
<destFile>${project.basedir}/../target/jacoco.exec</destFile>
变成
<destFile>${project.basedir}/target/jacoco.exec</destFile>
Now it's generating reports for individual modules. Any idea how to generate consolidated report
现在它正在为各个模块生成报告。 知道如何生成合并报告
采纳答案by Rogério
JaCoCo version 0.7.7 can generate an aggregate coverage reportfrom multiple Maven modules through a new goal jacoco:report-aggregate
.
JaCoCo 0.7.7 版可以通过新目标从多个 Maven 模块生成聚合覆盖率报告jacoco:report-aggregate
。
回答by barryku
Copy the code end of article at https://dzone.com/articles/jacoco-maven-multi-moduleand put it in .... works for me. Just add as many modules as you would like to report on. Also I needed to change antrun from 1.7 to 1.8 for the ant tasks to run.
在https://dzone.com/articles/jacoco-maven-multi-module复制文章的代码结尾并将其放入 .... 对我有用。只需添加您想要报告的尽可能多的模块。此外,我需要将 antrun 从 1.7 更改为 1.8 才能运行 ant 任务。
回答by Michael Remme
One problem in multimodule projects is caused, if the aggregator pom is used as parent pom for the modules either, like it is the case in the above example:
如果聚合器 pom 被用作模块的父 pom,则会导致多模块项目中的一个问题,就像上面的例子一样:
- parentAggregator pom
---sub module A pom.xml -> parentAggregator pom
---sub module B pom.xml -> parentAggregator pom
---sub module C pom.xml -> parentAggregator pom
In this case, the build order is:
在这种情况下,构建顺序是:
- parentAggregator
- sub module A
- sub module B
- sub module C
which means, that the parent aggregator can not collect complete information. In my case a transfer of data into sonarQube by mvn sonar:sonar resulted in unexpected and uncomplete results.
这意味着,父聚合器无法收集完整的信息。在我的情况下,通过 mvn sonar:sonar 将数据传输到 sonarQube 导致了意外和不完整的结果。
Changing the module structure to:
将模块结构更改为:
- aggregator pom
-- parent pom
---sub module A pom.xml -> parent pom
---sub module B pom.xml -> parent pom
---sub module C pom.xml -> parent pom
will change the build order to:
将构建顺序更改为:
- parent
- sub module A
- sub module B
- sub module C
- aggregator
In this case aggregator will be the last one and work with the results of the modules. In my case the results in SonarQube were like expected.
在这种情况下,聚合器将是最后一个并处理模块的结果。就我而言,SonarQube 中的结果与预期的一样。
回答by tm1701
After scannning many solutions I created a simple but complete Jacocodemo project showing:
在扫描了许多解决方案后,我创建了一个简单但完整的Jacoco演示项目,显示:
- Multi module project
- Unit test (via mvn clean install)
- Integration test (via mvn clean install -P integration-test)
- Jacoco - test coverage ( both aggregate datafile and aggregate reporting)
- FindBugs - code quality
- 多模块项目
- 单元测试(通过 mvn clean install)
- 集成测试(通过 mvn clean install -P 集成测试)
- Jacoco - 测试覆盖率(聚合数据文件和聚合报告)
- FindBugs - 代码质量
Enjoy the simple demo project.
享受简单的演示项目。