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

从Jib迁移至spring-boot-maven-plugin后New Relic Agent加载失败求助

解决Spring Boot Maven Plugin构建镜像时New Relic Agent无法加载的问题

问题根源

你遇到的错误源于两个核心问题:

  1. 指定的-javaagent路径指向了ZIP压缩包,但JVM要求javaagent必须是可直接加载的JAR文件;
  2. newrelic-java依赖本身是ZIP包,默认配置下它被直接打包到BOOT-INF/lib目录,而非解压出内部的newrelic.jar。

解决方案

1. 调整依赖与解压配置

先确保newrelic-java不会被打包进应用JAR,同时正确解压出Agent的核心文件:

修正依赖声明

在<dependencies>节点中添加:

<dependency>
    <groupId>com.newrelic.agent.java</groupId>
    <artifactId>newrelic-java</artifactId>
    <version>8.3.0</version>
    <scope>provided</scope> <!-- 避免被打包到BOOT-INF/lib -->
</dependency>

调整Maven Dependency Plugin

修改解压配置,将ZIP包解压到单独目录(如target/newrelic):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>unpack-newrelic</id>
            <phase>package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <includeGroupIds>com.newrelic.agent.java</includeGroupIds>
                <includeArtifactIds>newrelic-java</includeArtifactIds>
                <outputDirectory>${project.build.directory}/newrelic</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
        </execution>
    </executions>
</plugin>

2. 配置Spring Boot Maven Plugin

将解压后的New Relic文件复制到镜像中,并设置正确的javaagent启动参数:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <image>
            <env>
                <BP_JVM_VERSION>17</BP_JVM_VERSION>
                <!-- 指定JAR文件作为javaagent,路径对应镜像内的位置 -->
                <JAVA_TOOL_OPTIONS>-javaagent:/app/newrelic/newrelic.jar</JAVA_TOOL_OPTIONS>
            </env>
            <!-- 把本地解压后的newrelic目录复制到镜像的/app/newrelic路径 -->
            <extraResources>
                <extraResource>
                    <directory>${project.build.directory}/newrelic</directory>
                    <targetPath>newrelic</targetPath>
                </extraResource>
                <!-- 如果有自定义newrelic.yml,添加此节点将配置文件同步到镜像 -->
                <extraResource>
                    <directory>src/main/resources/newrelic</directory>
                    <targetPath>newrelic</targetPath>
                </extraResource>
            </extraResources>
        </image>
    </configuration>
</plugin>

3. 验证配置

执行mvn clean package spring-boot:build-image构建镜像后:

  • 用dive工具检查镜像,确认/app/newrelic/newrelic.jar存在;
  • 容器启动时会自动读取JAVA_TOOL_OPTIONS中的参数加载Agent,无需手动修改启动命令。

关键注意点

  • Spring Boot Buildpack构建的镜像默认工作目录是/app,所以extraResources的targetPath会对应到该目录下的子路径;
  • newrelic.yml必须与newrelic.jar在同一目录,否则需通过-Dnewrelic.config.file指定配置文件路径。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:37:51