目录
Properties、Methods、Script blocks列表
每个任务都属于一个项目。您可以使用TaskContainer上的各种方法来创建和查找任务实例。TaskContainer.create(java.lang.String)使用给定名称创建一个空任务。您还可以在构建文件中使用task关键字:
task myTask
task myTask { configure closure }
task myTask(type: SomeType)
task myTask(type: SomeType) { configure closure }
每个任务都有一个名称,可用于引用其拥有项目中的任务,以及一个完全限定的路径,该路径在所有项目的所有任务中都是唯一的。路径是拥有项目路径和任务名称的串联。路径元素使用:字符分隔。
Task Actions
Task由一系列Action对象组成。执行任务时,通过调用Action.execute(T)依次执行每个操作。您可以通过调用Task.doFirst(org.gradle.api.Action)或Task.doLast(org.gradle.api.Action)向任务添加操作。
Groovy闭包还可用于提供任务action。action执行时,将使用task作为参数调用闭包。您可以通过调用Task.doFirst(groovy.lang.Closure)或Task.doLast(groovy.lang.Closure)向任务添加action闭包。
有两个特殊的例外,任务操作可以抛出中止执行并继续而不会使构建失败。action任务可以中止action的执行,并通过抛出StopActionException继续执行任务的下一个action。任务可以中止任务的执行,并通过抛出StopExecutionException继续执行下一个任务.
任务的依赖关系和任务排序
任务可能依赖于其他任务,或者可能被安排为始终在另一个任务之后运行。 Gradle确保在执行任务时遵守所有任务依赖性和排序规则,以便在所有依赖项和任何“必须运行”任务执行之后执行任务。
使用Task.dependsOn(java.lang.Object [])或Task.setDependsOn(java.lang.Iterable)和Task.mustRunAfter(java.lang.Object []),Task.setMustRunAfter(java), Task.shouldRunAfter(java.lang.Object[])
andTask.setShouldRunAfter(java.lang.Iterable)
来控制对任务的依赖性。您可以使用以下任何类型的对象来指定依赖关系和排序:
- String,CharSequence或groovy.lang.GString任务路径或名称。相对于任务的Project的相对路径被解析。这允许您引用其他项目中的任务。
- A Task.
- A closure.闭包可以将Task作为参数。它可能会返回此处列出的任何类型。其返回值以递归方式转换为任务。 null返回值被视为空集合。
- A
TaskDependency
object. - A
TaskReference
object. - A
Buildable
object. - A
RegularFileProperty
orDirectoryProperty
. - A
Provider
object. 可能包含此处列出的任何类型。 - A
Iterable
,Collection
,Map
or array. 能包含此处列出的任何类型。 iterable / collection / map / array的元素以递归方式转换为任务。 - A
Callable
.call()方法可以返回此处列出的任何类型。其返回值以递归方式转换为任务。 null返回值被视为空集合。 - Anything else is treated as a failure.
在构建文件中使用任务
Dynamic Properties
任务有4个“范围”属性。您可以从构建文件或通过调用Task.property(java.lang.String)方法按名称访问这些属性。您可以通过调用Task.setProperty(java.lang.String,java.lang.Object)方法来更改这些属性的值。
- Task对象本身。这包括Task实现类声明的任何属性getter和setter。根据相应的getter和setter方法的存在,此范围的属性是可读或可写的。
- 插件添加到任务的extensions 。每个extension都可以作为只读属性使用,其名称与extension的名称相同。
- 通过插件添加到任务的convention 属性。插件可以通过任务的Convention对象向任务添加属性和方法。此范围的属性可以是可读的或可写的,具体取决于Convention实现类。
- 任务的extra 属性。每个任务对象都维护一个附加属性集合的map。这些是任意的名称 - >值对,可用于动态地向任务对象添加属性。定义后,此作用域的属性是可读写的。
//Task`s method of property
Object property(String propertyName)
Returns the value of the given property of this task. This method locates a property as follows:
If this task object has a property with the given name, return the value of the property.
If this task has an extension with the given name, return the extension.
If this task's convention object has a property with the given name, return the value of the property.
If this task has an extra property with the given name, return the value of the property.
If not found, throw MissingPropertyException
//Task`s method of setProperty
void setProperty(String name, Object value)
Sets a property of this task. This method searches for a property with the given name in the following locations, and sets the property on the first location where it finds the property.
The task object itself. For example, the enabled project property.
The task's convention object.
The task's extra properties.
If the property is not found, a MissingPropertyException is thrown.
Dynamic Methods
插件可以使用其Convention对象向Task添加方法。
Parallel Execution
默认情况下,除非任务正在等待异步工作并且另一个任务(非依赖)已准备好执行,否则任务不会并行执行。启动构建时,可以通过--parallel标志启用并行执行。在并行模式中,不同项目的任务(即,在多项目构建中)能够并行执行。
Properties、Methods、Script blocks列表
Properties
Property | Description |
actions | The sequence of |
ant | The |
convention | The |
dependsOn | The dependencies of this task. |
description | The description of this task.............................................. |
Methods
Method | Description |
dependsOn(paths) | Adds the given dependencies to this task. See here for a description of the types of objects which can be used as task dependencies. |
doFirst(action) | Adds the given closure to the beginning of this task's action list. The closure is passed this task as a parameter when executed. |
doFirst(actionName, action) | incubating Adds the given |
doFirst(action) | Adds the given |
Script blocks
没有.