Package org.gradle.api

# Interface Script

public interface Script

This interface is implemented by all Gradle Groovy DSL scripts to add in some Gradle-specific methods. As your compiled script class will implement this interface, you can use the methods and properties declared by this interface directly in your script.

Generally, a `Script` object will have a delegate object attached to it. For example, a build script will have a `Project` instance attached to it, and an initialization script will have a `Gradle` instance attached to it. Any property reference or method call which is not found on this `Script` object is forwarded to the delegate object.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `apply(Closure closure)`
  Configures the delegate object for this script using plugins or scripts.
  `void`
  `apply(Map<String,?> options)`
  Configures the delegate object for this script using plugins or scripts.
  `void`
  `buildscript(Closure configureClosure)`
  Configures the classpath for this script.
  `WorkResult`
  `copy(Closure closure)`
  Copy the specified files.
  `CopySpec`
  `copySpec(Closure closure)`
  Creates a `CopySpec` which can later be used to copy files or create an archive.
  `boolean`
  `delete(Object... paths)`
  Deletes files and directories.
  `ExecResult`
  `exec(Closure closure)`
  Deprecated.
  Since 8.11.
  `ExecResult`
  `exec(Action<? super ExecSpec> action)`
  Deprecated.
  Since 8.11.
  `File`
  `file(Object path)`
  Resolves a file path relative to the directory containing this script.
  `File`
  `file(Object path, PathValidation validation)`
  Resolves a file path relative to the directory containing this script and validates it using the given scheme.
  `ConfigurableFileCollection`
  `files(Object... paths)`
  Returns a `ConfigurableFileCollection` containing the given files.
  `ConfigurableFileCollection`
  `files(Object paths, Closure configureClosure)`
  Creates a new `ConfigurableFileCollection` using the given paths.
  `ConfigurableFileTree`
  `fileTree(Object baseDir)`
  Creates a new `ConfigurableFileTree` using the given base directory.
  `ConfigurableFileTree`
  `fileTree(Object baseDir, Closure configureClosure)`
  Creates a new `ConfigurableFileTree` using the given base directory.
  `ConfigurableFileTree`
  `fileTree(Map<String,?> args)`
  Creates a new `ConfigurableFileTree` using the provided map of arguments.
  `ScriptHandler`
  `getBuildscript()`
  Returns the script handler for this script.
  `Logger`
  `getLogger()`
  Returns the logger for this script.
  `LoggingManager`
  `getLogging()`
  Returns the `LoggingManager` which can be used to receive logging and to control the standard output/error capture for this script.
  `ResourceHandler`
  `getResources()`
  Provides access to resource-specific utility methods, for example factory methods that create various resources.
  `ExecResult`
  `javaexec(Closure closure)`
  Deprecated.
  Since 8.11.
  `ExecResult`
  `javaexec(Action<? super JavaExecSpec> action)`
  Deprecated.
  Since 8.11.
  `File`
  `mkdir(Object path)`
  Creates a directory and returns a file pointing to it.
  `<T> Provider<T>`
  `provider(Callable<T> value)`
  Creates a `Provider` implementation based on the provided value.
  `String`
  `relativePath(Object path)`
  Returns the relative path from the directory containing this script to the given path.
  `FileTree`
  `tarTree(Object tarPath)`
  Creates a new `FileTree` which contains the contents of the given TAR file.
  `URI`
  `uri(Object path)`
  Resolves a file path to a URI, relative to the directory containing this script.
  `FileTree`
  `zipTree(Object zipPath)`
  Creates a new `FileTree` which contains the contents of the given ZIP file.

