Gradle Distribution Plugin and javaagent

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?

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

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.

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.

It looks like I can see the default scripts here:

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?

I eventually did solve this and you can see my solution here:

In case someone needs to apply a java agent, feel free to use the plugin: