jacoco多module场景文件合并

1. 背景描述

jacoco在通过命令生成报告时需要指定classfiles文件和sourcefiles文件。如果我们的springboot项目是一个多module项目,则需要将所有module的class文件和源代码文件合并到一个文件夹中,而且需要保留这些文件的目录结构。

java -jar jacococli.jar report jacoco-demo.exec --classfiles **\classes --sourcefiles **\source --html html-report --encoding=utf-8

本文通过maven的插件实现多module的class文件和java文件和合并

多module项目结构
module-demo
  ├── module-common                    # 第一个模块项目
  ├── module-web 					# 第二个模块项目
  ├── module-start					# 第三个模块项目,此模块有SpringBootApplication启动类,pom的dependencies中依赖了上面所有module

2. 操作步骤

在所有子模块中增加maven-source-plugin和spring-boot-maven-plugin插件,用于将子模块打包为源代码jar和编译后jar。配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.1.2</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打包后在对应的子模块的target文件夹中会生成两个jar包。

在module-starter模块中增加spring-boot-maven-plugin、maven-dependency-plugin和maven-resources-plugin插件,用于将所有module的class文件和java文件整合为制品中的classes文件夹和source文件夹。配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <!--聚合所有module的classes文件-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>classes-merge</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <!--把每个依赖的模块,都配置进来-->
                            <artifactItem>
                                <groupId>com.example</groupId>
                                <artifactId>module-common</artifactId>
                                <version>0.0.1-SNAPSHOT</version>
                                <type>jar</type>
                                <overWrite>false</overWrite>
                            </artifactItem>
                            <artifactItem>
                                <groupId>com.example</groupId>
                                <artifactId>module-web</artifactId>
                                <version>0.0.1-SNAPSHOT</version>
                                <type>jar</type>
                                <overWrite>false</overWrite>
                            </artifactItem>
                        </artifactItems>
                        <includes>**/*.class</includes>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--聚合所有module的java文件 前置操作需要其他module打源码包-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>source-merge</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <classifier>sources</classifier>
                        <failOnMissingClassifierArtifact>false</failOnMissingClassifierArtifact>
                        <includes>**/*.java</includes>
                        <includeArtifactIds>module-common,module-web,module-starter</includeArtifactIds>
                        <outputDirectory>${project.build.directory}/source</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!--copy当前module的源文件包到指定的source目录-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/source</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/java</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打包后在module-starter模块的target文件夹中会生成classes文件夹和source文件夹

3. 配置下说明

① <phase>标签:配置在maven的哪个生命周期执行
② <artifactItems>标签:这里需要将所有的子module(除了当前的module-starter)配置进来
③ <includes>标签:配置解压缩jar时要包含的文件,可以用英文逗号配置多种文件,这里jacoco只需要class文件和java文件。
④ <includeArtifactIds>标签:配置解压源文件jar时过滤的artifact,这里需要写入所有的子module的artifact,以英文逗号分割,支持com.*模糊匹配。若此项为空,则会将所有依赖的jar(包括jackson、spring相关jar)全部解压获取对应文件。类似的过滤标签还有<includeGroupIds>、<includeScope>、<excludeArtifactIds>、<excludeGroupIds>等。

消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息