- ## Method Details

  - ### apply

    void apply(Closure closure)

    Configures the delegate object for this script using plugins or scripts.

    The given closure is used to configure an `ObjectConfigurationAction` which is then used to configure the delegate object.

    Parameters:
      `closure` - The closure to configure the `ObjectConfigurationAction`.

  - ### apply

    void apply(Map<String,?> options)

    Configures the delegate object for this script using plugins or scripts.

    The following options are available:

    - `from`: A script to apply to the delegate object. Accepts any path supported by `uri(Object)`.
    - `plugin`: The id or implementation class of the plugin to apply to the delegate object.
    - `to`: The target delegate object or objects.

    For more detail, see `ObjectConfigurationAction`.

    Parameters:
      `options` - The options to use to configure the `ObjectConfigurationAction`.

  - ### getBuildscript

    ScriptHandler getBuildscript()
    Returns the script handler for this script. You can use this handler to manage the classpath used to compile and execute this script.
    Returns:
      the classpath handler. Never returns null.

  - ### buildscript

    void buildscript(Closure configureClosure)
    Configures the classpath for this script.

    The given closure is executed against this script's `ScriptHandler`. The `ScriptHandler` is passed to the closure as the closure's delegate.

    Parameters:
      `configureClosure` - the closure to use to configure the script classpath.

  - ### file

    File file(Object path)

    Resolves a file path relative to the directory containing this script. This works as described for `Project.file(Object)`

    Parameters:
      `path` - The object to resolve as a File.
    Returns:
      The resolved file. Never returns null.

  - ### file

    File file(Object path, PathValidation validation) throws InvalidUserDataException

    Resolves a file path relative to the directory containing this script and validates it using the given scheme. See `PathValidation` for the list of possible validations.

    Parameters:
      `path` - An object to resolve as a File.
      `validation` - The validation to perform on the file.
    Returns:
      The resolved file. Never returns null.
    Throws:
      `InvalidUserDataException` - When the file does not meet the given validation constraint.

  - ### uri

    URI uri(Object path)

    Resolves a file path to a URI, relative to the directory containing this script. Evaluates the provided path object as described for `file(Object)`, with the exception that any URI scheme is supported, not just 'file:' URIs.

    Parameters:
      `path` - The object to resolve as a URI.
    Returns:
      The resolved URI. Never returns null.

  - ### files

    ConfigurableFileCollection files(Object... paths)

    Returns a `ConfigurableFileCollection` containing the given files. This works as described for `Project.files(Object...)`. Relative paths are resolved relative to the directory containing this script.

    Parameters:
      `paths` - The paths to the files. May be empty.
    Returns:
      The file collection. Never returns null.

  - ### files

    ConfigurableFileCollection files(Object paths, Closure configureClosure)

    Creates a new `ConfigurableFileCollection` using the given paths. The file collection is configured using the given closure. This method works as described for `Project.files(Object, groovy.lang.Closure)`. Relative paths are resolved relative to the directory containing this script.

    Parameters:
      `paths` - The contents of the file collection. Evaluated as per `files(Object...)`.
      `configureClosure` - The closure to use to configure the file collection.
    Returns:
      the configured file tree. Never returns null.

  - ### relativePath

    String relativePath(Object path)

    Returns the relative path from the directory containing this script to the given path. The given path object is (logically) resolved as described for `file(Object)`, from which a relative path is calculated.

    Parameters:
      `path` - The path to convert to a relative path.
    Returns:
      The relative path. Never returns null.

  - ### fileTree

    ConfigurableFileTree fileTree(Object baseDir)

    Creates a new `ConfigurableFileTree` using the given base directory. The given baseDir path is evaluated as per `file(Object)`.

    The returned file tree is lazy, so that it scans for files only when the contents of the file tree are queried. The file tree is also live, so that it scans for files each time the contents of the file tree are queried.

    Parameters:
      `baseDir` - The base directory of the file tree. Evaluated as per `file(Object)`.
    Returns:
      the file tree. Never returns null.

  - ### fileTree

    ConfigurableFileTree fileTree(Map<String,?> args)

    Creates a new `ConfigurableFileTree` using the provided map of arguments. The map will be applied as properties on the new file tree. Example:

         fileTree(dir:'src', excludes:['**/ignore/**','**/.svn/**'])

    The returned file tree is lazy, so that it scans for files only when the contents of the file tree are queried. The file tree is also live, so that it scans for files each time the contents of the file tree are queried.

    Parameters:
      `args` - map of property assignments to `ConfigurableFileTree` object
    Returns:
      the configured file tree. Never returns null.

  - ### fileTree

    ConfigurableFileTree fileTree(Object baseDir, Closure configureClosure)

    Creates a new `ConfigurableFileTree` using the given base directory. The given baseDir path is evaluated as per `file(Object)`. The closure will be used to configure the new file tree. The file tree is passed to the closure as its delegate. Example:

         fileTree('src') {
            exclude '**/.svn/**'
         }.copy { into 'dest'}

    The returned file tree is lazy, so that it scans for files only when the contents of the file tree are queried. The file tree is also live, so that it scans for files each time the contents of the file tree are queried.

    Parameters:
      `baseDir` - The base directory of the file tree. Evaluated as per `file(Object)`.
      `configureClosure` - Closure to configure the `ConfigurableFileTree` object.
    Returns:
      the configured file tree. Never returns null.

  - ### zipTree

    FileTree zipTree(Object zipPath)

    Creates a new `FileTree` which contains the contents of the given ZIP file. The given zipPath path is evaluated as per `file(Object)`. You can combine this method with the `copy(groovy.lang.Closure)` method to unzip a ZIP file.

    The returned file tree is lazy, so that it scans for files only when the contents of the file tree are queried. The file tree is also live, so that it scans for files each time the contents of the file tree are queried.

    Parameters:
      `zipPath` - The ZIP file. Evaluated as per `file(Object)`.
    Returns:
      the file tree. Never returns null.

  - ### tarTree

    FileTree tarTree(Object tarPath)
    Creates a new `FileTree` which contains the contents of the given TAR file. The given tarPath path can be:
    - an instance of `Resource`
    - any other object is evaluated as per `file(Object)`
    The returned file tree is lazy, so that it scans for files only when the contents of the file tree are queried. The file tree is also live, so that it scans for files each time the contents of the file tree are queried.

    Unless custom implementation of resources is passed, the tar tree attempts to guess the compression based on the file extension.

    You can combine this method with the `copy(groovy.lang.Closure)` method to untar a TAR file:

         task untar(type: Copy) {
           from tarTree('someCompressedTar.gzip')

           //tar tree attempts to guess the compression based on the file extension
           //however if you must specify the compression explicitly you can:
           from tarTree(resources.gzip('someTar.ext'))

           //in case you work with unconventionally compressed tars
           //you can provide your own implementation of a ReadableResource:
           //from tarTree(yourOwnResource as ReadableResource)

           into 'dest'
         }

    Parameters:
      `tarPath` - The TAR file or an instance of `Resource`.
    Returns:
      the file tree. Never returns null.

  - ### copy

    WorkResult copy(Closure closure)
    Copy the specified files. The given closure is used to configure a `CopySpec`, which is then used to copy the files. Example:

         copy {
            from configurations.runtimeClasspath
            into 'build/deploy/lib'
         }

    Note that CopySpecs can be nested:

         copy {
            into 'build/webroot'
            exclude '**/.svn/**'
            from('src/main/webapp') {
               include '**/*.jsp'
               filter(ReplaceTokens, tokens:[copyright:'2009', version:'2.3.1'])
            }
            from('src/main/js') {
               include '**/*.js'
            }
         }

    Parameters:
      `closure` - Closure to configure the CopySpec
    Returns:
      `WorkResult` that can be used to check if the copy did any work.

  - ### copySpec

    CopySpec copySpec(Closure closure)
    Creates a `CopySpec` which can later be used to copy files or create an archive. The given closure is used to configure the `CopySpec` before it is returned by this method.
    Parameters:
      `closure` - Closure to configure the CopySpec
    Returns:
      The CopySpec

  - ### mkdir

    File mkdir(Object path)
    Creates a directory and returns a file pointing to it.
    Parameters:
      `path` - The path for the directory to be created. Evaluated as per `file(Object)`.
    Returns:
      the created directory
    Throws:
      `InvalidUserDataException` - If the path points to an existing file.

  - ### delete

    boolean delete(Object... paths)
    Deletes files and directories.
    Parameters:
      `paths` - Any type of object accepted by `Project.files(Object...)`
    Returns:
      true if anything got deleted, false otherwise

  - ### javaexec

    @Deprecated ExecResult javaexec(@DelegatesTo(JavaExecSpec.class) Closure closure)
    Deprecated.
    Since 8.11. This method will be removed in Gradle 9.0. Use `ExecOperations.javaexec(Action)` or `ProviderFactory.javaexec(Action)` instead.
    Executes a Java main class. The closure configures a `JavaExecSpec`.
    Parameters:
      `closure` - The closure for configuring the execution.
    Returns:
      the result of the execution

  - ### javaexec

    @Deprecated ExecResult javaexec(Action<? super JavaExecSpec> action)
    Deprecated.
    Since 8.11. This method will be removed in Gradle 9.0. Use `ExecOperations.javaexec(Action)` or `ProviderFactory.javaexec(Action)` instead.
    Executes a Java main class.
    Parameters:
      `action` - The action for configuring the execution.
    Returns:
      the result of the execution

  - ### exec

    @Deprecated ExecResult exec(@DelegatesTo(ExecSpec.class) Closure closure)
    Deprecated.
    Since 8.11. This method will be removed in Gradle 9.0. Use `ExecOperations.exec(Action)` or `ProviderFactory.exec(Action)` instead.
    Executes an external command. The closure configures a `ExecSpec`.
    Parameters:
      `closure` - The closure for configuring the execution.
    Returns:
      the result of the execution

  - ### exec

    @Deprecated ExecResult exec(Action<? super ExecSpec> action)
    Deprecated.
    Since 8.11. This method will be removed in Gradle 9.0. Use `ExecOperations.exec(Action)` or `ProviderFactory.exec(Action)` instead.
    Executes an external command.
    Parameters:
      `action` - The action for configuring the execution.
    Returns:
      the result of the execution

  - ### getLogging

    LoggingManager getLogging()
    Returns the `LoggingManager` which can be used to receive logging and to control the standard output/error capture for this script. By default, System.out is redirected to the Gradle logging system at the QUIET log level, and System.err is redirected at the ERROR log level.
    Returns:
      the LoggingManager. Never returns null.

  - ### getLogger

    Logger getLogger()
    Returns the logger for this script. You can use this in your script to write log messages.
    Returns:
      The logger. Never returns null.

  - ### getResources

    ResourceHandler getResources()
    Provides access to resource-specific utility methods, for example factory methods that create various resources.
    Returns:
      Returned instance contains various resource-specific utility methods.

  - ### provider

    <T> Provider<T> provider(Callable<T> value)
    Creates a `Provider` implementation based on the provided value.
    Parameters:
      `value` - The `java.util.concurrent.Callable` use to calculate the value.
    Returns:
      The provider. Never returns null.
    Throws:
      `InvalidUserDataException` - If the provided value is null.
    Since:
      4.0
    See Also:
      - `ProviderFactory.provider(Callable)`