Comparable<Task>, org.gradle.api.internal.DynamicObjectAware, org.gradle.api.internal.IConventionAware, org.gradle.api.internal.TaskInternal, ExtensionAware, Task, BaseExecSpec, JavaExecSpec, JavaForkOptions, ProcessForkOptions, Configurable<Task>@DisableCachingByDefault(because="Gradle would require more information to cache this task") public class JavaExec extends org.gradle.api.internal.ConventionTask implements JavaExecSpec
Similar to Exec, but starts a JVM with the given classpath and application class.
plugins {
id 'java'
}
task runApp(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
mainClass = 'package.Main'
// arguments to pass to the application
args 'appArg1'
}
// Using and creating an Executable Jar
jar {
manifest {
attributes('Main-Class': 'package.Main')
}
}
task runExecutableJar(type: JavaExec) {
// Executable jars can have only _one_ jar on the classpath.
classpath = files(tasks.jar)
// 'main' does not need to be specified
// arguments to pass to the application
args 'appArg1'
}
The process can be started in debug mode (see getDebug()) in an ad-hoc manner by supplying the `--debug-jvm` switch when invoking the build.
gradle someJavaExecTask --debug-jvm
Also, debug configuration can be explicitly set in debugOptions(Action):
task runApp(type: JavaExec) {
...
debugOptions {
enabled = true
port = 5566
server = true
suspend = false
}
}
Task.NamerTASK_ACTION, TASK_CONSTRUCTOR_ARGS, TASK_DEPENDS_ON, TASK_DESCRIPTION, TASK_GROUP, TASK_NAME, TASK_OVERWRITE, TASK_TYPE| Constructor | Description |
|---|---|
JavaExec() |
| Modifier and Type | Method | Description |
|---|---|---|
JavaExecSpec |
args(Iterable<?> args) |
Adds args for the main class to be executed.
|
JavaExec |
args(Object... args) |
Adds args for the main class to be executed.
|
JavaExec |
bootstrapClasspath(Object... classpath) |
Adds the given values to the end of the bootstrap classpath for the process.
|
JavaExec |
classpath(Object... paths) |
Adds elements to the classpath for executing the main class.
|
JavaExec |
copyTo(JavaForkOptions options) |
Copies these options to the given options.
|
JavaExec |
copyTo(ProcessForkOptions target) |
Copies these options to the given target options.
|
void |
debugOptions(Action<JavaDebugOptions> action) |
Configures Java Debug Wire Protocol properties for the process.
|
JavaExec |
environment(String name,
Object value) |
Adds an environment variable to the environment for this process.
|
JavaExec |
environment(Map<String,?> environmentVariables) |
Adds some environment variables to the environment for this process.
|
void |
exec() |
|
JavaExec |
executable(Object executable) |
Sets the name of the executable to use.
|
List<String> |
getAllJvmArgs() |
Returns the full set of arguments to use to launch the JVM for the process.
|
List<String> |
getArgs() |
Returns the arguments passed to the main class to be executed.
|
List<CommandLineArgumentProvider> |
getArgumentProviders() |
Argument providers for the application.
|
FileCollection |
getBootstrapClasspath() |
Returns the bootstrap classpath to use for the process.
|
FileCollection |
getClasspath() |
Returns the classpath for executing the main class.
|
List<String> |
getCommandLine() |
Returns the full command line, including the executable plus its arguments.
|
boolean |
getDebug() |
Determines whether debugging is enabled for the test process.
|
JavaDebugOptions |
getDebugOptions() |
Returns the Java Debug Wire Protocol properties for the process.
|
String |
getDefaultCharacterEncoding() |
Returns the default character encoding to use.
|
boolean |
getEnableAssertions() |
Returns true if assertions are enabled for the process.
|
Map<String,Object> |
getEnvironment() |
The environment variables to use for the process.
|
OutputStream |
getErrorOutput() |
Returns the output stream to consume standard error from the process executing the command.
|
protected org.gradle.process.internal.ExecActionFactory |
getExecActionFactory() |
|
String |
getExecutable() |
Returns the name of the executable to use.
|
Provider<ExecResult> |
getExecutionResult() |
Returns the result for the command run by this task.
|
Property<JavaLauncher> |
getJavaLauncher() |
Configures the java executable to be used to run the tests.
|
JavaVersion |
getJavaVersion() |
Returns the version of the Java executable specified by
getExecutable(). |
List<String> |
getJvmArgs() |
Returns the extra arguments to use to launch the JVM for the process.
|
List<CommandLineArgumentProvider> |
getJvmArgumentProviders() |
Command line argument providers for the java process to fork.
|
String |
getMain() |
Deprecated.
|
Property<String> |
getMainClass() |
The fully qualified name of the Main class to be executed.
|
Property<String> |
getMainModule() |
The name of the main module to be executed if the application should run as a Java module.
|
String |
getMaxHeapSize() |
Returns the maximum heap size for the process, if any.
|
String |
getMinHeapSize() |
Returns the minimum heap size for the process, if any.
|
ModularitySpec |
getModularity() |
Returns the module path handling for executing the main class.
|
protected ObjectFactory |
getObjectFactory() |
|
protected ProviderFactory |
getProviderFactory() |
|
InputStream |
getStandardInput() |
Returns the standard input stream for the process executing the command.
|
OutputStream |
getStandardOutput() |
Returns the output stream to consume standard output from the process executing the command.
|
Map<String,Object> |
getSystemProperties() |
Returns the system properties which will be used for the process.
|
File |
getWorkingDir() |
Returns the working directory for the process.
|
boolean |
isIgnoreExitValue() |
Tells whether a non-zero exit value is ignored, or an exception thrown.
|
JavaExec |
jvmArgs(Iterable<?> arguments) |
Adds some arguments to use to launch the JVM for the process.
|
JavaExec |
jvmArgs(Object... arguments) |
Adds some arguments to use to launch the JVM for the process.
|
void |
setAllJvmArgs(Iterable<?> arguments) |
Sets the full set of arguments to use to launch the JVM for the process.
|
void |
setAllJvmArgs(List<String> arguments) |
Sets the full set of arguments to use to launch the JVM for the process.
|
JavaExec |
setArgs(Iterable<?> applicationArgs) |
Sets the args for the main class to be executed.
|
JavaExec |
setArgs(List<String> applicationArgs) |
Sets the args for the main class to be executed.
|
JavaExec |
setArgsString(String args) |
Parses an argument list from
args and passes it to setArgs(List). |
void |
setBootstrapClasspath(FileCollection classpath) |
Sets the bootstrap classpath to use for the process.
|
JavaExec |
setClasspath(FileCollection classpath) |
Sets the classpath for executing the main class.
|
void |
setDebug(boolean enabled) |
Enable or disable debugging for the process.
|
void |
setDefaultCharacterEncoding(String defaultCharacterEncoding) |
Sets the default character encoding to use.
|
void |
setEnableAssertions(boolean enabled) |
Enable or disable assertions for the process.
|
void |
setEnvironment(Map<String,?> environmentVariables) |
Sets the environment variable to use for the process.
|
JavaExec |
setErrorOutput(OutputStream outputStream) |
Sets the output stream to consume standard error from the process executing the command.
|
void |
setExecutable(Object executable) |
Sets the name of the executable to use.
|
void |
setExecutable(String executable) |
Sets the name of the executable to use.
|
JavaExecSpec |
setIgnoreExitValue(boolean ignoreExitValue) |
Sets whether a non-zero exit value is ignored, or an exception thrown.
|
void |
setJvmArgs(Iterable<?> arguments) |
Sets the extra arguments to use to launch the JVM for the process.
|
void |
setJvmArgs(List<String> arguments) |
Sets the extra arguments to use to launch the JVM for the process.
|
JavaExec |
setMain(String mainClassName) |
Deprecated.
|
void |
setMaxHeapSize(String heapSize) |
Sets the maximum heap size for the process.
|
void |
setMinHeapSize(String heapSize) |
Sets the minimum heap size for the process.
|
JavaExec |
setStandardInput(InputStream inputStream) |
Sets the standard input stream for the process executing the command.
|
JavaExec |
setStandardOutput(OutputStream outputStream) |
Sets the output stream to consume standard output from the process executing the command.
|
void |
setSystemProperties(Map<String,?> properties) |
Sets the system properties to use for the process.
|
void |
setWorkingDir(File dir) |
Sets the working directory for the process.
|
void |
setWorkingDir(Object dir) |
Sets the working directory for the process.
|
JavaExec |
systemProperties(Map<String,?> properties) |
Adds some system properties to use for the process.
|
JavaExec |
systemProperty(String name,
Object value) |
Adds a system property to use for the process.
|
JavaExec |
workingDir(Object dir) |
Sets the working directory for the process.
|
conventionMapping, conventionMapping, getConventionMappingcompareTo, configure, dependsOn, doFirst, doFirst, doFirst, doLast, doLast, doLast, finalizedBy, getActions, getAnt, getDependsOn, getDescription, getDestroyables, getDidWork, getEnabled, getExtensions, getFinalizedBy, getGroup, getInputs, getLocalState, getLogger, getLogging, getMustRunAfter, getName, getOutputs, getPath, getProject, getShouldRunAfter, getState, getTaskDependencies, getTemporaryDir, getTimeout, hasProperty, mustRunAfter, onlyIf, onlyIf, property, setActions, setDependsOn, setDescription, setDidWork, setEnabled, setFinalizedBy, setGroup, setMustRunAfter, setOnlyIf, setOnlyIf, setProperty, setShouldRunAfter, shouldRunAfter, usesServiceappendParallelSafeAction, doNotTrackState, getAsDynamicObject, getConvention, getIdentityPath, getImpliesSubProjects, getOnlyIf, getReasonNotToTrackState, getRequiredServices, getServices, getSharedResources, getStandardOutputCapture, getTaskActions, getTaskIdentity, getTemporaryDirFactory, hasTaskActions, injectIntoNewInstance, isEnabled, isHasCustomActions, prependParallelSafeAction, setImpliesSubProjectsclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitdoNotTrackState, getConvention@Inject protected ObjectFactory getObjectFactory()
@Inject protected org.gradle.process.internal.ExecActionFactory getExecActionFactory()
@Inject protected ProviderFactory getProviderFactory()
public void exec()
public List<String> getAllJvmArgs()
getAllJvmArgs in interface JavaForkOptionspublic void setAllJvmArgs(List<String> arguments)
setAllJvmArgs in interface JavaForkOptionsarguments - The arguments. Must not be null.public void setAllJvmArgs(Iterable<?> arguments)
setAllJvmArgs in interface JavaForkOptionsarguments - The arguments. Must not be null.public List<String> getJvmArgs()
getJvmArgs in interface JavaForkOptionspublic void setJvmArgs(List<String> arguments)
setJvmArgs in interface JavaForkOptionsarguments - The arguments. Must not be null.public void setJvmArgs(Iterable<?> arguments)
setJvmArgs in interface JavaForkOptionsarguments - The arguments. Must not be null.public JavaExec jvmArgs(Iterable<?> arguments)
jvmArgs in interface JavaForkOptionsarguments - The arguments. Must not be null.public JavaExec jvmArgs(Object... arguments)
jvmArgs in interface JavaForkOptionsarguments - The arguments.public Map<String,Object> getSystemProperties()
getSystemProperties in interface JavaForkOptionspublic void setSystemProperties(Map<String,?> properties)
setSystemProperties in interface JavaForkOptionsproperties - The system properties. Must not be null.public JavaExec systemProperties(Map<String,?> properties)
systemProperties in interface JavaForkOptionsproperties - The system properties. Must not be null.public JavaExec systemProperty(String name, Object value)
systemProperty in interface JavaForkOptionsname - The name of the propertyvalue - The value for the property. May be null.public FileCollection getBootstrapClasspath()
getBootstrapClasspath in interface JavaForkOptionspublic void setBootstrapClasspath(FileCollection classpath)
setBootstrapClasspath in interface JavaForkOptionsclasspath - The classpath. Must not be null. Can be empty.public JavaExec bootstrapClasspath(Object... classpath)
bootstrapClasspath in interface JavaForkOptionsclasspath - The classpath.public String getMinHeapSize()
getMinHeapSize in interface JavaForkOptionspublic void setMinHeapSize(String heapSize)
setMinHeapSize in interface JavaForkOptionsheapSize - The minimum heap size. Use null for the default minimum heap size.public String getDefaultCharacterEncoding()
getDefaultCharacterEncoding in interface JavaForkOptionsdefault character encoding of this JVM should be used.public void setDefaultCharacterEncoding(String defaultCharacterEncoding)
file.encoding property). For JVMs
where this is the case, setting the file.encoding property via JavaForkOptions.setSystemProperties(java.util.Map) or similar will have no effect as
this value will be overridden by the value specified by JavaForkOptions.getDefaultCharacterEncoding().setDefaultCharacterEncoding in interface JavaForkOptionsdefaultCharacterEncoding - The default character encoding. Use null to use this JVM's default charsetpublic String getMaxHeapSize()
getMaxHeapSize in interface JavaForkOptionspublic void setMaxHeapSize(String heapSize)
setMaxHeapSize in interface JavaForkOptionsheapSize - The heap size. Use null for the default maximum heap size.public boolean getEnableAssertions()
getEnableAssertions in interface JavaForkOptionspublic void setEnableAssertions(boolean enabled)
setEnableAssertions in interface JavaForkOptionsenabled - true to enable assertions, false to disable.public boolean getDebug()
debug = true — the process
is started in a suspended state, listening on port 5005. You should disable parallel test execution when
debugging and you will need to reattach the debugger occasionally if you use a non-zero value for
Test.getForkEvery().
Since Gradle 5.6, you can configure the port and other Java debug properties via
JavaForkOptions.debugOptions(Action).
getDebug in interface JavaForkOptionspublic void setDebug(boolean enabled)
The debug properties (e.g. the port number) can be configured in JavaForkOptions.debugOptions(Action).
setDebug in interface JavaForkOptionsenabled - true to enable debugging, false to disable.public JavaDebugOptions getDebugOptions()
JavaForkOptions-agentlib:jdwp=...
will be appended to the JVM arguments with the configuration from the parameter.getDebugOptions in interface JavaForkOptionspublic void debugOptions(Action<JavaDebugOptions> action)
JavaForkOptionsJavaForkOptions.setDebug(boolean) is enabled then
the -agentlib:jdwp=... will be appended to the JVM arguments with the configuration from the parameter.debugOptions in interface JavaForkOptionsaction - the Java debug configurationpublic Property<String> getMainModule()
getMainModule in interface JavaExecSpecpublic Property<String> getMainClass()
This does not need to be set if using an Executable Jar with a Main-Class attribute.
Use this property instead of JavaExecSpec.getMain() and JavaExecSpec.setMain(String).
getMainClass in interface JavaExecSpec@Deprecated @ReplacedBy("mainClass") public String getMain()
This does not need to be set if using an Executable Jar with a Main-Class attribute.
getMain in interface JavaExecSpec@Deprecated public JavaExec setMain(String mainClassName)
setMain in interface JavaExecSpecmainClassName - the fully qualified name of the main class to be executed.public List<String> getArgs()
getArgs in interface JavaExecSpecpublic JavaExec setArgsString(String args)
args and passes it to setArgs(List).
The parser supports both single quote (') and double quote (") as quote delimiters.
For example, to pass the argument foo bar, use "foo bar".
Note: the parser does not support using backslash to escape quotes. If this is needed,
use the other quote delimiter around it.
For example, to pass the argument 'singly quoted', use "'singly quoted'".
args - Args for the main class. Will be parsed into an argument list.public JavaExec setArgs(List<String> applicationArgs)
setArgs in interface JavaExecSpecapplicationArgs - Args for the main class.public JavaExec setArgs(Iterable<?> applicationArgs)
setArgs in interface JavaExecSpecapplicationArgs - Args for the main class.public JavaExec args(Object... args)
args in interface JavaExecSpecargs - Args for the main class.public JavaExecSpec args(Iterable<?> args)
args in interface JavaExecSpecargs - Args for the main class.public List<CommandLineArgumentProvider> getArgumentProviders()
getArgumentProviders in interface JavaExecSpecpublic JavaExec setClasspath(FileCollection classpath)
setClasspath in interface JavaExecSpecclasspath - the classpathpublic JavaExec classpath(Object... paths)
classpath in interface JavaExecSpecpaths - classpath elementspublic FileCollection getClasspath()
getClasspath in interface JavaExecSpecpublic ModularitySpec getModularity()
getModularity in interface JavaExecSpecpublic JavaExec copyTo(JavaForkOptions options)
copyTo in interface JavaForkOptionsoptions - The target options.@Input public JavaVersion getJavaVersion()
getExecutable().@Internal("covered by getJavaVersion") @Nullable public String getExecutable()
getExecutable in interface ProcessForkOptionspublic void setExecutable(String executable)
setExecutable in interface ProcessForkOptionsexecutable - The executable. Must not be null.public void setExecutable(Object executable)
setExecutable in interface ProcessForkOptionsexecutable - The executable. Must not be null.public JavaExec executable(Object executable)
executable in interface ProcessForkOptionsexecutable - The executable. Must not be null.@Internal public File getWorkingDir()
getWorkingDir in interface ProcessForkOptionspublic void setWorkingDir(File dir)
setWorkingDir in interface ProcessForkOptionsdir - The working directory. Must not be null.public void setWorkingDir(Object dir)
Project.file(Object).setWorkingDir in interface ProcessForkOptionsdir - The working directory. Must not be null.public JavaExec workingDir(Object dir)
Project.file(Object).workingDir in interface ProcessForkOptionsdir - The working directory. Must not be null.@Internal public Map<String,Object> getEnvironment()
getEnvironment in interface ProcessForkOptionspublic void setEnvironment(Map<String,?> environmentVariables)
setEnvironment in interface ProcessForkOptionsenvironmentVariables - The environment variables. Must not be null.public JavaExec environment(String name, Object value)
environment in interface ProcessForkOptionsname - The name of the variable.value - The value for the variable. Must not be null.public JavaExec environment(Map<String,?> environmentVariables)
environment in interface ProcessForkOptionsenvironmentVariables - The environment variables. Must not be null.public JavaExec copyTo(ProcessForkOptions target)
copyTo in interface ProcessForkOptionstarget - The target optionspublic JavaExec setStandardInput(InputStream inputStream)
setStandardInput in interface BaseExecSpecinputStream - The standard input stream for the process. Must not be null.@Internal public InputStream getStandardInput()
getStandardInput in interface BaseExecSpecpublic JavaExec setStandardOutput(OutputStream outputStream)
setStandardOutput in interface BaseExecSpecoutputStream - The standard output stream for the process. Must not be null.@Internal public OutputStream getStandardOutput()
System.out.getStandardOutput in interface BaseExecSpecpublic JavaExec setErrorOutput(OutputStream outputStream)
setErrorOutput in interface BaseExecSpecoutputStream - The standard output error stream for the process. Must not be null.@Internal public OutputStream getErrorOutput()
System.err.getErrorOutput in interface BaseExecSpecpublic JavaExecSpec setIgnoreExitValue(boolean ignoreExitValue)
setIgnoreExitValue in interface BaseExecSpecignoreExitValue - whether a non-zero exit value is ignored, or an exception thrown@Input public boolean isIgnoreExitValue()
false.isIgnoreExitValue in interface BaseExecSpec@Internal public List<String> getCommandLine()
getCommandLine in interface BaseExecSpecpublic List<CommandLineArgumentProvider> getJvmArgumentProviders()
getJvmArgumentProviders in interface JavaForkOptions@Internal public Provider<ExecResult> getExecutionResult()
@Optional public Property<JavaLauncher> getJavaLauncher()