Setting javaagent for particular task in Gradle
Asked Answered
H

2

8

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?

Hairtail answered 24/6, 2015 at 13:5 Comment(0)
B
7

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.

Biologist answered 1/7, 2015 at 13:57 Comment(0)
H
2

Try with.

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

Because the jvmArgs is only known in a JavaExec Task.

Herder answered 24/6, 2015 at 13:26 Comment(2)
It says: "No main class specified". must be jettyRun works different way.Hairtail
Did you See the Question. Sorry to only give a Link #17999562Herder

© 2022 - 2024 — McMap. All rights reserved.