spring boot 通常会打成一个fatjar的方式启动,如果我们本地启动(增加agent)则是这样子:
java -javaagent:/path/myAgent.jar -jar myFat.jar
但是部署到生产环境,绝对路径是会变的,所以两种方案:
和fatjar放在同一目录下,/target
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.demo.Application</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.demo</groupId>
<artifactId>myAgent</artifactId>
<version>x.x.x</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
- 指定spring boot maven plugin的mainClass属性
- 使用maven copy 插件,将 myAgent.jar 复制到target目录
- docker脚本
COPY ./ztarget/springBootApp.jar /path/
COPY ./target/myAgent.jar /path/
- 服务器上可执行脚本
java -javaagent:myAgent.jar -jar myFat.jar
将agent打包至fatJar中
参考: Including Java Agent in Standalone Spring Boot Application
效果:java -javaagent:myFat.jar -jar myFat.jar