简体   繁体   English

使用 JaCoCo 和 spring-boot-maven-plugin 生成代码覆盖率

[英]Generate Code Coverage with JaCoCo and spring-boot-maven-plugin

During integration tests I use spring-boot-maven-plugin to start my spring application:在集成测试期间,我使用 spring-boot-maven-plugin 来启动我的 spring 应用程序:

<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>

Now I would like to add JaCoCo agent to the exection but adding it to the configuration as agent or jvmarguments didn't work.现在我想将 JaCoCo 代理添加到执行中,但将其添加到配置中,因为代理或 jvmarguments 不起作用。 When starting I always see:开始时我总是看到:

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

How can I use JaCoCo with spring-boot-maven-plugin?如何将 JaCoCo 与 spring-boot-maven-plugin 一起使用?

By default Spring Boot Maven plugin creates a fork and the agent configuration has to be explicitly specified.默认情况下,Spring Boot Maven 插件会创建一个 fork,并且必须明确指定代理配置。 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.它可以通过在 Spring Boot Maven 插件中设置<agents><agent>...</agent></agents>配置属性来完成,但它不适用于 Jacoco,因为没有惯用的方法来找出确切的路径到代理 jar 文件。

Instead you can pass argLine variable set by Jacoco to Spring Boot Maven plugin JVM arguments:相反,您可以将argLine设置的argLine变量传递给 Spring Boot Maven 插件 JVM 参数:

<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>

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:由于必须配置相当多的 Maven 插件才能使其工作,为了避免混淆,这里是完整的pom.xml文件:

    <?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>

With this, once you execute mvn clean verify you can find Jacoco reports in target/site/jacoco/index.html .有了这个,一旦你执行mvn clean verify你可以在target/site/jacoco/index.html找到 Jacoco 报告。

You can find complete sample project at jacoco-spring-boot-maven-plugin-sample您可以在jacoco-spring-boot-maven-plugin-sample找到完整的示例项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM