Interface Task
- All Superinterfaces:
- Comparable<Task>,- ExtensionAware,- Named
- All Known Subinterfaces:
- ObjectFilesToBinary
- All Known Implementing Classes:
- AbstractArchiveTask,- AbstractCodeQualityTask,- AbstractCompile,- AbstractConfigurationReportTask,- AbstractCopyTask,- AbstractDependencyReportTask,- AbstractExecTask,- AbstractLinkTask,- AbstractNativeCompileTask,- AbstractNativePCHCompileTask,- AbstractNativeSourceCompileTask,- AbstractProjectBasedReportTask,- AbstractPublishToMaven,- AbstractReportTask,- AbstractScalaCompile,- org.gradle.api.internal.AbstractTask,- AbstractTestTask,- AntlrTask,- AntTarget,- ArtifactTransformsReportTask,- 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,- 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,- UpdateDaemonJvm,- Upload,- ValidatePlugins,- War,- WindowsResourceCompile,- Wrapper,- WriteProperties,- XCTest,- XmlGeneratorTask,- Zip
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.
Task Actions
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.
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 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:
- A String,CharSequenceorgroovy.lang.GStringtask path or name. A relative path is interpreted relative to the task'sProject. This allows you to refer to tasks in other projects. These task references will not cause task creation.
- A Task.
- A TaskDependencyobject.
- A TaskReferenceobject.
- A Buildableobject.
- A RegularFilePropertyorDirectoryProperty.
- A Providerobject. May contain any of the types listed here.
- A Iterable,Collection,Mapor array. May contain any of the types listed here. The elements of the iterable/collection/map/array are recursively converted to tasks.
- A Callable. Thecall()method may return any of the types listed here. Its return value is recursively converted to tasks. Anullreturn value is treated as an empty collection.
- A Groovy Closureor Kotlin function. The closure may take aTaskas parameter. The closure or function may return any of the types listed here. Its return value is recursively converted to tasks. Anullreturn value is treated as an empty collection.
- Anything else is treated as an error.
Using a Task in a Build File
Dynamic Properties
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.
- The Taskobject 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 the corresponding getter and setter methods.
- The extensions added to the task by plugins. Each extension is available as a read-only property with the same name as the extension.
- The convention properties added to the task by plugins. A plugin can add properties and methods to a task through
 the task's Conventionobject. The properties of this scope may be readable or writable, depending on the convention objects.
- The extra properties of the task. Each task object maintains a map of additional properties. These are arbitrary name -> value pairs which you can use to dynamically add properties to a task object. Once defined, the properties of this scope are readable and writable.
Dynamic Methods
A Plugin may add methods to a Task using its Convention object.
Parallel Execution
 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.
