Klasse JavaExec

java.lang.Object
org.gradle.api.internal.AbstractTask
org.gradle.api.DefaultTask
org.gradle.api.internal.ConventionTask
org.gradle.api.tasks.JavaExec
Alle implementierten Schnittstellen:
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>

@DisableCachingByDefault(because="Gradle would require more information to cache this task") public abstract class JavaExec extends org.gradle.api.internal.ConventionTask implements JavaExecSpec
Executes a Java application in a child process.

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
    }
 }
 
  • Konstruktordetails

    • JavaExec

      public JavaExec()
  • Methodendetails

    • exec

      public void exec()
    • getAllJvmArgs

      public List<String> 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.
      Angegeben von:
      getAllJvmArgs in Schnittstelle JavaForkOptions
      Gibt zurück:
      The immutable list of arguments. Returns an empty list if there are no arguments.
    • setAllJvmArgs

      public void setAllJvmArgs(List<String> arguments)
      Sets the full set of arguments to use to launch the JVM for the process. Overwrites any previously set system properties, minimum/maximum heap size, assertions, and bootstrap classpath.
      Angegeben von:
      setAllJvmArgs in Schnittstelle JavaForkOptions
      Parameter:
      arguments - The arguments. Must not be null.
    • setAllJvmArgs

      public void setAllJvmArgs(Iterable<?> arguments)
      Sets the full set of arguments to use to launch the JVM for the process. Overwrites any previously set system properties, minimum/maximum heap size, assertions, and bootstrap classpath.
      Angegeben von:
      setAllJvmArgs in Schnittstelle JavaForkOptions
      Parameter:
      arguments - The arguments. Must not be null.
    • getJvmArgs

      public List<String> getJvmArgs()
      Returns the extra arguments to use to launch the JVM for the process. Does not include system properties and the minimum/maximum heap size.
      Angegeben von:
      getJvmArgs in Schnittstelle JavaForkOptions
      Gibt zurück:
      The immutable list of arguments. Returns an empty list if there are no arguments.
    • setJvmArgs

      public void setJvmArgs(List<String> arguments)
      Sets the extra arguments to use to launch the JVM for the process. System properties and minimum/maximum heap size are updated.
      Angegeben von:
      setJvmArgs in Schnittstelle JavaForkOptions
      Parameter:
      arguments - The arguments. Must not be null.
    • setJvmArgs

      public void setJvmArgs(Iterable<?> arguments)
      Sets the extra arguments to use to launch the JVM for the process. System properties and minimum/maximum heap size are updated.
      Angegeben von:
      setJvmArgs in Schnittstelle JavaForkOptions
      Parameter:
      arguments - The arguments. Must not be null.
    • jvmArgs

      public JavaExec jvmArgs(Iterable<?> arguments)
      Adds some arguments to use to launch the JVM for the process.
      Angegeben von:
      jvmArgs in Schnittstelle JavaForkOptions
      Parameter:
      arguments - The arguments. Must not be null.
      Gibt zurück:
      this
    • jvmArgs

      public JavaExec jvmArgs(Object... arguments)
      Adds some arguments to use to launch the JVM for the process.
      Angegeben von:
      jvmArgs in Schnittstelle JavaForkOptions
      Parameter:
      arguments - The arguments.
      Gibt zurück:
      this
    • getSystemProperties

      public Map<String,Object> getSystemProperties()
      Returns the system properties which will be used for the process.
      Angegeben von:
      getSystemProperties in Schnittstelle JavaForkOptions
      Gibt zurück:
      The system properties. Returns an empty map when there are no system properties.
    • setSystemProperties

      public void setSystemProperties(Map<String,?> properties)
      Sets the system properties to use for the process.
      Angegeben von:
      setSystemProperties in Schnittstelle JavaForkOptions
      Parameter:
      properties - The system properties. Must not be null.
    • systemProperties

      public JavaExec systemProperties(Map<String,?> properties)
      Adds some system properties to use for the process.
      Angegeben von:
      systemProperties in Schnittstelle JavaForkOptions
      Parameter:
      properties - The system properties. Must not be null.
      Gibt zurück:
      this
    • systemProperty

      public JavaExec systemProperty(String name, Object value)
      Adds a system property to use for the process.
      Angegeben von:
      systemProperty in Schnittstelle JavaForkOptions
      Parameter:
      name - The name of the property
      value - The value for the property. May be null.
      Gibt zurück:
      this
    • getBootstrapClasspath

      public FileCollection getBootstrapClasspath()
      Returns the bootstrap classpath to use for the process. The default bootstrap classpath for the JVM is used when this classpath is empty.
      Angegeben von:
      getBootstrapClasspath in Schnittstelle JavaForkOptions
      Gibt zurück:
      The bootstrap classpath. Never returns null.
    • setBootstrapClasspath

      public void setBootstrapClasspath(FileCollection classpath)
      Sets the bootstrap classpath to use for the process. Set to an empty classpath to use the default bootstrap classpath for the specified JVM.
      Angegeben von:
      setBootstrapClasspath in Schnittstelle JavaForkOptions
      Parameter:
      classpath - The classpath. Must not be null. Can be empty.
    • bootstrapClasspath

      public JavaExec bootstrapClasspath(Object... classpath)
      Adds the given values to the end of the bootstrap classpath for the process.
      Angegeben von:
      bootstrapClasspath in Schnittstelle JavaForkOptions
      Parameter:
      classpath - The classpath.
      Gibt zurück:
      this
    • getMinHeapSize

      public String getMinHeapSize()
      Returns the minimum heap size for the process, if any.
      Angegeben von:
      getMinHeapSize in Schnittstelle JavaForkOptions
      Gibt zurück:
      The minimum heap size. Returns null if the default minimum heap size should be used.
    • setMinHeapSize

      public void setMinHeapSize(String heapSize)
      Sets the minimum heap size for the process. Supports the units megabytes (e.g. "512m") and gigabytes (e.g. "1g").
      Angegeben von:
      setMinHeapSize in Schnittstelle JavaForkOptions
      Parameter:
      heapSize - The minimum heap size. Use null for the default minimum heap size.
    • getDefaultCharacterEncoding

      public String getDefaultCharacterEncoding()
      Returns the default character encoding to use.
      Angegeben von:
      getDefaultCharacterEncoding in Schnittstelle JavaForkOptions
      Gibt zurück:
      The default character encoding. Returns null if the default character encoding of this JVM should be used.
    • setDefaultCharacterEncoding

      public void setDefaultCharacterEncoding(String defaultCharacterEncoding)
      Sets the default character encoding to use. Note: Many JVM implementations support the setting of this attribute via system property on startup (namely, the 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().
      Angegeben von:
      setDefaultCharacterEncoding in Schnittstelle JavaForkOptions
      Parameter:
      defaultCharacterEncoding - The default character encoding. Use null to use this JVM's default charset
    • getMaxHeapSize

      public String getMaxHeapSize()
      Returns the maximum heap size for the process, if any.
      Angegeben von:
      getMaxHeapSize in Schnittstelle JavaForkOptions
      Gibt zurück:
      The maximum heap size. Returns null if the default maximum heap size should be used.
    • setMaxHeapSize

      public void setMaxHeapSize(String heapSize)
      Sets the maximum heap size for the process. Supports the units megabytes (e.g. "512m") and gigabytes (e.g. "1g").
      Angegeben von:
      setMaxHeapSize in Schnittstelle JavaForkOptions
      Parameter:
      heapSize - The heap size. Use null for the default maximum heap size.
    • getEnableAssertions

      public boolean getEnableAssertions()
      Returns true if assertions are enabled for the process.
      Angegeben von:
      getEnableAssertions in Schnittstelle JavaForkOptions
      Gibt zurück:
      true if assertions are enabled, false if disabled
    • setEnableAssertions

      public void setEnableAssertions(boolean enabled)
      Enable or disable assertions for the process.
      Angegeben von:
      setEnableAssertions in Schnittstelle JavaForkOptions
      Parameter:
      enabled - true to enable assertions, false to disable.
    • getDebug

      public boolean 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 for Test.getForkEvery().

      Since Gradle 5.6, you can configure the port and other Java debug properties via JavaForkOptions.debugOptions(Action).

      Angegeben von:
      getDebug in Schnittstelle JavaForkOptions
      Gibt zurück:
      true when debugging is enabled, false to disable.
    • setDebug

      public void setDebug(boolean enabled)
      Enable or disable debugging for the process. When enabled, the process is started suspended and listening on port 5005.

      The debug properties (e.g. the port number) can be configured in JavaForkOptions.debugOptions(Action).

      Angegeben von:
      setDebug in Schnittstelle JavaForkOptions
      Parameter:
      enabled - true to enable debugging, false to disable.
    • getDebugOptions

      public JavaDebugOptions 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.
      Angegeben von:
      getDebugOptions in Schnittstelle JavaForkOptions
    • debugOptions

      public void debugOptions(Action<JavaDebugOptions> action)
      Configures Java Debug Wire Protocol properties for the process. If JavaForkOptions.setDebug(boolean) is enabled then the -agentlib:jdwp=... will be appended to the JVM arguments with the configuration from the parameter.
      Angegeben von:
      debugOptions in Schnittstelle JavaForkOptions
      Parameter:
      action - the Java debug configuration
    • getMainModule

      public Property<String> getMainModule()
      The name of the main module to be executed if the application should run as a Java module.
      Angegeben von:
      getMainModule in Schnittstelle JavaExecSpec
    • getMainClass

      public Property<String> 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-Class attribute.

      Angegeben von:
      getMainClass in Schnittstelle JavaExecSpec
    • getArgs

      public List<String> getArgs()
      Returns the arguments passed to the main class to be executed.
      Angegeben von:
      getArgs in Schnittstelle JavaExecSpec
    • setArgsString

      public JavaExec setArgsString(String args)
      Parses an argument list from 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'".

      Parameter:
      args - Args for the main class. Will be parsed into an argument list.
      Gibt zurück:
      this
      Seit:
      4.9
    • setArgs

      public JavaExec setArgs(List<String> applicationArgs)
      Sets the args for the main class to be executed.
      Angegeben von:
      setArgs in Schnittstelle JavaExecSpec
      Parameter:
      applicationArgs - Args for the main class.
      Gibt zurück:
      this
    • setArgs

      public JavaExec setArgs(Iterable<?> applicationArgs)
      Sets the args for the main class to be executed.
      Angegeben von:
      setArgs in Schnittstelle JavaExecSpec
      Parameter:
      applicationArgs - Args for the main class.
      Gibt zurück:
      this
    • args

      public JavaExec args(Object... args)
      Adds args for the main class to be executed.
      Angegeben von:
      args in Schnittstelle JavaExecSpec
      Parameter:
      args - Args for the main class.
      Gibt zurück:
      this
    • args

      public JavaExecSpec args(Iterable<?> args)
      Adds args for the main class to be executed.
      Angegeben von:
      args in Schnittstelle JavaExecSpec
      Parameter:
      args - Args for the main class.
      Gibt zurück:
      this
    • getArgumentProviders

      public List<CommandLineArgumentProvider> getArgumentProviders()
      Argument providers for the application.
      Angegeben von:
      getArgumentProviders in Schnittstelle JavaExecSpec
    • setClasspath

      public JavaExec setClasspath(FileCollection classpath)
      Sets the classpath for executing the main class.
      Angegeben von:
      setClasspath in Schnittstelle JavaExecSpec
      Parameter:
      classpath - the classpath
      Gibt zurück:
      this
    • classpath

      public JavaExec classpath(Object... paths)
      Adds elements to the classpath for executing the main class.
      Angegeben von:
      classpath in Schnittstelle JavaExecSpec
      Parameter:
      paths - classpath elements
      Gibt zurück:
      this
    • getClasspath

      public FileCollection getClasspath()
      Returns the classpath for executing the main class.
      Angegeben von:
      getClasspath in Schnittstelle JavaExecSpec
    • getModularity

      public ModularitySpec getModularity()
      Returns the module path handling for executing the main class.
      Angegeben von:
      getModularity in Schnittstelle JavaExecSpec
    • copyTo

      public JavaExec copyTo(JavaForkOptions options)
      Copies these options to the given options.
      Angegeben von:
      copyTo in Schnittstelle JavaForkOptions
      Parameter:
      options - The target options.
      Gibt zurück:
      this
    • getJavaVersion

      @Input public JavaVersion getJavaVersion()
      Returns the version of the Java executable specified by getJavaLauncher().
      Seit:
      5.2
    • getExecutable

      @Internal("covered by getJavaVersion") @Nullable public String getExecutable()
      Returns the name of the executable to use.
      Angegeben von:
      getExecutable in Schnittstelle ProcessForkOptions
      Gibt zurück:
      The executable.
    • setExecutable

      public void setExecutable(String executable)
      Sets the name of the executable to use.
      Angegeben von:
      setExecutable in Schnittstelle ProcessForkOptions
      Parameter:
      executable - The executable. Must not be null.
    • setExecutable

      public void setExecutable(Object executable)
      Sets the name of the executable to use.
      Angegeben von:
      setExecutable in Schnittstelle ProcessForkOptions
      Parameter:
      executable - The executable. Must not be null.
    • executable

      public JavaExec executable(Object executable)
      Sets the name of the executable to use.
      Angegeben von:
      executable in Schnittstelle ProcessForkOptions
      Parameter:
      executable - The executable. Must not be null.
      Gibt zurück:
      this
    • getWorkingDir

      @Internal public File getWorkingDir()
      Returns the working directory for the process. Defaults to the project directory.
      Angegeben von:
      getWorkingDir in Schnittstelle ProcessForkOptions
      Gibt zurück:
      The working directory. Never returns null.
    • setWorkingDir

      public void setWorkingDir(File dir)
      Sets the working directory for the process.
      Angegeben von:
      setWorkingDir in Schnittstelle ProcessForkOptions
      Parameter:
      dir - The working directory. Must not be null.
    • setWorkingDir

      public void setWorkingDir(Object dir)
      Sets the working directory for the process. The supplied argument is evaluated as per Project.file(Object).
      Angegeben von:
      setWorkingDir in Schnittstelle ProcessForkOptions
      Parameter:
      dir - The working directory. Must not be null.
    • workingDir

      public JavaExec workingDir(Object dir)
      Sets the working directory for the process. The supplied argument is evaluated as per Project.file(Object).
      Angegeben von:
      workingDir in Schnittstelle ProcessForkOptions
      Parameter:
      dir - The working directory. Must not be null.
      Gibt zurück:
      this
    • getEnvironment

      @Internal public Map<String,Object> getEnvironment()
      The environment variables to use for the process. Defaults to the environment of this process.
      Angegeben von:
      getEnvironment in Schnittstelle ProcessForkOptions
      Gibt zurück:
      The environment. Returns an empty map when there are no environment variables.
    • setEnvironment

      public void setEnvironment(Map<String,?> environmentVariables)
      Sets the environment variable to use for the process.
      Angegeben von:
      setEnvironment in Schnittstelle ProcessForkOptions
      Parameter:
      environmentVariables - The environment variables. Must not be null.
    • environment

      public JavaExec environment(String name, Object value)
      Adds an environment variable to the environment for this process.
      Angegeben von:
      environment in Schnittstelle ProcessForkOptions
      Parameter:
      name - The name of the variable.
      value - The value for the variable. Must not be null.
      Gibt zurück:
      this
    • environment

      public JavaExec environment(Map<String,?> environmentVariables)
      Adds some environment variables to the environment for this process.
      Angegeben von:
      environment in Schnittstelle ProcessForkOptions
      Parameter:
      environmentVariables - The environment variables. Must not be null.
      Gibt zurück:
      this
    • copyTo

      public JavaExec copyTo(ProcessForkOptions target)
      Copies these options to the given target options.
      Angegeben von:
      copyTo in Schnittstelle ProcessForkOptions
      Parameter:
      target - The target options
      Gibt zurück:
      this
    • setStandardInput

      public JavaExec setStandardInput(InputStream inputStream)
      Sets the standard input stream for the process executing the command. The stream is closed after the process completes.
      Angegeben von:
      setStandardInput in Schnittstelle BaseExecSpec
      Parameter:
      inputStream - The standard input stream for the process. Must not be null.
      Gibt zurück:
      this
    • getStandardInput

      @Internal public InputStream 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.
      Angegeben von:
      getStandardInput in Schnittstelle BaseExecSpec
      Gibt zurück:
      The standard input stream.
    • setStandardOutput

      public JavaExec setStandardOutput(OutputStream outputStream)
      Sets the output stream to consume standard output from the process executing the command. The stream is closed after the process completes.
      Angegeben von:
      setStandardOutput in Schnittstelle BaseExecSpec
      Parameter:
      outputStream - The standard output stream for the process. Must not be null.
      Gibt zurück:
      this
    • getStandardOutput

      @Internal public OutputStream getStandardOutput()
      Returns the output stream to consume standard output from the process executing the command. Defaults to System.out.
      Angegeben von:
      getStandardOutput in Schnittstelle BaseExecSpec
      Gibt zurück:
      The output stream
    • setErrorOutput

      public JavaExec setErrorOutput(OutputStream outputStream)
      Sets the output stream to consume standard error from the process executing the command. The stream is closed after the process completes.
      Angegeben von:
      setErrorOutput in Schnittstelle BaseExecSpec
      Parameter:
      outputStream - The standard output error stream for the process. Must not be null.
      Gibt zurück:
      this
    • getErrorOutput

      @Internal public OutputStream getErrorOutput()
      Returns the output stream to consume standard error from the process executing the command. Default to System.err.
      Angegeben von:
      getErrorOutput in Schnittstelle BaseExecSpec
      Gibt zurück:
      The error output stream.
    • setIgnoreExitValue

      public JavaExecSpec setIgnoreExitValue(boolean ignoreExitValue)
      Sets whether a non-zero exit value is ignored, or an exception thrown.
      Angegeben von:
      setIgnoreExitValue in Schnittstelle BaseExecSpec
      Parameter:
      ignoreExitValue - whether a non-zero exit value is ignored, or an exception thrown
      Gibt zurück:
      this
    • isIgnoreExitValue

      @Input public boolean isIgnoreExitValue()
      Tells whether a non-zero exit value is ignored, or an exception thrown. Defaults to false.
      Angegeben von:
      isIgnoreExitValue in Schnittstelle BaseExecSpec
      Gibt zurück:
      whether a non-zero exit value is ignored, or an exception thrown
    • getCommandLine

      @Internal public List<String> getCommandLine()
      Returns the full command line, including the executable plus its arguments.
      Angegeben von:
      getCommandLine in Schnittstelle BaseExecSpec
      Gibt zurück:
      The full command line, including the executable plus its arguments
    • getJvmArgumentProviders

      public List<CommandLineArgumentProvider> getJvmArgumentProviders()
      Command line argument providers for the java process to fork.
      Angegeben von:
      getJvmArgumentProviders in Schnittstelle JavaForkOptions
    • getJvmArguments

      public ListProperty<String> 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.
      Angegeben von:
      getJvmArguments in Schnittstelle JavaExecSpec
    • getExecutionResult

      @Internal public Provider<ExecResult> getExecutionResult()
      Returns the result for the command run by this task. The provider has no value if this task has not been executed yet.
      Gibt zurück:
      A provider of the result.
      Seit:
      6.1
    • getJavaLauncher

      public Property<JavaLauncher> getJavaLauncher()
      Configures the java executable to be used to run the tests.
      Seit:
      6.7
    • getObjectFactory

      @Inject protected ObjectFactory getObjectFactory()
    • getPropertyFactory

      @Inject protected org.gradle.api.internal.provider.PropertyFactory getPropertyFactory()
    • getExecActionFactory

      @Inject protected org.gradle.process.internal.ExecActionFactory getExecActionFactory()
    • getJavaToolchainService

      @Inject protected JavaToolchainService getJavaToolchainService()
    • getProviderFactory

      @Inject protected ProviderFactory getProviderFactory()