API文档: | Script |
---|
该接口由所有 Gradle Groovy DSL 脚本实现,以添加一些特定于 Gradle 的方法。由于您编译的脚本类将实现此接口,因此您可以直接在脚本中使用此接口声明的方法和属性。
通常,一个Script
对象会附加一个委托对象。例如,构建脚本将附加一个Project
实例,初始化脚本将附加一个Gradle
实例。在此对象上找不到的任何属性引用或方法调用Script
都将转发到委托对象。
财产 | 描述 |
buildscript | 该脚本的脚本处理程序。您可以使用此处理程序来管理用于编译和执行此脚本的类路径。 |
logger | 该脚本的记录器。您可以在脚本中使用它来写入日志消息。 |
logging | 可 |
resources | 提供对特定于资源的实用程序方法的访问,例如创建各种资源的工厂方法。 |
方法 | 描述 |
apply(closure) | 使用插件或脚本为此脚本配置委托对象。 |
apply(options) | 使用插件或脚本为此脚本配置委托对象。 |
copy(closure) | 复制指定的文件。给定的闭包用于配置 a |
copySpec(closure) | 创建一个 |
delete(paths) | 删除文件和目录。 |
exec(closure) | 执行外部命令。该闭包配置了一个 |
exec(action) | 执行外部命令。 |
file(path) | 解析相对于包含此脚本的目录的文件路径。这按如下所述工作 |
file(path, validation) | 解析相对于包含此脚本的目录的文件路径,并使用给定的方案验证它。请参阅 |
fileTree(baseDir) |
|
fileTree(baseDir, configureClosure) |
|
fileTree(args) | Creates a new |
files(paths, configureClosure) | Creates a new |
files(paths) | Returns a |
javaexec(closure) | Executes a Java main class. The closure configures a |
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 |
tarTree(tarPath) | Creates a new |
uri(path) | Resolves a file path to a URI, relative to the directory containing this script. Evaluates the provided path
object as described for |
zipTree(zipPath) | Creates a new |
Block | Description |
buildscript | Configures the classpath for this script. |
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.
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.
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 byScript.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' } }
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)
Object
...Deletes files and directories.
ExecResult
exec
(Closure
closure)
Executes an external command. The closure configures a ExecSpec
.
ExecResult
exec
(Action
<? super ExecSpec
>
action)
Action
<? super ExecSpec
>Executes an external command.
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)
Map
<String
, ?>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.
ConfigurableFileCollection
files
(Object
...
paths)
Object
...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)
Action
<? super JavaExecSpec
>Executes a Java main class.
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.
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
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.
除非通过资源的自定义实现,否则 tar 树会尝试根据文件扩展名猜测压缩情况。
您可以将此方法与Script.copy(groovy.lang.Closure)
解压 TAR 文件的方法结合使用:
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(相对于包含此脚本的目录)。按照 中的描述评估提供的路径对象Script.file(java.lang.Object)
,但支持任何 URI 方案,而不仅仅是“file:”URI。
创建一个新文件FileTree
,其中包含给定 ZIP 文件的内容。给定的 zipPath 路径按照 进行评估Script.file(java.lang.Object)
。您可以将此方法与Script.copy(groovy.lang.Closure)
解压缩 ZIP 文件的方法结合起来。
返回的文件树是惰性的,因此仅当查询文件树的内容时才扫描文件。文件树也是活动的,因此每次查询文件树的内容时都会扫描文件。
配置此脚本的类路径。
给定的闭包是针对此脚本的ScriptHandler
.作为闭包的委托传递ScriptHandler
给闭包。