Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting javaagent for particular task in Gradle

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?

like image 756
Maladec VampaYa Avatar asked Jun 24 '15 13:06

Maladec VampaYa


People also ask

What does Gradlew installDist do?

You can run gradle installDist to assemble the uncompressed distribution into $buildDir/install/${project.name} .

What does Gradle use to determine the order in which tasks can be run?

Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.

How does Gradle determine up to date?

Gradle will determine if a task is up to date by checking the inputs and outputs. For example, your compile task input is the source code. If the source code hasn't changed since the last compile, then it will check the output to make sure you haven't blown away your class files generated by the compiler.


2 Answers

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.

like image 192
madhead - StandWithUkraine Avatar answered Sep 19 '22 15:09

madhead - StandWithUkraine


Try with.

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

Because the jvmArgs is only known in a JavaExec Task.

like image 36
Robert Halter Avatar answered Sep 21 '22 15:09

Robert Halter