Comparable<Task>
, ExtensionAware
ObjectFilesToBinary
AbstractArchiveTask
, AbstractCompile
, AbstractConfigurationReportTask
, AbstractCopyTask
, AbstractDependencyReportTask
, AbstractExecTask
, AbstractLinkTask
, AbstractNativeCompileTask
, AbstractNativePCHCompileTask
, AbstractNativeSourceCompileTask
, AbstractPublishToMaven
, AbstractReportTask
, AbstractScalaCompile
, org.gradle.api.internal.AbstractTask
, AbstractTestTask
, AntlrTask
, AntTarget
, Assemble
, BuildEnvironmentReportTask
, CCompile
, Checkstyle
, CodeNarc
, ComponentReport
, ConventionReportTask
, org.gradle.api.internal.ConventionTask
, Copy
, CppCompile
, CppPreCompiledHeaderCompile
, CPreCompiledHeaderCompile
, CreateStartScripts
, CreateStartScripts
, CreateStaticLibrary
, DefaultTask
, Delete
, DependencyInsightReportTask
, DependencyReportTask
, DependentComponentsReport
, Ear
, Exec
, ExtractSymbols
, GenerateBuildDashboard
, GenerateCUnitLauncher
, GenerateEclipseClasspath
, GenerateEclipseJdt
, GenerateEclipseProject
, GenerateEclipseWtpComponent
, GenerateEclipseWtpFacet
, GenerateFiltersFileTask
, GenerateIdeaModule
, GenerateIdeaProject
, GenerateIdeaWorkspace
, GenerateIvyDescriptor
, GenerateMavenPom
, GenerateModuleMetadata
, GeneratePluginDescriptors
, GenerateProjectFileTask
, GenerateSchemeFileTask
, GenerateSolutionFileTask
, GenerateSwiftPackageManagerManifest
, GenerateWorkspaceSettingsFileTask
, GenerateXcodeProjectFileTask
, GenerateXcodeWorkspaceFileTask
, GeneratorTask
, GradleBuild
, GroovyCompile
, Groovydoc
, HtmlDependencyReportTask
, InitBuild
, InstallExecutable
, InstallXCTestBundle
, JacocoBase
, JacocoCoverageVerification
, JacocoMerge
, JacocoReport
, JacocoReportBase
, Jar
, Jar
, JavaCompile
, Javadoc
, JavaExec
, LinkExecutable
, LinkMachOBundle
, LinkSharedLibrary
, ModelReport
, ObjectiveCCompile
, ObjectiveCppCompile
, ObjectiveCppPreCompiledHeaderCompile
, ObjectiveCPreCompiledHeaderCompile
, OutgoingVariantsReportTask
, PluginUnderTestMetadata
, Pmd
, PrefixHeaderFileGenerateTask
, ProcessResources
, ProjectBasedReportTask
, ProjectReportTask
, PropertiesGeneratorTask
, PropertyListGeneratorTask
, PropertyReportTask
, PublishToIvyRepository
, PublishToMavenLocal
, PublishToMavenRepository
, ResolvableConfigurationsReportTask
, RunTestExecutable
, ScalaCompile
, ScalaDoc
, Sign
, SourceTask
, StripSymbols
, SwiftCompile
, Sync
, Tar
, TaskReportTask
, Test
, TestReport
, UnexportMainSymbol
, Upload
, ValidatePlugins
, War
, WindowsResourceCompile
, Wrapper
, WriteProperties
, XCTest
, XmlGeneratorTask
, Zip
public interface Task extends Comparable<Task>, ExtensionAware
A Task
represents a single atomic piece of work for a build, such as compiling classes or generating
javadoc.
Each task belongs to a Project
. You can use the various methods on TaskContainer
to create and lookup task instances. For example, TaskContainer.create(String)
creates an empty task with the given name. You can also use the
task
keyword in your build file:
task myTask task 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 qualified path, which is unique across all tasks in all projects. The path is the concatenation of the owning project's path and the task's name. Path elements are separated using the ":" character.
A Task
is made up of a sequence of Action
objects. When the task is executed, each of the
actions is executed in turn, by calling Action.execute(T)
. You can add actions to a task by calling doFirst(Action)
or doLast(Action)
.
Groovy closures can also be used to provide a task action. When the action is executed, the closure is called with
the task as parameter. You can add action closures to a task by calling doFirst(groovy.lang.Closure)
or
doLast(groovy.lang.Closure)
.
There are 2 special exceptions which a task action can throw to abort execution and continue without failing the
build. A task action can abort execution of the action and continue to the next action of the task by throwing a
StopActionException
. A task action can abort execution of the task and continue to the
next task by throwing a StopExecutionException
. Using these exceptions allows you to
have precondition actions which skip execution of the task, or part of the task, if not true.
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 after all of its dependencies and any "must run after" tasks have been executed.
Dependencies to a task are controlled using dependsOn(Object...)
or setDependsOn(Iterable)
,
and mustRunAfter(Object...)
, setMustRunAfter(Iterable)
, shouldRunAfter(Object...)
and
setShouldRunAfter(Iterable)
are used to specify ordering between tasks. You can use objects of any of
the following types to specify dependencies and ordering:
String
, CharSequence
or groovy.lang.GString
task path or name. A relative path is interpreted relative to the task's Project
. This
allows you to refer to tasks in other projects. These task references will not cause task creation.Task
.TaskDependency
object.TaskReference
object.Buildable
object.RegularFileProperty
or DirectoryProperty
.Provider
object. May contain any of the types listed here.Iterable
, Collection
, Map
or array. May contain any of the types listed here. The elements of the
iterable/collection/map/array are recursively converted to tasks.Callable
. The call()
method may return any of the types listed here. Its return value is
recursively converted to tasks. A null
return value is treated as an empty collection.Closure
or Kotlin function. The closure may take a Task
as parameter.
The closure or function may return any of the types listed here. Its return value is
recursively converted to tasks. A null
return value is treated as an empty collection.A Task
has 4 'scopes' for properties. You can access these properties by name from the build file or by
calling the property(String)
method. You can change the value of these properties by calling the setProperty(String, Object)
method.
Task
object itself. This includes any property getters and setters declared by the Task
implementation class. The properties of this scope are readable or writable based on the presence of the
corresponding getter and setter methods.Convention
object. The properties of this scope may be readable or writable, depending on the convention objects.A Plugin
may add methods to a Task
using its Convention
object.
By default, tasks are not executed in parallel unless a task is waiting on asynchronous work and another task (which
is not dependent) is ready to execute.
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.
Modifier and Type | Interface | Description |
---|---|---|
static class |
Task.Namer |
Modifier and Type | Field | Description |
---|---|---|
static String |
TASK_ACTION |
|
static String |
TASK_CONSTRUCTOR_ARGS |
Constructor arguments for the Task
|
static String |
TASK_DEPENDS_ON |
|
static String |
TASK_DESCRIPTION |
|
static String |
TASK_GROUP |
|
static String |
TASK_NAME |
|
static String |
TASK_OVERWRITE |
|
static String |
TASK_TYPE |
Modifier and Type | Method | Description |
---|---|---|
Task |
configure(Closure configureClosure) |
Applies the statements of the closure against this task object.
|
Task |
dependsOn(Object... paths) |
Adds the given dependencies to this task.
|
Task |
doFirst(Closure action) |
Adds the given closure to the beginning of this task's action list.
|
Task |
doFirst(String actionName,
Action<? super Task> action) |
Adds the given
Action to the beginning of this task's action list. |
Task |
doFirst(Action<? super Task> action) |
Adds the given
Action to the beginning of this task's action list. |
Task |
doLast(Closure action) |
Adds the given closure to the end of this task's action list.
|
Task |
doLast(String actionName,
Action<? super Task> action) |
Adds the given
Action to the end of this task's action list. |
Task |
doLast(Action<? super Task> action) |
Adds the given
Action to the end of this task's action list. |
void |
doNotTrackState(String reasonNotToTrackState) |
Do not track the state of the task.
|
Task |
finalizedBy(Object... paths) |
Adds the given finalizer tasks for this task.
|
List<Action<? super Task>> |
getActions() |
Returns the sequence of
Action objects which will be executed by this task, in the order of
execution. |
AntBuilder |
getAnt() |
Returns the
AntBuilder for this task. |
Convention |
getConvention() |
Deprecated.
The concept of conventions is deprecated.
|
Set<Object> |
getDependsOn() |
Returns the dependencies of this task.
|
String |
getDescription() |
Returns the description of this task.
|
TaskDestroyables |
getDestroyables() |
Returns the destroyables of this task.
|
boolean |
getDidWork() |
Checks if the task actually did any work.
|
boolean |
getEnabled() |
Returns if this task is enabled or not.
|
TaskDependency |
getFinalizedBy() |
Returns tasks that finalize this task.
|
String |
getGroup() |
Returns the task group which this task belongs to.
|
TaskInputs |
getInputs() |
Returns the inputs of this task.
|
TaskLocalState |
getLocalState() |
Returns the local state of this task.
|
Logger |
getLogger() |
Returns the logger for this task.
|
LoggingManager |
getLogging() |
Returns the
LoggingManager which can be used to receive logging and to control the
standard output/error capture for this task. |
TaskDependency |
getMustRunAfter() |
Returns tasks that this task must run after.
|
String |
getName() |
Returns the name of this task.
|
TaskOutputs |
getOutputs() |
Returns the outputs of this task.
|
String |
getPath() |
Returns the path of the task, which is a fully qualified name for the task.
|
Project |
getProject() |
Returns the
Project which this task belongs to. |
TaskDependency |
getShouldRunAfter() |
Returns tasks that this task should run after.
|
TaskState |
getState() |
Returns the execution state of this task.
|
TaskDependency |
getTaskDependencies() |
Returns a
TaskDependency which contains all the tasks that this task depends on. |
File |
getTemporaryDir() |
Returns a directory which this task can use to write temporary files to.
|
Property<Duration> |
getTimeout() |
The timeout of this task.
|
boolean |
hasProperty(String propertyName) |
Determines if this task has the given property.
|
Task |
mustRunAfter(Object... paths) |
Specifies that this task must run after all of the supplied tasks.
|
void |
notCompatibleWithConfigurationCache(String reason) |
Specifies that this task is not compatible with the configuration cache.
|
void |
onlyIf(Closure onlyIfClosure) |
Execute the task only if the given closure returns true.
|
void |
onlyIf(Spec<? super Task> onlyIfSpec) |
Execute the task only if the given spec is satisfied.
|
Object |
property(String propertyName) |
Returns the value of the given property of this task.
|
void |
setActions(List<Action<? super Task>> actions) |
Sets the sequence of
Action objects which will be executed by this task. |
void |
setDependsOn(Iterable<?> dependsOnTasks) |
Sets the dependencies of this task.
|
void |
setDescription(String description) |
Sets a description for this task.
|
void |
setDidWork(boolean didWork) |
Sets whether the task actually did any work.
|
void |
setEnabled(boolean enabled) |
Set the enabled state of a task.
|
void |
setFinalizedBy(Iterable<?> finalizedBy) |
Specifies the set of finalizer tasks for this task.
|
void |
setGroup(String group) |
Sets the task group which this task belongs to.
|
void |
setMustRunAfter(Iterable<?> mustRunAfter) |
Specifies the set of tasks that this task must run after.
|
void |
setOnlyIf(Closure onlyIfClosure) |
Execute the task only if the given closure returns true.
|
void |
setOnlyIf(Spec<? super Task> onlyIfSpec) |
Execute the task only if the given spec is satisfied.
|
void |
setProperty(String name,
Object value) |
Sets a property of this task.
|
void |
setShouldRunAfter(Iterable<?> shouldRunAfter) |
Specifies the set of tasks that this task should run after.
|
TaskDependency |
shouldRunAfter(Object... paths) |
Specifies that this task should run after all of the supplied tasks.
|
void |
usesService(Provider<? extends BuildService<?>> service) |
Registers a
BuildService that is used by this task so
its constraint on parallel execution can be honored. |
compareTo
getExtensions
static final String TASK_NAME
static final String TASK_DESCRIPTION
static final String TASK_GROUP
static final String TASK_TYPE
static final String TASK_DEPENDS_ON
static final String TASK_OVERWRITE
static final String TASK_ACTION
static final String TASK_CONSTRUCTOR_ARGS
@Internal String getName()
Returns the name of this task. The name uniquely identifies the task within its Project
.
@Internal Project getProject()
Returns the Project
which this task belongs to.
Calling this method from a task action is not supported when configuration caching is enabled.
@Internal List<Action<? super Task>> getActions()
Returns the sequence of Action
objects which will be executed by this task, in the order of
execution.
void setActions(List<Action<? super Task>> actions)
Sets the sequence of Action
objects which will be executed by this task.
actions
- The actions.@Internal TaskDependency getTaskDependencies()
Returns a TaskDependency
which contains all the tasks that this task depends on.
Calling this method from a task action is not supported when configuration caching is enabled.
@Internal Set<Object> getDependsOn()
Returns the dependencies of this task.
void setDependsOn(Iterable<?> dependsOnTasks)
Sets the dependencies of this task. See here for a description of the types of objects which can be used as task dependencies.
dependsOnTasks
- The set of task paths.Task dependsOn(Object... 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.
paths
- The dependencies to add to this task.void onlyIf(Closure onlyIfClosure)
Execute the task only if the given closure returns true. The closure will be evaluated at task execution time, not during configuration. The closure will be passed a single parameter, this task. If the closure returns false, 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 { isProductionEnvironment() }
onlyIfClosure
- code to execute to determine if task should be run@Incubating void doNotTrackState(String reasonNotToTrackState)
UntrackedTask
@Incubating void notCompatibleWithConfigurationCache(String reason)
Configuration cache problems found in the task will be reported but won't cause the build to fail.
The presence of incompatible tasks in the task graph will cause the configuration state to be discarded
at the end of the build unless the global configuration-cache-problems
option is set to warn
,
in which case the configuration state would still be cached in a best-effort manner as usual for the option.
IMPORTANT: This setting doesn't affect how Gradle treats problems found in other tasks also present in the task graph and those could still cause the build to fail.
void onlyIf(Spec<? super Task> onlyIfSpec)
Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, not during 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 isProductionEnvironment(); } });
onlyIfSpec
- specifies if a task should be runvoid setOnlyIf(Closure onlyIfClosure)
Execute the task only if the given closure returns true. The closure will be evaluated at task execution time, not during configuration. The closure will be passed a single parameter, this task. If the closure returns false, the task will be skipped.
The given predicate replaces all such predicates for this task.
onlyIfClosure
- code to execute to determine if task should be runvoid setOnlyIf(Spec<? super Task> onlyIfSpec)
Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, not during configuration. If the Spec is not satisfied, the task will be skipped.
The given predicate replaces all such predicates for this task.
onlyIfSpec
- specifies if a task should be run@Internal TaskState getState()
void setDidWork(boolean didWork)
didWork
- indicates if the task did any work@Internal boolean getDidWork()
Checks if the task actually did any work. Even if a Task executes, it may determine that it has nothing to do. For example, a compilation task may determine that source files have not changed since the last time a the task was run.
@Internal String getPath()
Returns the path of the task, which is a fully qualified name for the task. The path of a task is the path of
its Project
plus the name of the task, separated by :
.
Task doFirst(Action<? super Task> action)
Adds the given Action
to the beginning of this task's action list.
action
- The action to addTask doFirst(Closure 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.
action
- The action closure to execute.Task doFirst(String actionName, Action<? super Task> action)
Adds the given Action
to the beginning of this task's action list.
actionName
- An arbitrary string that is used for logging.action
- The action to addTask doLast(Action<? super Task> action)
Adds the given Action
to the end of this task's action list.
action
- The action to add.Task doLast(String actionName, Action<? super Task> action)
Adds the given Action
to the end of this task's action list.
actionName
- An arbitrary string that is used for logging.action
- The action to add.Task doLast(Closure action)
Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.
action
- The action closure to execute.@Internal boolean getEnabled()
Returns if this task is enabled or not.
setEnabled(boolean)
void setEnabled(boolean enabled)
Set the enabled state of a task. If a task is disabled none of the its actions are executed. Note that disabling a task does not prevent the execution of the tasks which this task depends on.
enabled
- The enabled state of this task (true or false)Task configure(Closure configureClosure)
Applies the statements of the closure against this task object. The delegate object for the closure is set to this task.
configureClosure
- The closure to be applied (can be null).@Internal AntBuilder getAnt()
Returns the AntBuilder
for this task. You can use this in your build file to execute ant
tasks.
AntBuilder
@Internal Logger getLogger()
Returns the logger for this task. You can use this in your build file to write log messages.
@Internal LoggingManager getLogging()
LoggingManager
which can be used to receive logging and to control the
standard output/error capture for this task. 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.@Nullable Object property(String propertyName) throws MissingPropertyException
Returns the value of the given property of this task. This method locates a property as follows:
MissingPropertyException
propertyName
- The name of the property.MissingPropertyException
- When the given property is unknown.boolean hasProperty(String propertyName)
Determines if this task has the given property. See here for details of the properties which are available for a task.
propertyName
- The name of the property to locate.void setProperty(String name, Object value) throws MissingPropertyException
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.
enabled
project property.MissingPropertyException
is thrown.name
- The name of the propertyvalue
- The value of the propertyMissingPropertyException
@Internal @Deprecated Convention getConvention()
Returns the Convention
object for this task. A Plugin
can use the convention object to
contribute properties and methods to this task.
ExtensionAware.getExtensions()
@Internal @Nullable String getDescription()
void setDescription(@Nullable String description)
gradle tasks
is called.description
- The description of the task. Might be null.@Internal @Nullable String getGroup()
void setGroup(@Nullable String group)
group
- The task group for this task. Can be null.@Internal TaskInputs getInputs()
Returns the inputs of this task.
@Internal TaskOutputs getOutputs()
Returns the outputs of this task.
@Internal TaskDestroyables getDestroyables()
Returns the destroyables of this task.
@Internal TaskLocalState getLocalState()
@Internal File getTemporaryDir()
Returns a directory which this task can use to write temporary files to. Each task instance is provided with a separate temporary directory. There are no guarantees that the contents of this directory will be kept beyond the execution of the task.
Task mustRunAfter(Object... paths)
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.
See here for a description of the types of objects which can be used to specify an ordering relationship.
paths
- The tasks this task must run after.void setMustRunAfter(Iterable<?> mustRunAfter)
Specifies the set of tasks that this task must run after.
task taskY { mustRunAfter = ["taskX1", "taskX2"] }
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.
See here for a description of the types of objects which can be used to specify an ordering relationship.
mustRunAfter
- The set of task paths this task must run after.@Internal TaskDependency getMustRunAfter()
Returns tasks that this task must run after.
Task finalizedBy(Object... paths)
Adds the given finalizer tasks for this task.
task taskY { finalizedBy "taskX" }
See here for a description of the types of objects which can be used to specify a finalizer task.
paths
- The tasks that finalize this task.void setFinalizedBy(Iterable<?> finalizedBy)
Specifies the set of finalizer tasks for this task.
task taskY { finalizedBy = ["taskX1", "taskX2"] }
See here for a description of the types of objects which can be used to specify a finalizer task.
finalizedBy
- The tasks that finalize this task.@Internal TaskDependency getFinalizedBy()
Returns tasks that finalize this task.
TaskDependency shouldRunAfter(Object... paths)
Specifies that this task should run after all of the supplied tasks.
task taskY { shouldRunAfter "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.
See here for a description of the types of objects which can be used to specify an ordering relationship.
paths
- The tasks this task should run after.void setShouldRunAfter(Iterable<?> shouldRunAfter)
Specifies the set of tasks that this task should run after.
task taskY { shouldRunAfter = ["taskX1", "taskX2"] }
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.
See here for a description of the types of objects which can be used to specify an ordering relationship.
shouldRunAfter
- The set of task paths this task should run after.@Internal TaskDependency getShouldRunAfter()
Returns tasks that this task should run after.
@Internal Property<Duration> getTimeout()
The timeout of this task.
task myTask { timeout = Duration.ofMinutes(10) }
The Thread executing this task will be interrupted if the task takes longer than the specified amount of time to run. In order for a task to work properly with this feature, it needs to react to interrupts and must clean up any resources it opened.
By default, tasks never time out.
@Incubating void usesService(Provider<? extends BuildService<?>> service)
BuildService
that is used by this task so
its constraint on parallel execution
can be honored.service
- The service provider.