• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

acntech/jacoco-multimodule-maven

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

acntech/jacoco-multimodule-maven

开源软件地址(OpenSource Url):

https://github.com/acntech/jacoco-multimodule-maven

开源编程语言(OpenSource Language):

Java 100.0%

开源软件介绍(OpenSource Introduction):

Jacoco coverage in multi-module maven projects

Simple dummy-project that demonstrates how to configure the jacoco code coverage library in a multi-module maven project with integration-tests in order to get a complete coverage-report.

Problem

One of the problems with jacoco and maven is that coverage is only reported for code in the module in which the tests are located and not for the entire project as a whole. This means that the coverage-percent will be under-reported in many cases.

Solution

In this example class A in module-a calls class B in module-B which calls class C in module-c. All the tests are located in module-a. With standard configuration class A is the only class that will get coverage in the jacoco report.

By using report-appending in jacoco we can fix this and get coverage for all the classes in the whole project and not just for the classes in the module where the tests resides.

Root pom.xml

Properties:

<properties>
    <jacoco.version>0.7.9</jacoco.version>
    <sonar.jacoco.reportPaths>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPaths>
    <sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
    <sonar.language>java</sonar.language>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
</properties>

Configuration of the jacoco maven plugin:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
        <!-- Unit-tests -->
        <execution>
            <id>agent-for-ut</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.reportPaths}</destFile>
            </configuration>
        </execution>
        <!-- Integration-tests executed with failsafe-plugin -->
        <execution>
            <id>agent-for-it</id>
            <phase>package</phase>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.itReportPath}</destFile>
            </configuration>
        </execution>
    </executions>
</plugin>
Nested submodules

No further configuration is required if you only have one level of submodules. In our example we have two levels of submodules for module-c.

root
 - module-a
 - module-b
 - module-c
     - module-c1

To get nested submodules to append to the report we have to override the jacoco properties we specified in the root pom:

<sonar.jacoco.reportPaths>${project.basedir}/../../target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.jacoco.itReportPath>${project.basedir}/../../target/jacoco-it.exec</sonar.jacoco.itReportPath>

Notice that we need to traverse up to the folder of the root module in the file path.

Testing

Run sonar in a docker container docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube

Build the project with integration-tests and and publish to sonar mvn clean install failsafe:integration-test sonar:sonar

You should now have jacoco.exec and jacoco-it.exec in the target-folder of the root-module.

Open localhost:9000 and verify that the coverage is 100%.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
tesla/m2eclipse-webby: Web Application Runner for Maven WAR Projects发布时间:2022-08-17
下一篇:
mojohaus/animal-sniffer发布时间:2022-08-17
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap