Interface Task

    • Method Detail

      • getName

        @Internal
        java.lang.String getName()

        Returns the name of this task. The name uniquely identifies the task within its Project.

        Specified by:
        getName in interface Named
        Returns:
        The name of the task. Never returns null.
      • getProject

        @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.

        Returns:
        The project this task belongs to. Never returns null.
      • getActions

        @Internal
        java.util.List<Action<? super Task>> getActions()

        Returns the sequence of Action objects 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.
      • setActions

        void setActions​(java.util.List<Action<? super Task>> actions)

        Sets the sequence of Action objects which will be executed by this task.

        Parameters:
        actions - The actions.
      • getTaskDependencies

        @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.

        Returns:
        The dependencies of this task. Never returns null.
      • getDependsOn

        @Internal
        java.util.Set<java.lang.Object> getDependsOn()

        Returns the dependencies of this task.

        Returns:
        The dependencies of this task. Returns an empty set if this task has no dependencies.
      • setDependsOn

        void setDependsOn​(java.lang.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.

        Parameters:
        dependsOnTasks - The set of task paths.
      • dependsOn

        Task dependsOn​(java.lang.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.

        Parameters:
        paths - The dependencies to add to this task.
        Returns:
        the task object this method is applied to
      • onlyIf

        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() }

        Parameters:
        onlyIfClosure - code to execute to determine if task should be run
      • doNotTrackState

        void doNotTrackState​(java.lang.String reasonNotToTrackState)
        Do not track the state of the task.

        Instructs Gradle to treat the task as untracked.

        Since:
        7.3
        See Also:
        UntrackedTask
      • notCompatibleWithConfigurationCache

        void notCompatibleWithConfigurationCache​(java.lang.String reason)
        Specifies 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-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.

        Since:
        7.4
      • onlyIf

        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();
            }
         });
         
        Parameters:
        onlyIfSpec - specifies if a task should be run
      • onlyIf

        @Incubating
        void onlyIf​(java.lang.String onlyIfReason,
                    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("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
      • setOnlyIf

        void 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.

        Parameters:
        onlyIfClosure - code to execute to determine if task should be run
      • setOnlyIf

        void 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.

        Parameters:
        onlyIfSpec - specifies if a task should be run
      • setOnlyIf

        @Incubating
        void setOnlyIf​(java.lang.String onlyIfReason,
                       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.

        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
      • getState

        @Internal
        TaskState getState()
        Returns 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.
      • setDidWork

        void 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
      • getDidWork

        @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.

        Returns:
        true if this task did any work
      • getPath

        @Internal
        java.lang.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 :.

        Returns:
        the path of the task, which is equal to the path of the project plus the name of the task.
      • doFirst

        Task doFirst​(Action<? super Task> action)

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

        Parameters:
        action - The action to add
        Returns:
        the task object this method is applied to
      • doFirst

        Task doFirst​(@DelegatesTo(Task.class)
                     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.

        Parameters:
        action - The action closure to execute.
        Returns:
        This task.
      • doFirst

        Task doFirst​(java.lang.String actionName,
                     Action<? super Task> action)

        Adds the given Action to 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
      • doLast

        Task doLast​(Action<? super Task> action)

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

        Parameters:
        action - The action to add.
        Returns:
        the task object this method is applied to
      • doLast

        Task doLast​(java.lang.String actionName,
                    Action<? super Task> action)

        Adds the given Action to 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
      • doLast

        Task doLast​(@DelegatesTo(Task.class)
                    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.

        Parameters:
        action - The action closure to execute.
        Returns:
        This task.
      • setEnabled

        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.

        Parameters:
        enabled - The enabled state of this task (true or false)
      • configure

        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.

        Parameters:
        configureClosure - The closure to be applied (can be null).
        Returns:
        This task
      • getAnt

        @Internal
        AntBuilder getAnt()

        Returns the AntBuilder for this task. You can use this in your build file to execute ant tasks.

        Returns:
        The AntBuilder
      • getLogger

        @Internal
        Logger getLogger()

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

        Returns:
        The logger. Never returns null.
      • getLogging

        @Internal
        LoggingManager getLogging()
        Returns the 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.
        Returns:
        the LoggingManager. Never returns null.
      • property

        @Nullable
        java.lang.Object property​(java.lang.String propertyName)
                           throws MissingPropertyException

        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, 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.
      • hasProperty

        boolean hasProperty​(java.lang.String propertyName)

        Determines 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.
      • setProperty

        void setProperty​(java.lang.String name,
                         java.lang.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.

        1. The task object itself. For example, the enabled project property.
        2. The task's convention object.
        3. The task's extra properties.
        If the property is not found, a MissingPropertyException is thrown.
        Parameters:
        name - The name of the property
        value - The value of the property
        Throws:
        MissingPropertyException
      • getConvention

        @Internal
        @Deprecated
        Convention getConvention()
        Deprecated.
        The concept of conventions is deprecated. Use extensions if possible.

        Returns the Convention object for this task. A Plugin can use the convention object to contribute properties and methods to this task.

        Returns:
        The convention object. Never returns null.
        See Also:
        ExtensionAware.getExtensions()
      • getDescription

        @Internal
        @Nullable
        java.lang.String getDescription()
        Returns the description of this task.
        Returns:
        the description. May return null.
      • setDescription

        void setDescription​(@Nullable
                            java.lang.String description)
        Sets a description for this task. This should describe what the task does to the user of the build. The description will be displayed when gradle tasks is called.
        Parameters:
        description - The description of the task. Might be null.
      • getGroup

        @Internal
        @Nullable
        java.lang.String getGroup()
        Returns 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.
      • setGroup

        void setGroup​(@Nullable
                      java.lang.String group)
        Sets 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.
      • getInputs

        @Internal
        TaskInputs getInputs()

        Returns the inputs of this task.

        Returns:
        The inputs. Never returns null.
      • getOutputs

        @Internal
        TaskOutputs getOutputs()

        Returns the outputs of this task.

        Returns:
        The outputs. Never returns null.
      • getDestroyables

        @Internal
        TaskDestroyables getDestroyables()

        Returns the destroyables of this task.

        Returns:
        The destroyables. Never returns null.
        Since:
        4.0
      • getTemporaryDir

        @Internal
        java.io.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.

        Returns:
        The directory. Never returns null. The directory will already exist.
      • mustRunAfter

        Task mustRunAfter​(java.lang.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.

        Parameters:
        paths - The tasks this task must run after.
        Returns:
        the task object this method is applied to
      • setMustRunAfter

        void setMustRunAfter​(java.lang.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.

        Parameters:
        mustRunAfter - The set of task paths this task must run after.
      • getMustRunAfter

        @Internal
        TaskDependency getMustRunAfter()

        Returns 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.
      • finalizedBy

        Task finalizedBy​(java.lang.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.

        Parameters:
        paths - The tasks that finalize this task.
        Returns:
        the task object this method is applied to
      • setFinalizedBy

        void setFinalizedBy​(java.lang.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.

        Parameters:
        finalizedBy - The tasks that finalize this task.
      • getFinalizedBy

        @Internal
        TaskDependency getFinalizedBy()

        Returns 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.
      • shouldRunAfter

        TaskDependency shouldRunAfter​(java.lang.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.

        Parameters:
        paths - The tasks this task should run after.
        Returns:
        the task object this method is applied to
      • setShouldRunAfter

        void setShouldRunAfter​(java.lang.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.

        Parameters:
        shouldRunAfter - The set of task paths this task should run after.
      • getShouldRunAfter

        @Internal
        TaskDependency getShouldRunAfter()

        Returns 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.
      • getTimeout

        @Internal
        Property<java.time.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.

        Since:
        5.0