- 
Nested Class SummaryNested Classes
- 
Field SummaryFields
- 
Method SummaryModifier and TypeMethodDescriptionApplies the statements of the closure against this task object.Adds the given dependencies to this task.Adds the given closure to the beginning of this task's action list.Adds the givenActionto the beginning of this task's action list.Adds the givenActionto the beginning of this task's action list.Adds the given closure to the end of this task's action list.Adds the givenActionto the end of this task's action list.Adds the givenActionto the end of this task's action list.voiddoNotTrackState(String reasonNotToTrackState) Do not track the state of the task.finalizedBy(Object... paths) Adds the given finalizer tasks for this task.Returns the sequence ofActionobjects which will be executed by this task, in the order of execution.getAnt()Returns theAntBuilderfor this task.Deprecated.The concept of conventions is deprecated.Returns the dependencies of this task.Returns the description of this task.Returns the destroyables of this task.booleanChecks if the task actually did any work.booleanReturns if this task is enabled or not.Returns tasks that finalize this task.getGroup()Returns the task group which this task belongs to.Returns the inputs of this task.Returns the local state of this task.Returns the logger for this task.Returns theLoggingManagerwhich can be used to receive logging and to control the standard output/error capture for this task.Returns tasks that this task must run after.getName()Returns the name of this task.Returns the outputs of this task.getPath()Returns the path of the task, which is a fully qualified name for the task.Returns theProjectwhich this task belongs to.Returns tasks that this task should run after.getState()Returns the execution state of this task.Returns aTaskDependencywhich contains all the tasks that this task depends on.Returns a directory which this task can use to write temporary files to.The timeout of this task.booleanhasProperty(String propertyName) Determines if this task has the given property.mustRunAfter(Object... paths) Specifies that this task must run after all of the supplied tasks.voidSpecifies that this task is not compatible with the configuration cache.voidExecute the task only if the given closure returns true.voidExecute the task only if the given spec is satisfied.voidExecute the task only if the given spec is satisfied.Returns the value of the given property of this task.voidsetActions(List<Action<? super Task>> actions) Sets the sequence ofActionobjects which will be executed by this task.voidsetDependsOn(Iterable<?> dependsOnTasks) Sets the dependencies of this task.voidsetDescription(String description) Sets a description for this task.voidsetDidWork(boolean didWork) Sets whether the task actually did any work.voidsetEnabled(boolean enabled) Set the enabled state of a task.voidsetFinalizedBy(Iterable<?> finalizedBy) Specifies the set of finalizer tasks for this task.voidSets the task group which this task belongs to.voidsetMustRunAfter(Iterable<?> mustRunAfter) Specifies the set of tasks that this task must run after.voidExecute the task only if the given closure returns true.voidExecute the task only if the given spec is satisfied.voidExecute the task only if the given spec is satisfied.voidsetProperty(String name, Object value) Sets a property of this task.voidsetShouldRunAfter(Iterable<?> shouldRunAfter) Specifies the set of tasks that this task should run after.shouldRunAfter(Object... paths) Specifies that this task should run after all of the supplied tasks.voidusesService(Provider<? extends BuildService<?>> service) Registers aBuildServicethat is used by this task soits constraint on parallel executioncan be honored.Methods inherited from interface java.lang.ComparablecompareToMethods inherited from interface org.gradle.api.plugins.ExtensionAwaregetExtensions
- 
Field Details- 
TASK_NAME- See Also:
 
- 
TASK_DESCRIPTION- See Also:
 
- 
TASK_GROUP- See Also:
 
- 
TASK_TYPE- See Also:
 
- 
TASK_DEPENDS_ON- See Also:
 
- 
TASK_OVERWRITE- See Also:
 
- 
TASK_ACTION- See Also:
 
- 
TASK_CONSTRUCTOR_ARGSConstructor arguments for the Task- Since:
- 4.7
- See Also:
 
 
- 
- 
Method Details- 
getNameReturns the name of this task. The name uniquely identifies the task within its Project.
- 
getProjectReturns the Projectwhich this task belongs to.Calling this method from a task action is not supported when configuration caching is enabled. - Returns:
- The project this task belongs to. Never returns null.
 
- 
getActionsReturns the sequence of Actionobjects which will be executed by this task, in the order of execution.- Returns:
- The task actions in the order they are executed. Returns an empty list if this task has no actions.
 
- 
setActionsSets the sequence of Actionobjects which will be executed by this task.- Parameters:
- actions- The actions.
 
- 
getTaskDependenciesReturns a TaskDependencywhich contains all the tasks that this task depends on.Calling this method from a task action is not supported when configuration caching is enabled. - Returns:
- The dependencies of this task. Never returns null.
 
- 
getDependsOnReturns the dependencies of this task. - Returns:
- The dependencies of this task. Returns an empty set if this task has no dependencies.
 
- 
setDependsOnSets the dependencies of this task. See here for a description of the types of objects which can be used as task dependencies. - Parameters:
- dependsOnTasks- The set of task paths.
 
- 
dependsOnAdds the given dependencies to this task. See here for a description of the types of objects which can be used as task dependencies. - Parameters:
- paths- The dependencies to add to this task.
- Returns:
- the task object this method is applied to
 
