简体   繁体   English

如何使用Boot Gradle插件在Spring Boot应用程序中嵌入JavaAgent

[英]How to embed a javaagent in spring boot app using boot gradle plugin

I am using a javaagent for my spring boot app and currently I am running it via 我正在为我的spring boot应用程序使用javaagent,目前正在通过

java -javaagent:agent.jar -jar app.jar

My project is a gradle project and I want to embed the agent.jar inside the app.jar so that I can run it as 我的项目是gradle项目,我想将agent.jar嵌入到app.jar中,这样我就可以像

java -javaagent:app.jar -jar app.jar

It can be done via boot maven plugin as mentioned here - https://jeroendruwe.be/spring-boot-and-new-relic/ but there is no alternative for boot gradle plugin. 它可以通过启动Maven插件来完成这里提到- https://jeroendruwe.be/spring-boot-and-new-relic/ ,但没有办法,对于引导gradle这个插件。 The closest I find is this - https://jdpgrailsdev.github.io/blog/2014/04/08/spring_boot_gradle_newrelic.html , but it does not embed the jar as intended. 我找到的最接近的是这个-https://jdpgrailsdev.github.io/blog/2014/04/08/spring_boot_gradle_newrelic.html ,但是它没有按预期的方式嵌入jar。

Is there anyway it can be done via gradle? 无论如何,可以通过gradle完成它吗?

Can be add as dependency jar from local directory. 可以从本地目录添加为依赖项jar。 'libs' is directory inside your project. “ libs”是项目内的目录。

dependencies { compile fileTree(dir: 'libs', include: '*.jar') } 依赖项{编译fileTree(dir:'libs',包括:'* .jar')}

gradle jar : 摇篮罐:

jar {
manifest{
    attributes "Agent-Class" : "com.......agent.AgentTest"
    //attributes "Premain-Class" : "com.......agent.AgentTest"
        }
    }

Althought, I am using to do like this : 虽然,我经常这样做:

    public static void main(String[] args) throws Exception {
    String pid = "6236";
    String agentPath = "...";
    VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid);
    virtualmachine.loadAgent("/home/aaa/Code/agent-1.0-SNAPSHOT.jar");
    virtualMachine.detach();
}

Did you try 你试过了吗

java -Xbootclasspath=('your path'/app.jar or 'your path to libs'/agent.jar) -javaagent:'your path'/app.jar -jar 'your path'/app.jar

?

Another approach: use jar as dependency and move (or create another one) your premain class in Spring Boot application jar. 另一种方法:使用jar作为依赖项,并在Spring Boot应用程序jar中移动(或创建另一个)您的premain类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM