Setting javaagent for particular task in Gradle

8.9k views Asked by At

This is my run configuration.

task run << {
    jvmArgs "-javaagent:/home/audrius/org.springframework.instrument-3.0.5.RELEASE.jar"
    jettyRun.execute()
}

but it gives me:

Could not find method jvmArgs()

How do you set javaagent for jettyRun?

2

There are 2 answers

0
madhead On BEST ANSWER

Unfortunately, Gradle runs Jetty inside it's own JVM, so you can't set javaagent only for a specific task. It is set for the whole JVM. So, you have two ways to accomplish what you want: either you run Gradle itself with javaagent enabled, or you spawn another JVM process and run Jetty in it.

First solution is pretty easy: provide the option as you normally do. For example, put org.gradle.jvmargs = "-javaagent:/home/audrius/org.springframework.instrument-3.0.5.RELEASE.jar" in your gradle.properties

The second way is pretty hard. You can't just spawn new JVM and say "run this Gradle task" to it. I guess you'll need to use Gradle Tooling API to spawn new process based on your exising build config via GradleConnector:

task run << {
    ProjectConnection connection = GradleConnector.newConnector().forProjectDirectory(new File("someProjectFolder")).connect();

    try {
        BuildLauncher build = connection.newBuild();

        build.setJvmArguments("-javaagent:/home/audrius/org.springframework.instrument-3.0.5.RELEASE.jar")

        build.forTasks("jettyRun").run();
    } finally {
        connection.close();
    }
}

As you see, second solution is pretty ugly. I'd better choose first approach.

2
Robert Halter On

Try with.

task run (type:JavaExec) << { ...

Because the jvmArgs is only known in a JavaExec Task.