You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用agent-embedder-maven-plugin为Spring Boot测试添加JavaAgent

用agent-embedder-maven-plugin统一处理Spring Boot部署与测试的LoadTimeWeaving需求

1. 统一依赖版本,避免硬编码

借助spring-boot-starter-parent的版本管理,直接引入spring-instrument依赖,无需手动指定版本:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <scope>provided</scope>
</dependency>

provided scope确保依赖不会被打包到最终Jar,仅用于agent嵌入流程。

2. 配置agent-embedder-maven-plugin,覆盖主构建与测试阶段

调整插件配置,让它在打包阶段嵌入agent到可执行Jar,同时在测试类处理阶段将agent注入测试类路径:

<plugin>
    <groupId>io.github.hakky54</groupId>
    <artifactId>agent-embedder-maven-plugin</artifactId>
    <version>1.1.0</version>
    <executions>
        <!-- 处理部署用可执行Jar -->
        <execution>
            <id>embed-agent-in-jar</id>
            <goals><goal>embed</goal></goals>
            <phase>package</phase>
            <configuration>
                <agents>
                    <agent>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-instrument</artifactId>
                        <version>${spring.version}</version>
                    </agent>
                </agents>
                <executable>true</executable>
            </configuration>
        </execution>
        <!-- 处理测试类路径 -->
        <execution>
            <id>embed-agent-for-tests</id>
            <goals><goal>embed</goal></goals>
            <phase>process-test-classes</phase>
            <configuration>
                <agents>
                    <agent>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-instrument</artifactId>
                        <version>${spring.version}</version>
                    </agent>
                </agents>
                <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

3. 清理maven-surefire-plugin配置

移除原有的-javaagent手动参数,确保surefire使用处理后的测试类环境:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>${surefire.version}</version>
    <configuration>
        <useSystemClassLoader>false</useSystemClassLoader>
        <forkCount>1</forkCount>
        <reuseForks>false</reuseForks>
    </configuration>
</plugin>

关闭系统类加载器复用、开启单进程测试,确保agent能被正确加载生效。

4. 确认LoadTimeWeaving Bean配置

保证Spring配置类中正确定义织入器:

@Configuration
@EnableLoadTimeWeaving
public class JpaConfiguration {

    @Bean
    public LoadTimeWeaver loadTimeWeaver() {
        return new InstrumentationLoadTimeWeaver();
    }

    // 你的EntityManagerFactory等JPA相关配置
}

核心原理

插件在process-test-classes阶段完成agent嵌入后,surefire启动测试进程时会自动从测试类路径加载agent,无需额外参数;打包阶段嵌入的agent则会随可执行Jar一同发布,解决部署脚本和IDE配置重复添加JavaAgent的问题。

内容的提问来源于stack exchange,提问作者SJ1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 21:03:27