Movatterモバイル変換


[0]ホーム

URL:


Task

API Documentation:Task

ATask represents a single atomic piece of work for a build, such as compiling classes or generatingjavadoc.

Each task belongs to aProject. You can use the various methods onTaskContainer to create and lookup task instances. For example,TaskContainer.create(java.lang.String) creates an empty task with the given name. You can also use thetask keyword in your build file:

task myTasktask myTask { configure closure }task myTask(type: SomeType)task myTask(type: SomeType) { configure closure }

Each task has a name, which can be used to refer to the task within its owning project, and a fully qualifiedpath, which is unique across all tasks in all projects. The path is the concatenation of the owning project's pathand the task's name. Path elements are separated using the:character.

Task Actions

ATask is made up of a sequence ofAction objects. When the task is executed, each of theactions is executed in turn, by callingAction.execute(T). You can add actions to a task by callingTask.doFirst(org.gradle.api.Action) orTask.doLast(org.gradle.api.Action).

Groovy closures can also be used to provide a task action. When the action is executed, the closure is called withthe task as parameter. You can add action closures to a task by callingTask.doFirst(groovy.lang.Closure) orTask.doLast(groovy.lang.Closure).

There are 2 special exceptions which a task action can throw to abort execution and continue without failing thebuild. A task action can abort execution of the action and continue to the next action of the task by throwing aStopActionException. A task action can abort execution of the task and continue to thenext task by throwing aStopExecutionException. Using these exceptions allows you tohave precondition actions which skip execution of the task, or part of the task, if not true.

Task Dependencies and Task Ordering

A task may have dependencies on other tasks or might be scheduled to always run after another task.Gradle ensures that all task dependencies and ordering rules are honored when executing tasks, so that the task is executed afterall of its dependencies and any "must run after" tasks have been executed.

Dependencies to a task are controlled usingTask.dependsOn(java.lang.Object[]) orTask.setDependsOn(java.lang.Iterable),andTask.mustRunAfter(java.lang.Object[]),Task.setMustRunAfter(java.lang.Iterable),Task.shouldRunAfter(java.lang.Object[]) andTask.setShouldRunAfter(java.lang.Iterable) are used to specify ordering between tasks. You can use objects of any ofthe following types to specify dependencies and ordering:

  • AString,CharSequence orgroovy.lang.GString task path or name. A relative path is interpreted relative to the task'sProject. Thisallows you to refer to tasks in other projects.
  • ATask.
  • A closure. The closure may take aTask as parameter. It may return any of the types listed here. Itsreturn value is recursively converted to tasks. Anull return value is treated as an empty collection.
  • ATaskDependency object.
  • ABuildable object.
  • AIterable,Collection,Map or array. May contain any of the types listed here. The elements of theiterable/collection/map/array are recursively converted to tasks.
  • ACallable. Thecall() method may return any of the types listed here. Its return value isrecursively converted to tasks. Anull return value is treated as an empty collection.

Using a Task in a Build File

Dynamic Properties

ATask has 4 'scopes' for properties. You can access these properties by name from the build file or bycalling theTask.property(java.lang.String) method. You can change the value of these properties by calling theTask.setProperty(java.lang.String, java.lang.Object) method.

  • TheTask object itself. This includes any property getters and setters declared by theTaskimplementation class. The properties of this scope are readable or writable based on the presence of thecorresponding getter and setter methods.
  • Theextensions added to the task by plugins. Each extension is available as a read-only property with the samename as the extension.
  • Theconvention properties added to the task by plugins. A plugin can add properties and methods to a task throughthe task'sConvention object. The properties of this scope may be readable or writable, depending on the convention objects.
  • Theextra properties of the task. Each task object maintains a map of additional properties. Theseare arbitrary name -> value pairs which you can use to dynamically add properties to a task object. Once defined, the propertiesof this scope are readable and writable.

Dynamic Methods

APlugin may add methods to aTask using itsConvention object.

Parallel Execution

By default, tasks are not executed in parallel.Parallel execution can be enabled by the--parallel flag when the build is initiated.In parallel mode, the tasks of different projects (i.e. in a multi project build) are able to be executed in parallel.If a task is annotated withParallelizableTask, it may also be executed in parallel with other tasks of the same project.SeeParallelizableTask for more details on writing parallelizable tasks.

Properties

PropertyDescription
actions

