Class JavaExec
- All Implemented Interfaces:
Comparable<Task>,org.gradle.api.internal.DynamicObjectAware,org.gradle.api.internal.IConventionAware,org.gradle.api.internal.TaskInternal,Named,ExtensionAware,Task,BaseExecSpec,JavaExecSpec,JavaForkOptions,ProcessForkOptions,Configurable<Task>
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
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classUpgrade forgetArgs()(package private) static classUpgrade forgetClasspath()(package private) static classUpgrade forgetErrorOutput()(package private) static classUpgrade forgetIgnoreExitValue()(package private) static classUpgrade forgetStandardInput()(package private) static classUpgrade forgetStandardOutput()Nested classes/interfaces inherited from interface org.gradle.api.Named
Named.Namer -
Field Summary
Fields inherited from interface org.gradle.api.Task
TASK_ACTION, TASK_CONSTRUCTOR_ARGS, TASK_DEPENDS_ON, TASK_DESCRIPTION, TASK_GROUP, TASK_NAME, TASK_OVERWRITE, TASK_TYPE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds args for the main class to be executed.Adds args for the main class to be executed.bootstrapClasspath(@Nullable Object... classpath) Adds the given values to the end of the bootstrap classpath for the process.Adds elements to the classpath for executing the main class.copyTo(JavaForkOptions options) Copies these options to the given options.copyTo(ProcessForkOptions target) Copies these options to the given target options.voiddebugOptions(Action<JavaDebugOptions> action) Configures Java Debug Wire Protocol properties for the process.environment(String name, Object value) Adds an environment variable to the environment for this process.environment(Map<String, ?> environmentVariables) Adds some environment variables to the environment for this process.voidexec()executable(Object executable) Sets the name of the executable to use.Returns the full set of arguments to use to launch the JVM for the process.getArgs()Returns the arguments passed to the main class to be executed.Argument providers for the application.Returns the bootstrap classpath to use for the process.Returns the classpath for executing the main class.Returns the full command line, including the executable plus its arguments.getDebug()Determines whether debugging is enabled for the test process.Returns the Java Debug Wire Protocol properties for the process.Returns the default character encoding to use.A flag that marks if assertions are enabled for the process.The environment variables to use for the process.Returns the output stream to consume standard error from the process executing the command.protected abstract org.gradle.process.internal.ExecActionFactoryReturns the name of the executable to use.Returns the result for the command run by this task.Tells whether a non-zero exit value is ignored, or an exception thrown.Added for Kotlin DSL source compatibility.abstract Property<JavaLauncher> Configures the java executable to be used to run the tests.protected abstract JavaToolchainServiceReturns the version of the Java executable specified bygetJavaLauncher().The extra arguments to use to launch the JVM for the process.Command line argument providers for the java process to fork.Extra JVM arguments to be to use to launch the JVM for the process.The fully qualified name of the Main class to be executed.The name of the main module to be executed if the application should run as a Java module.Returns the maximum heap size for the process, if any.Returns the minimum heap size for the process, if any.Returns the module path handling for executing the main class.protected abstract ObjectFactoryprotected abstract org.gradle.api.internal.provider.PropertyFactoryprotected abstract ProviderFactoryReturns the standard input stream for the process executing the command.Returns the output stream to consume standard output from the process executing the command.System properties which will be used for the process.Returns the working directory for the process.Adds some arguments to use to launch the JVM for the process.Adds some arguments to use to launch the JVM for the process.setArgsString(String args) Parses an argument list fromargsand passes it togetArgs().systemProperties(Map<String, ? extends @Nullable Object> properties) Adds some system properties to use for the process.systemProperty(String name, @Nullable Object value) Adds a system property to use for the process.workingDir(Object dir) Sets the working directory for the process.Methods inherited from class org.gradle.api.internal.ConventionTask
conventionMapping, conventionMapping, getConventionMappingMethods inherited from class org.gradle.api.DefaultTask
compareTo, 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, onlyIf, property, setActions, setDependsOn, setDescription, setDidWork, setEnabled, setFinalizedBy, setGroup, setMustRunAfter, setOnlyIf, setOnlyIf, setOnlyIf, setProperty, setShouldRunAfter, shouldRunAfter, usesServiceMethods inherited from class org.gradle.api.internal.AbstractTask
acceptServiceReferences, appendParallelSafeAction, doNotTrackState, doNotTrackStateIf, getAsDynamicObject, getIdentityPath, getImpliesSubProjects, getLifecycleDependencies, getOnlyIf, getReasonNotToTrackState, getReasonsNotToTrackState, getReasonTaskIsIncompatibleWithConfigurationCache, getRequiredServices, getServices, getSharedResources, getStandardOutputCapture, getTaskActions, getTaskIdentity, getTemporaryDirFactory, hasTaskActions, injectIntoNewInstance, isCompatibleWithConfigurationCache, isEnabled, isHasCustomActions, notCompatibleWithConfigurationCache, prependParallelSafeAction, restoreOnlyIf, restoreTaskActions, setImpliesSubProjectsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.gradle.api.Task
doNotTrackState, notCompatibleWithConfigurationCache
-
Constructor Details
-
JavaExec
public JavaExec()
-
-
Method Details
-
exec
public void exec() -
getAllJvmArgs
Returns the full set of arguments to use to launch the JVM for the process. This includes arguments to define system properties, the minimum/maximum heap size, and the bootstrap classpath.- Specified by:
getAllJvmArgsin interfaceJavaForkOptions- Returns:
- The immutable list of arguments. Returns an empty list if there are no arguments.
-
getJvmArgs
The extra arguments to use to launch the JVM for the process.- Specified by:
getJvmArgsin interfaceJavaForkOptions- Returns:
- The list of arguments. Returns an empty list if there are no arguments.
-
jvmArgs
Adds some arguments to use to launch the JVM for the process.- Specified by:
jvmArgsin interfaceJavaForkOptions- Parameters:
arguments- The arguments. Must not be null.- Returns:
- this
-
jvmArgs
Adds some arguments to use to launch the JVM for the process.- Specified by:
jvmArgsin interfaceJavaForkOptions- Parameters:
arguments- The arguments.- Returns:
- this
-
getSystemProperties
System properties which will be used for the process.- Specified by:
getSystemPropertiesin interfaceJavaForkOptions- Returns:
- The system properties. Returns an empty map when there are no system properties.
-
systemProperties
Adds some system properties to use for the process.- Specified by:
systemPropertiesin interfaceJavaForkOptions- Parameters:
properties- The system properties. Must not be null.- Returns:
- this
-
systemProperty
Adds a system property to use for the process.- Specified by:
systemPropertyin interfaceJavaForkOptions- Parameters:
name- The name of the propertyvalue- The value for the property. May be null.- Returns:
- this
-
getBootstrapClasspath
Returns the bootstrap classpath to use for the process. The default bootstrap classpath for the JVM is used when this classpath is empty.- Specified by:
getBootstrapClasspathin interfaceJavaForkOptions- Returns:
- The bootstrap classpath. Never returns null.
-
bootstrapClasspath
Adds the given values to the end of the bootstrap classpath for the process.- Specified by:
bootstrapClasspathin interfaceJavaForkOptions- Parameters:
classpath- The classpath.- Returns:
- this
-
getMinHeapSize
Returns the minimum heap size for the process, if any. Supports the units megabytes (e.g. "512m") and gigabytes (e.g. "1g").- Specified by:
getMinHeapSizein interfaceJavaForkOptions- Returns:
- The minimum heap size. Returns null if the default minimum heap size should be used.
-
getDefaultCharacterEncoding
Returns the default character encoding to use.- Specified by:
getDefaultCharacterEncodingin interfaceJavaForkOptions- Returns:
- The default character encoding. Returns null if the
default character encoding of this JVMshould be used.
-
getMaxHeapSize
Returns the maximum heap size for the process, if any. Supports the units megabytes (e.g. "512m") and gigabytes (e.g. "1g").- Specified by:
getMaxHeapSizein interfaceJavaForkOptions- Returns:
- The maximum heap size. Returns null if the default maximum heap size should be used.
-
getEnableAssertions
A flag that marks if assertions are enabled for the process.- Specified by:
getEnableAssertionsin interfaceJavaForkOptions
-
getDebug
Determines whether debugging is enabled for the test process. When enabled —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 forTest.getForkEvery().Since Gradle 5.6, you can configure the port and other Java debug properties via
JavaForkOptions.debugOptions(Action).- Specified by:
getDebugin interfaceJavaForkOptions
-
getDebugOptions
Returns the Java Debug Wire Protocol properties for the process. If enabled then the-agentlib:jdwp=...will be appended to the JVM arguments with the configuration from the parameter.- Specified by:
getDebugOptionsin interfaceJavaForkOptions
-
debugOptions
Configures Java Debug Wire Protocol properties for the process. IfJavaForkOptions.getDebug()(boolean)} is enabled then the-agentlib:jdwp=...will be appended to the JVM arguments with the configuration from the parameter.- Specified by:
debugOptionsin interfaceJavaForkOptions- Parameters:
action- the Java debug configuration
-
getMainModule
The name of the main module to be executed if the application should run as a Java module.- Specified by:
getMainModulein interfaceJavaExecSpec
-
getMainClass
The fully qualified name of the Main class to be executed.This does not need to be set if using an Executable Jar with a
Main-Classattribute.- Specified by:
getMainClassin interfaceJavaExecSpec
-
getArgs
Returns the arguments passed to the main class to be executed.- Specified by:
getArgsin interfaceJavaExecSpec
-
setArgsString
Parses an argument list fromargsand passes it togetArgs().The parser supports both single quote (
') and double quote (") as quote delimiters. For example, to pass the argumentfoo 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'".- Parameters:
args- Args for the main class. Will be parsed into an argument list.- Returns:
- this
- Since:
- 4.9
-
args
Adds args for the main class to be executed.- Specified by:
argsin interfaceJavaExecSpec- Parameters:
args- Args for the main class.- Returns:
- this
-
args
Adds args for the main class to be executed.- Specified by:
argsin interfaceJavaExecSpec- Parameters:
args- Args for the main class.- Returns:
- this
-
getArgumentProviders
Argument providers for the application.- Specified by:
getArgumentProvidersin interfaceJavaExecSpec
-
classpath
Adds elements to the classpath for executing the main class.- Specified by:
classpathin interfaceJavaExecSpec- Parameters:
paths- classpath elements- Returns:
- this
-
getClasspath
Returns the classpath for executing the main class.- Specified by:
getClasspathin interfaceJavaExecSpec
-
getModularity
Returns the module path handling for executing the main class.- Specified by:
getModularityin interfaceJavaExecSpec
-
copyTo
Copies these options to the given options.- Specified by:
copyToin interfaceJavaForkOptions- Parameters:
options- The target options.- Returns:
- this
-
getJavaVersion
@Internal("covered by getJavaLauncher().getMetadata().getLanguageVersion()") public Provider<JavaVersion> getJavaVersion()Returns the version of the Java executable specified bygetJavaLauncher().- Since:
- 5.2
-
getExecutable
Returns the name of the executable to use.- Specified by:
getExecutablein interfaceProcessForkOptions- Returns:
- The executable.
-
executable
Sets the name of the executable to use.- Specified by:
executablein interfaceProcessForkOptions- Parameters:
executable- The executable. Must not be null.- Returns:
- this
-
getWorkingDir
Returns the working directory for the process. Defaults to the project directory.- Specified by:
getWorkingDirin interfaceProcessForkOptions- Returns:
- The working directory. Never returns null.
-
workingDir
Sets the working directory for the process. The supplied argument is evaluated as perProject.file(Object).- Specified by:
workingDirin interfaceProcessForkOptions- Parameters:
dir- The working directory. Must not be null.- Returns:
- this
-
getEnvironment
The environment variables to use for the process. Defaults to the environment of this process.- Specified by:
getEnvironmentin interfaceProcessForkOptions- Returns:
- The environment. Returns an empty map when there are no environment variables.
-
environment
Adds an environment variable to the environment for this process.- Specified by:
environmentin interfaceProcessForkOptions- Parameters:
name- The name of the variable.value- The value for the variable. Must not be null.- Returns:
- this
-
environment
Adds some environment variables to the environment for this process.- Specified by:
environmentin interfaceProcessForkOptions- Parameters:
environmentVariables- The environment variables. Must not be null.- Returns:
- this
-
copyTo
Copies these options to the given target options.- Specified by:
copyToin interfaceProcessForkOptions- Parameters:
target- The target options- Returns:
- this
-
getStandardInput
Returns the standard input stream for the process executing the command. The stream is closed after the process completes. Defaults to an empty stream.- Specified by:
getStandardInputin interfaceBaseExecSpec- Returns:
- The standard input stream.
-
getStandardOutput
Returns the output stream to consume standard output from the process executing the command. Defaults toSystem.out.- Specified by:
getStandardOutputin interfaceBaseExecSpec- Returns:
- The output stream
-
getErrorOutput
Returns the output stream to consume standard error from the process executing the command. Default toSystem.err.- Specified by:
getErrorOutputin interfaceBaseExecSpec- Returns:
- The error output stream.
-
getIgnoreExitValue
Tells whether a non-zero exit value is ignored, or an exception thrown. Defaults tofalse.- Specified by:
getIgnoreExitValuein interfaceBaseExecSpec- Returns:
- whether a non-zero exit value is ignored, or an exception thrown
-
getIsIgnoreExitValue
Added for Kotlin DSL source compatibility.- Specified by:
getIsIgnoreExitValuein interfaceBaseExecSpec
-
getCommandLine
Returns the full command line, including the executable plus its arguments.- Specified by:
getCommandLinein interfaceBaseExecSpec- Returns:
- The full command line, including the executable plus its arguments
-
getJvmArgumentProviders
Command line argument providers for the java process to fork.- Specified by:
getJvmArgumentProvidersin interfaceJavaForkOptions
-
getJvmArguments
Extra JVM arguments to be to use to launch the JVM for the process. Must be used to set a convention for JVM arguments.- Specified by:
getJvmArgumentsin interfaceJavaExecSpec
-
getExecutionResult
Returns the result for the command run by this task. The provider has no value if this task has not been executed yet.- Returns:
- A provider of the result.
- Since:
- 6.1
-
getJavaLauncher
Configures the java executable to be used to run the tests.- Since:
- 6.7
-
getObjectFactory
-
getPropertyFactory
@Inject protected abstract org.gradle.api.internal.provider.PropertyFactory getPropertyFactory() -
getExecActionFactory
@Inject protected abstract org.gradle.process.internal.ExecActionFactory getExecActionFactory() -
getJavaToolchainService
-
getProviderFactory
-