gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:44:20 27 4
gpt4 key购买 nike

在集成测试期间,我使用 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>

现在我想将 JaCoCo 代理添加到执行中,但将其添加到配置中,因为代理或 jvmarguments 不起作用。开始时我总是看到:
[INFO] --- spring-boot-maven-plugin:2.2.4.RELEASE:start (start-spring-boot) @ tosca-ui ---
[INFO] Attaching agents: []

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

最佳答案

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

相反,您可以将 Jacoco 设置的 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>

由于必须配置相当多的 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>

这样,一旦您执行 mvn clean verify ,您就可以在 target/site/jacoco/index.html 中找到 Jacoco 报告。

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

关于java - 使用 JaCoCo 和 spring-boot-maven-plugin 生成代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60037013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com