Script

API Documentation: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.

Properties

PropertyDescription
buildscript

The script handler for this script. You can use this handler to manage the classpath used to compile and execute this script.

logger

The logger for this script. You can use this in your script to write log messages.

logging

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.

resources

Provides access to resource-specific utility methods, for example factory methods that create various resources.

Methods

MethodDescription
apply(closure)

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

apply(options)

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

copy(closure)

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

copySpec(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.

delete(paths)

Deletes files and directories.

exec(closure)

Executes an external command. The closure configures a ExecSpec.

exec(action)

Executes an external command.

file(path)

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

file(path, validation)

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.

fileTree(baseDir)

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

fileTree(baseDir, configureClosure)

Creates a new ConfigurableFileTree using the given base directory. The given baseDir path is evaluated as per Script.file(java.lang.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(args)

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

files(paths, 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(java.lang.Object, groovy.lang.Closure). Relative paths are resolved relative to the directory containing this script.

files(paths)

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

javaexec(closure)

Executes a Java main class. The closure configures a JavaExecSpec.

javaexec(action)

Executes a Java main class.

mkdir(path)

Creates a directory and returns a file pointing to it.

relativePath(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 Script.file(java.lang.Object), from which a relative path is calculated.

tarTree(tarPath)

Creates a new FileTree which contains the contents of the given TAR file. The given tarPath path can be:

uri(path)

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

zipTree(zipPath)

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

Script blocks

BlockDescription
buildscript

Configures the classpath for this script.

Property details

ScriptHandler buildscript (read-only)

The script handler for this script. You can use this handler to manage the classpath used to compile and execute this script.

Logger logger (read-only)

The logger for this script. You can use this in your script to write log messages.

LoggingManager logging (read-only)

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.

ResourceHandler resources (read-only)

Provides access to resource-specific utility methods, for example factory methods that create various resources.

Method details

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.

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 Script.uri(java.lang.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.

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'
   }
}

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.

boolean delete(Object... paths)

Deletes files and directories.

ExecResult exec(Closure closure)

Executes an external command. The closure configures a ExecSpec.

ExecResult exec(Action<? super ExecSpec> action)

Executes an external command.

File file(Object path)

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

File file(Object path, PathValidation validation)

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.

ConfigurableFileTree fileTree(Object baseDir)

Creates a new ConfigurableFileTree using the given base directory. The given baseDir path is evaluated as per Script.file(java.lang.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.

ConfigurableFileTree fileTree(Object baseDir, Closure configureClosure)

Creates a new ConfigurableFileTree using the given base directory. The given baseDir path is evaluated as per Script.file(java.lang.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.

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.

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(java.lang.Object, groovy.lang.Closure). Relative paths are resolved relative to the directory containing this script.

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

ExecResult javaexec(Closure closure)

Executes a Java main class. The closure configures a JavaExecSpec.

ExecResult javaexec(Action<? super JavaExecSpec> action)

Executes a Java main class.

File mkdir(Object path)

Creates a directory and returns a file pointing to it.

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 Script.file(java.lang.Object), from which a relative path is calculated.

FileTree tarTree(Object tarPath)

Creates a new FileTree which contains the contents of the given TAR file. The given tarPath path can be:

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 Script.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'
}

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 Script.file(java.lang.Object), with the exception that any URI scheme is supported, not just 'file:' URIs.

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 Script.file(java.lang.Object). You can combine this method with the Script.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.

Script block details

buildscript { }

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.

Delegates to:
ScriptHandler from buildscript