- 
onlyIfExecute 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() }- Parameters:
- onlyIfClosure- code to execute to determine if task should be run
 
- 
doNotTrackStateDo not track the state of the task.Instructs Gradle to treat the task as untracked. - Since:
- 7.3
- See Also:
 
- 
notCompatibleWithConfigurationCacheSpecifies that this task is not compatible with the configuration cache.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-problemsoption is set towarn, 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. - Since:
- 7.4
 
- 
onlyIfExecute 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(); } });- Parameters:
- onlyIfSpec- specifies if a task should be run
 
- 
onlyIfExecute 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("run only in production environment", new Spec<Task>() { boolean isSatisfiedBy(Task task) { return isProductionEnvironment(); } });- Parameters:
- onlyIfReason- specifies the reason for a task to run, which is used for logging
- onlyIfSpec- specifies if a task should be run
- Since:
- 7.6
 
- 
setOnlyIfExecute 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. - Parameters:
- onlyIfClosure- code to execute to determine if task should be run
 
- 
setOnlyIfExecute 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. - Parameters:
- onlyIfSpec- specifies if a task should be run
 
- 
setOnlyIfExecute 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. - Parameters:
- onlyIfReason- specifies the reason for a task to run, which is used for logging
- onlyIfSpec- specifies if a task should be run
- Since:
- 7.6
 
- 
getStateReturns the execution state of this task. This provides information about the execution of this task, such as whether it has executed, been skipped, has failed, etc.- Returns:
- The execution state of this task. Never returns null.
 
- 
setDidWorkvoid setDidWork(boolean didWork) Sets whether the task actually did any work. Most built-in tasks will set this automatically, but it may be useful to manually indicate this for custom user tasks.- Parameters:
- didWork- indicates if the task did any work
 
- 
getDidWorkChecks 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. - Returns:
- true if this task did any work
 
- 
getPathReturns the path of the task, which is a fully qualified name for the task. The path of a task is the path of its Projectplus the name of the task, separated by:.- Returns:
- the path of the task, which is equal to the path of the project plus the name of the task.
 
- 
doFirstAdds the given Actionto the beginning of this task's action list.- Parameters:
- action- The action to add
- Returns:
- the task object this method is applied to
 
- 
doFirstAdds the given closure to the beginning of this task's action list. The closure is passed this task as a parameter when executed. - Parameters:
- action- The action closure to execute.
- Returns:
- This task.
 
- 
doFirstAdds the given Actionto the beginning of this task's action list.- Parameters:
- actionName- An arbitrary string that is used for logging.
- action- The action to add
- Returns:
- the task object this method is applied to
- Since:
- 4.2
 
- 
doLastAdds the given Actionto the end of this task's action list.- Parameters:
- action- The action to add.
- Returns:
- the task object this method is applied to
 
- 
doLastAdds the given Actionto the end of this task's action list.- Parameters:
- actionName- An arbitrary string that is used for logging.
- action- The action to add.
- Returns:
- the task object this method is applied to
- Since:
- 4.2
 
- 
doLastAdds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed. - Parameters:
- action- The action closure to execute.
- Returns:
- This task.
 
- 
getEnabledReturns if this task is enabled or not. - See Also:
 
- 
setEnabledvoid 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. - Parameters:
- enabled- The enabled state of this task (true or false)
 
- 
configureApplies the statements of the closure against this task object. The delegate object for the closure is set to this task. - Parameters:
- configureClosure- The closure to be applied (can be null).
- Returns:
- This task
 
- 
getAntReturns the AntBuilderfor this task. You can use this in your build file to execute ant tasks.- Returns:
- The AntBuilder
 
- 
getLoggerReturns the logger for this task. You can use this in your build file to write log messages. - Returns:
- The logger. Never returns null.
 
- 
getLoggingReturns theLoggingManagerwhich 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.- Returns:
- the LoggingManager. Never returns null.
 