The sequence ofAction objects which will be executed by this task, in the order ofexecution.

ant

TheAntBuilder for this task. You can use this in your build file to execute anttasks.

convention

TheConvention object for this task. APlugin can use the convention object tocontribute properties and methods to this task.

dependsOn

The dependencies of this task.

description

The description of this task.

didWork

Checks if the task actually did any work. Even if a Task executes, it may determine that it has nothing todo. For example, a compilation task may determine that source files have not changed since the last time a thetask was run.

enabled

Returns if this task is enabled or not.

extensions

The container of extensions.

finalizedBy
Incubating

Returns tasks that finalize this task.

group

The task group which this task belongs to. The task group is used in reports and user interfaces togroup related tasks together when presenting a list of tasks to the user.

inputs

The inputs of this task.

logger

The logger for this task. You can use this in your build file to write log messages.

logging

TheLoggingManager which can be used to control the logging level andstandard output/error capture for this task. By default, System.out is redirected to the Gradle logging system atthe QUIET log level, and System.err is redirected at the ERROR log level.

mustRunAfter
Incubating

Returns tasks that this task must run after.

name

The name of this task. The name uniquely identifies the task within itsProject.

outputs

The outputs of this task.

path

The path of the task, which is a fully qualified name for the task. The path of a task is the path ofitsProject plus the name of the task, separated by:.

project

TheProject which this task belongs to.

state

The execution state of this task. This provides information about the execution of this task, such aswhether it has executed, been skipped, has failed, etc.

taskDependencies

Returns aTaskDependency which contains all the tasks that this task depends on.

temporaryDir

Returns a directory which this task can use to write temporary files to. Each task instance is provided with aseparate temporary directory. There are no guarantees that the contents of this directory will be kept beyond theexecution of the task.

Methods

MethodDescription
deleteAllActions()

Removes all the actions of this task.

dependsOn(paths)

Adds the given dependencies to this task. Seehere for a description of the typesof 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 aparameter when executed.

doFirst(action)

Adds the givenAction to the beginning of this task's action list.

doLast(action)

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameterwhen executed.

doLast(action)

Adds the givenAction to the end of this task's action list.

finalizedBy(paths)
Incubating

Adds the given finalizer tasks for this task.

hasProperty(propertyName)

Determines if this task has the given property. Seehere for details of theproperties which are available for a task.

leftShift(action)
Deprecated

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameterwhen executed. You can call this method from your build script using the << left shift operator.

mustRunAfter(paths)
Incubating

Specifies that this task must run after all of the supplied tasks.

onlyIf(onlyIfClosure)

Execute the task only if the given closure returns true. The closure will be evaluated at task executiontime, not during configuration. The closure will be passed a single parameter, this task. If the closure returnsfalse, the task will be skipped.

onlyIf(onlyIfSpec)

Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, notduring configuration. If the Spec is not satisfied, the task will be skipped.

property(propertyName)

Returns the value of the given property of this task. This method locates a property as follows:

setProperty(name, value)

Sets a property of this task. This method searches for a property with the given name in the followinglocations, and sets the property on the first location where it finds the property.

Script blocks

No script blocks

Property details

List<Action<? superTask>>actions

The sequence ofAction objects which will be executed by this task, in the order ofexecution.

AntBuilderant (read-only)

TheAntBuilder for this task. You can use this in your build file to execute anttasks.

Conventionconvention (read-only)

TheConvention object for this task. APlugin can use the convention object tocontribute properties and methods to this task.

Set<Object>dependsOn

The dependencies of this task.

Stringdescription

The description of this task.

booleandidWork

Checks if the task actually did any work. Even if a Task executes, it may determine that it has nothing todo. For example, a compilation task may determine that source files have not changed since the last time a thetask was run.

booleanenabled

Returns if this task is enabled or not.

ExtensionContainerextensions (read-only)

The container of extensions.

TaskDependencyfinalizedBy

Note: This property isincubating and may change in a future version of Gradle.

Returns tasks that finalize this task.

Stringgroup

The task group which this task belongs to. The task group is used in reports and user interfaces togroup related tasks together when presenting a list of tasks to the user.

TaskInputsinputs (read-only)

The inputs of this task.

Loggerlogger (read-only)

The logger for this task. You can use this in your build file to write log messages.

LoggingManagerlogging (read-only)

