使用JaCoCo和spring-boot-maven-plugin生成代码覆盖率 [英] Generate Code Coverage with JaCoCo and spring-boot-maven-plugin

查看:153
本文介绍了使用JaCoCo和spring-boot-maven-plugin生成代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在集成测试期间,我使用spring-boot-maven-plugin启动我的spring应用程序:

During integration tests I use spring-boot-maven-plugin to start my spring application:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
         <execution>
             <id>start-spring-boot</id>
             <phase>pre-integration-test</phase>
             <goals>
                 <goal>start</goal>
             </goals>
         </execution>
         <execution>
             <id>stop-spring-boot</id>
             <phase>post-integration-test</phase>
             <goals>
                 <goal>stop</goal>
             </goals>
         </execution>
     </executions>
</plugin>

现在我想将JaCoCo代理添加到执行中,但是由于代理或jvmarguments无法将其添加到配置中.启动时,我总是看到:

Now I would like to add JaCoCo agent to the exection but adding it to the configuration as agent or jvmarguments didn't work. When starting I always see:

[INFO] --- spring-boot-maven-plugin:2.2.4.RELEASE:start (start-spring-boot) @ tosca-ui ---
[INFO] Attaching agents: []

如何将JaCoCo与spring-boot-maven-plugin一起使用?

How can I use JaCoCo with spring-boot-maven-plugin?

推荐答案

默认情况下,Spring Boot Maven插件创建一个fork,必须明确指定代理配置.可以通过在Spring Boot Maven插件中设置<agents><agent>...</agent></agents>配置属性来完成,但不适用于Jacoco,因为没有惯用的方法来找出代理jar文件的确切路径.

By default Spring Boot Maven plugin creates a fork and the agent configuration has to be explicitly specified. It can be done through setting <agents><agent>...</agent></agents> configuration property in Spring Boot Maven plugin BUT it does not work with Jacoco since there is no idiomatic way to find out what's the exact path to the agent jar file.

相反,您可以将Jacoco设置的argLine变量传递给Spring Boot Maven插件JVM参数:

Instead you can pass argLine variable set by Jacoco to Spring Boot Maven plugin JVM arguments:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>${argLine}</jvmArguments>
    </configuration>
    <executions>
        <execution>
            <id>start-spring-boot</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-spring-boot</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

由于必须配置许多Maven插件才能使其正常运行,为避免混淆,这里是完整的pom.xml文件:

Since quite a few Maven plugins have to be configured to make it work, just to avoid any confusion here's the complete pom.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jacoco-spring-boot-maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jacoco-spring-boot-maven</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>${argLine}</jvmArguments>
                </configuration>
                <executions>
                    <execution>
                        <id>start-spring-boot</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-spring-boot</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

通过此操作,一旦执行mvn clean verify,您就可以在target/site/jacoco/index.html中找到Jacoco报告.

With this, once you execute mvn clean verify you can find Jacoco reports in target/site/jacoco/index.html.

您可以在 jacoco-spring-boot中找到完整的示例项目-maven-plugin-sample

这篇关于使用JaCoCo和spring-boot-maven-plugin生成代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