- 
propertyReturns 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
 - Parameters:
- propertyName- The name of the property.
- Returns:
- The value of the property, possibly null.
- Throws:
- MissingPropertyException- When the given property is unknown.
 
- 
hasPropertyDetermines if this task has the given property. See here for details of the properties which are available for a task. - Parameters:
- propertyName- The name of the property to locate.
- Returns:
- True if this project has the given property, false otherwise.
 
- 
setPropertySets 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 enabledproject property.
- The task's convention object.
- The task's extra properties.
 MissingPropertyExceptionis thrown.- Parameters:
- name- The name of the property
- value- The value of the property
- Throws:
- MissingPropertyException
 
- The task object itself.  For example, the 
- 
getConventionDeprecated.The concept of conventions is deprecated. Use extensions if possible.Returns the Conventionobject for this task. APlugincan use the convention object to contribute properties and methods to this task.- Returns:
- The convention object. Never returns null.
- See Also:
 
- 
getDescriptionReturns the description of this task.- Returns:
- the description. May return null.
 
- 
setDescriptionSets a description for this task. This should describe what the task does to the user of the build. The description will be displayed whengradle tasksis called.- Parameters:
- description- The description of the task. Might be null.
 
- 
getGroupReturns the task group which this task belongs to. The task group is used in reports and user interfaces to group related tasks together when presenting a list of tasks to the user.- Returns:
- The task group for this task. Might be null.
 
- 
setGroupSets the task group which this task belongs to. The task group is used in reports and user interfaces to group related tasks together when presenting a list of tasks to the user.- Parameters:
- group- The task group for this task. Can be null.
 
- 
getInputsReturns the inputs of this task. - Returns:
- The inputs. Never returns null.
 
- 
getOutputsReturns the outputs of this task. - Returns:
- The outputs. Never returns null.
 
- 
getDestroyablesReturns the destroyables of this task. - Returns:
- The destroyables. Never returns null.
- Since:
- 4.0
 
- 
getLocalStateReturns the local state of this task.- Since:
- 4.3
 
- 
getTemporaryDirReturns 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. - Returns:
- The directory. Never returns null. The directory will already exist.
 
- 
mustRunAfterSpecifies 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. - Parameters:
- paths- The tasks this task must run after.
- Returns:
- the task object this method is applied to
 
- 
setMustRunAfterSpecifies 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. - Parameters:
- mustRunAfter- The set of task paths this task must run after.
 
- 
getMustRunAfterReturns tasks that this task must run after. - Returns:
- The tasks that this task must run after. Returns an empty set if this task has no tasks it must run after.
 
- 
finalizedByAdds 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. - Parameters:
- paths- The tasks that finalize this task.
- Returns:
- the task object this method is applied to
 
- 
setFinalizedBySpecifies 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. - Parameters:
- finalizedBy- The tasks that finalize this task.
 
- 
getFinalizedByReturns tasks that finalize this task. - Returns:
- The tasks that finalize this task. Returns an empty set if there are no finalising tasks for this task.
 
- 
shouldRunAfterSpecifies 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. - Parameters:
- paths- The tasks this task should run after.
- Returns:
- the task object this method is applied to
 
- 
setShouldRunAfterSpecifies 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. - Parameters:
- shouldRunAfter- The set of task paths this task should run after.
 
- 
getShouldRunAfterReturns tasks that this task should run after. - Returns:
- The tasks that this task should run after. Returns an empty set if this task has no tasks it must run after.
 
- 
getTimeoutThe 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. - Since:
- 5.0
 
- 
usesServiceRegisters aBuildServicethat is used by this task soits constraint on parallel executioncan be honored.This is not necessary for task properties declared as ServiceReferences.- Parameters:
- service- The service provider.
- Since:
- 6.1
- See Also:
 
 
- 
Named.Namer.INSTANCEinstead (sinceTasknow extendsNamed).