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

问题描述 投票:0回答:1

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

java spring-boot jacoco
1个回答
0
投票

默认情况下,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处找到完整的示例项目

© www.soinside.com 2019 - 2024. All rights reserved.