TheLoggingManager which can be used to control the logging level andstandard output/error capture for this task. By default, System.out is redirected to the Gradle logging system atthe QUIET log level, and System.err is redirected at the ERROR log level.

TaskDependencymustRunAfter

Note: This property isincubating and may change in a future version of Gradle.

Returns tasks that this task must run after.

Stringname (read-only)

The name of this task. The name uniquely identifies the task within itsProject.

TaskOutputsoutputs (read-only)

The outputs of this task.

Stringpath (read-only)

The path of the task, which is a fully qualified name for the task. The path of a task is the path ofitsProject plus the name of the task, separated by:.

Projectproject (read-only)

TheProject which this task belongs to.

TaskStatestate (read-only)

The execution state of this task. This provides information about the execution of this task, such aswhether it has executed, been skipped, has failed, etc.

TaskDependencytaskDependencies (read-only)

Returns aTaskDependency which contains all the tasks that this task depends on.

FiletemporaryDir (read-only)

Returns a directory which this task can use to write temporary files to. Each task instance is provided with aseparate temporary directory. There are no guarantees that the contents of this directory will be kept beyond theexecution of the task.

Method details

TaskdeleteAllActions()

Removes all the actions of this task.

TaskdependsOn(Object... paths)

Adds the given dependencies to this task. Seehere for a description of the typesof objects which can be used as task dependencies.

TaskdoFirst(Closure action)

Adds the given closure to the beginning of this task's action list. The closure is passed this task as aparameter when executed.

TaskdoFirst(Action<? superTask> action)

Adds the givenAction to the beginning of this task's action list.

TaskdoLast(Closure action)

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameterwhen executed.

TaskdoLast(Action<? superTask> action)

Adds the givenAction to the end of this task's action list.

TaskfinalizedBy(Object... paths)

Note: This method isincubating and may change in a future version of Gradle.

Adds the given finalizer tasks for this task.

task taskY {    finalizedBy"taskX"}

Seehere for a description of the types of objects which can be used to specifya finalizer task.

booleanhasProperty(String propertyName)

Determines if this task has the given property. Seehere for details of theproperties which are available for a task.

TaskleftShift(Closure action)

Note: This method isdeprecated and will be removed in the next major version of Gradle.

Adds the given closure to the end of this task's action list. The closure is passed this task as a parameterwhen executed. You can call this method from your build script using the << left shift operator.

TaskmustRunAfter(Object... paths)

Note: This method isincubating and may change in a future version of Gradle.

Specifies that this task must run after all of the supplied tasks.

task taskY {    mustRunAfter"taskX"}

For each supplied task, this action adds a task 'ordering', and does not specify a 'dependency' between the tasks.As such, it is still possible to execute 'taskY' without first executing the 'taskX' in the example.

Seehere for a description of the types of objects which can be used to specifyan ordering relationship.

voidonlyIf(Closure onlyIfClosure)

Execute the task only if the given closure returns true. The closure will be evaluated at task executiontime, not during configuration. The closure will be passed a single parameter, this task. If the closure returnsfalse, the task will be skipped.

You may add multiple such predicates. The task is skipped if any of the predicates return false.

Typical usage:myTask.onlyIf{ dependsOnTaskDidWork() }

voidonlyIf(Spec<? superTask> onlyIfSpec)

Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, notduring configuration. If the Spec is not satisfied, the task will be skipped.

You may add multiple such predicates. The task is skipped if any of the predicates return false.

Typical usage (from Java):

myTask.onlyIf(new Spec<Task>() {boolean isSatisfiedBy(Task task) {return task.dependsOnTaskDidWork();   }});

Objectproperty(String propertyName)

Returns the value of the given property of this task. This method locates a property as follows:

  1. If this task object has a property with the given name, return the value of the property.
  2. If this task has an extension with the given name, return the extension.
  3. If this task's convention object has a property with the given name, return the value of the property.
  4. If this task has an extra property with the given name, return the value of the property.
  5. If not found, throwMissingPropertyException

voidsetProperty(String name,Object value)

Sets a property of this task. This method searches for a property with the given name in the followinglocations, and sets the property on the first location where it finds the property.

  1. The task object itself. For example, theenabled project property.
  2. The task's convention object.
  3. The task's extra properties.

If the property is not found, aMissingPropertyException is thrown.


[8]ページ先頭

©2009-2025 Movatter.jp