Gradle Distribution Plugin and javaagent


(Micah Zoltu) #1

I want to make it so Gradle will cause a Java Agent to be attached to my process when it is launched. I want the solution to work with installDist, run, etc.

To attach a Java Agent you have to launch java with the -javaagent:path/to/agent.jar to the java command. Normally to add an option to the java command line I would add it via applicationDefaultJvmArgs = [ '-javaagent:path/to/agent.jar']. However, this results in needing to know the absolute path of the agent JAR at build time, or know the location of the agent.jar relative to the working directory at runtime.

Looking at the scripts generated by installDist, I can see that an $APP_HOME variable is set but it is set after the java options are processed, so I don’t believe I can use that as part of my agent path. Is there some other way to get access to $APP_HOME from a command line option?


(Benjamin Muschko) #2

Is your question rather related to the Application plugin? I see you are referring to applicationDefaultJvmArgs and scripts being generated.


(Micah Zoltu) #3

Yes, I believe this question is scoped to the Application plugin. The exact boundaries of the various built-in plugins has always been vague to me, so I am not certain.


(Benjamin Muschko) #4

If this is about the Application plugin, then it’s simple to customize the script templates. You just have to point the CreateStartScripts task to a different template. Have look at the Javadocs of the task for an example.


(Micah Zoltu) #5

It looks like I can see the default scripts here: https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/

How can I go about referencing that script from within my plugin? I know I could just copy/paste that script into my plugin, but I would like to leverage patches/changes to that script (with gradle version updates) from within my plugin by deriving from the default script.

Also, I don’t believe those scripts are used for the run task, so how do I change how the run task works?


(Micah Zoltu) #6

I eventually did solve this and you can see my solution here: https://github.com/Zoltu/application-agent-gradle-plugin/blob/master/src/main/groovy/com.zoltu/ApplicationAgent.groovy

In case someone needs to apply a java agent, feel free to use the plugin: https://plugins.gradle.org/plugin/com.zoltu.application-agent