Gradle JavaExec Task Type: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(33 intermediate revisions by the same user not shown)
Line 5: Line 5:


=Internal=
=Internal=
* [[Gradle_Java_Plugin#jar_Task|Java Plugin]]
* [[Gradle_Task#Java_Plugin_Predefined_Task_Types|Gradle Tasks]]
* [[Gradle_Task#Java_Plugin_Predefined_Task_Types|Gradle Tasks]]
* [[Gradle_Java_Plugin#Java_Plugin_Predefined_Task_Types|Java Plugin]]
 
=Overview=
=Overview=
Executes a Java application as a child process. To get details on the actual java command line, execute Gradle with [[Gradle_Command_Line#-i.2C--info|-i command line option]].
Executes a Java application as a child process. To get details on the actual java command line, execute Gradle with [[Gradle_Command_Line#-i.2C--info|-i command line option]]. While the execution task name can be arbitrary, "run" is usually a good choice.
 
<syntaxhighlight lang='groovy'>
task someTask(type: JavaExec) {
    classpath = configurations.runtimeClasspath
    main = 'myPackage.MyMainClass'
    args = ['something', 'somethingelse']
    jvmArgs = ['-Xmx12G', '-Xms1G']
    environment "LD_LIBRARY_PATH", "/usr/local/lib"
    systemProperties = [ "some.prop": "some.value", "some.other.prop": "some.other.value" ]
}
</syntaxhighlight>


=Debugging Code Run with JavaExec=
<syntaxhighlight lang='groovy'>
<syntaxhighlight lang='groovy'>
task codeGen(type: JavaExec) {
task someTask(type: JavaExec) {
    jvmArgs = ['-Xmx12g']
     classpath = configurations.runtimeClasspath
     classpath = configurations.runtimeClasspath
     main = 'myPackage.MyMainClass'
     main = 'myPackage.MyMainClass'
     args 'something somethingelse'
     args = ['something', 'somethingelse']
    jvmArgs = ['-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005']
}
</syntaxhighlight>
The above configuration will start the JVM executing 'myPackage.MyMainClass' in debug mode and suspend waiting for a connection on 5005. For a process model, see {{Internal|Gradle_Process_Model#JavaExec|Gradle Process Model &#124; Daemon &#124; JavaExec}}
 
=Configuration=
==<tt>classpath</tt>==
<font color=darkgray>TODO</font>
 
<syntaxhighlight lang='groovy'>
classpath = sourceSets.main.runtimeClasspath
</syntaxhighlight>
<syntaxhighlight lang='groovy'>
classpath = configurations.runtimeClasspath
</syntaxhighlight>
<syntaxhighlight lang='groovy'>
classpath = configurations.compileClasspath
</syntaxhighlight>
 
==<tt>main</tt>==
The main class. Must be available on classpath.
<syntaxhighlight lang='groovy'>
main = 'playground.javaExec.Main'
</syntaxhighlight>
==<tt>args</tt>==
"args" can be provided as a single string, but in this case, even if individual tokens are separated by space, the main() method will get just one single string as argument:
<syntaxhighlight lang='groovy'>
args 'A B C'
</syntaxhighlight>
This will be passed to the Java main() method as args[0]:
<syntaxhighlight lang='text'>
arguments:
  args[0]: X Y Z
</syntaxhighlight>
 
To provide individual tokens, use this syntax:
<syntaxhighlight lang='groovy'>
args = ['A', 'B', 'C']
</syntaxhighlight>
The list elements will be passed to the Java main() method as individual arguments:
<syntaxhighlight lang='text'>
arguments:
  args[0]: A
  args[1]: B
  args[2]: C
</syntaxhighlight>


==<tt>jvmArgs</tt>==
<syntaxhighlight lang='groovy'>
jvmArgs = ['-Xmx12g']
</syntaxhighlight>
==<tt>maxHeapSize</tt>==
==<tt>environment</tt>==
Specifies a single environment variable, as a key value pair separated by comma. Multiple variable can be specified that way:
<syntaxhighlight lang='groovy'>
environment "SOME_ENV_VAR", "some value"
environment "SOME_OTHER_ENV_VAR", "some other value"
</syntaxhighlight>
==<tt>systemProperties</tt>==
A [[Groovy#Map|Groovy map]].
<syntaxhighlight lang='groovy'>
systemProperties = [ "some.prop": "some.value", "some.other.prop": "some.other.value" ]
</syntaxhighlight>
==<tt>group</tt>==
==<tt>description</tt>==
==<tt>standardOutput</tt>==
<syntaxhighlight lang='groovy'>
task someTask(type: JavaExec) {
  ...
  standardOutput = System.out
  ...
}
</syntaxhighlight>
==<tt>errorOutput</tt>==
<syntaxhighlight lang='groovy'>
task someTask(type: JavaExec) {
  ...
  errorOutput = System.err
  ...
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 19:24, 13 July 2021

External

Internal

Overview

Executes a Java application as a child process. To get details on the actual java command line, execute Gradle with -i command line option. While the execution task name can be arbitrary, "run" is usually a good choice.

task someTask(type: JavaExec) {
    classpath = configurations.runtimeClasspath
    main = 'myPackage.MyMainClass'
    args = ['something', 'somethingelse']
    jvmArgs = ['-Xmx12G', '-Xms1G']
    environment "LD_LIBRARY_PATH", "/usr/local/lib"
    systemProperties = [ "some.prop": "some.value", "some.other.prop": "some.other.value" ]
}

Debugging Code Run with JavaExec

task someTask(type: JavaExec) {
    classpath = configurations.runtimeClasspath
    main = 'myPackage.MyMainClass'
    args = ['something', 'somethingelse']
    jvmArgs = ['-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005']
}

The above configuration will start the JVM executing 'myPackage.MyMainClass' in debug mode and suspend waiting for a connection on 5005. For a process model, see

Gradle Process Model | Daemon | JavaExec

Configuration

classpath

TODO

classpath = sourceSets.main.runtimeClasspath
classpath = configurations.runtimeClasspath
classpath = configurations.compileClasspath

main

The main class. Must be available on classpath.

main = 'playground.javaExec.Main'

args

"args" can be provided as a single string, but in this case, even if individual tokens are separated by space, the main() method will get just one single string as argument:

args 'A B C'

This will be passed to the Java main() method as args[0]:

arguments:
  args[0]: X Y Z

To provide individual tokens, use this syntax:

args = ['A', 'B', 'C']

The list elements will be passed to the Java main() method as individual arguments:

arguments:
  args[0]: A
  args[1]: B
  args[2]: C

jvmArgs

jvmArgs = ['-Xmx12g']

maxHeapSize

environment

Specifies a single environment variable, as a key value pair separated by comma. Multiple variable can be specified that way:

environment "SOME_ENV_VAR", "some value"
environment "SOME_OTHER_ENV_VAR", "some other value"

systemProperties

A Groovy map.

systemProperties = [ "some.prop": "some.value", "some.other.prop": "some.other.value" ]

group

description

standardOutput

task someTask(type: JavaExec) {
  ...
  standardOutput = System.out
  ...
}

errorOutput

task someTask(type: JavaExec) {
  ...
  errorOutput = System.err
  ...
}