api / org.gradle.api

Package org.gradle.api

Types

Action

interface Action<T : Any>

Performs some action against objects of type T.

ActionConfiguration

interface ActionConfiguration

Allows specification of configuration for some action.

The configuration is represented using zero or more initialization parameters to use when constructing an instance of the implementation class. The following types are supported:

  • String
  • Boolean
  • Integer, Long, Short and other Number subtypes.
  • java.io.File
  • A java.util.List or java.util.Set of any supported type.
  • An array of any supported type.
  • A java.util.Map with keys and values of any supported type.
  • An Enum type.
  • A Named type created using org.gradle.api.model.ObjectFactory#named(Class, String).
  • Any serializable type.

AntBuilder

abstract class AntBuilder : AntBuilder

An AntBuilder allows you to use Ant from your build script.

Buildable

interface Buildable

A Buildable represents an artifact or set of artifacts which are built by one or more Task instances.

BuildableComponentSpec

interface BuildableComponentSpec : Buildable, ComponentSpec

A ComponentSpec that is directly Buildable via a specified task.

CheckableComponentSpec

interface CheckableComponentSpec : ComponentSpec

A ComponentSpec that is directly checkable via a specified task.

DefaultTask

open class DefaultTask : AbstractTask

DefaultTask is the standard Task implementation. You can extend this to implement your own task types.

Describable

interface Describable

Types can implement this interface when they provide a human-readable display name. It is strongly encouraged to compute this display name lazily: computing a display name, even if it's only a string concatenation, can take a significant amount of time during configuration for something that would only be used, typically, in error messages.

DomainObjectCollection

interface DomainObjectCollection<T : Any> : MutableCollection<T>

A DomainObjectCollection is a specialised Collection that adds the ability to receive modification notifications and use live filtered sub collections.

The filtered collections returned by the filtering methods, such as #matching(Closure), return collections that are live. That is, they reflect changes made to the source collection that they were created from. This is true for filtered collections made from filtered collections etc.

You can also add actions which are executed as elements are added to the collection. Actions added to filtered collections will be fired if an addition/removal occurs for the source collection that matches the filter.

DomainObjectSet

interface DomainObjectSet<T : Any> : DomainObjectCollection<T>, MutableSet<T>

A DomainObjectSet is a specialisation of DomainObjectCollection that guarantees Set semantics.

ExtensiblePolymorphicDomainObjectContainer

interface ExtensiblePolymorphicDomainObjectContainer<T : Any> : PolymorphicDomainObjectContainer<T>, NamedDomainObjectFactoryRegistry

A org.gradle.api.PolymorphicDomainObjectContainer that can be extended at runtime to create elements of new types.

JavaVersion

class JavaVersion

An enumeration of Java versions. Before 9: http://www.oracle.com/technetwork/java/javase/versioning-naming-139433.html 9+: http://openjdk.java.net/jeps/223

Named

interface Named

Types can implement this interface and use the embedded Namer implementation, to satisfy API that calls for a namer.

NamedDomainObjectCollection

interface NamedDomainObjectCollection<T : Any> : DomainObjectCollection<T>

A NamedDomainObjectCollection represents a collection of domain objects that have an inherent, constant, name.

Objects to be added to a named domain object collection must implement equals() in such a way that no two objects with different names are considered equal. That is, all equality tests must consider the name as an equality key. Behavior is undefined if two objects with different names are considered equal by their equals() implementation.

All implementations must guarantee that all elements in the collection are uniquely named. That is, an attempt to add an object with a name equal to the name of any existing object in the collection will fail. Implementations may choose to simply return false from add(T) or to throw an exception.

Objects in the collection are accessible as read-only properties, using the name of the object as the property name. For example (assuming the 'name' property provides the object name):

 books.add(new Book(name: "gradle", title: null)) books.gradle.title = "Gradle in Action" 

A dynamic method is added for each object which takes a configuration closure. This is equivalent to calling #getByName(String, groovy.lang.Closure). For example:

 books.add(new Book(name: "gradle", title: null)) books.gradle { title = "Gradle in Action" } 

You can also use the [] operator to access the objects of a collection by name. For example:

 books.add(new Book(name: "gradle", title: null)) books['gradle'].title = "Gradle in Action" 

Rule objects can be attached to the collection in order to respond to requests for objects by name where no object with name exists in the collection. This mechanism can be used to create objects on demand. For example:

 books.addRule('create any') { books.add(new Book(name: "gradle", title: null)) } books.gradle.name == "gradle" 

NamedDomainObjectContainer

interface NamedDomainObjectContainer<T : Any> : NamedDomainObjectSet<T>, Configurable

A named domain object container is a specialisation of NamedDomainObjectSet that adds the ability to create instances of the element type.

Implementations may use different strategies for creating new object instances.

Note that a container is an implementation of java.util.SortedSet, which means that the container is guaranteed to only contain elements with unique names within this container. Furthermore, items are ordered by their name.

NamedDomainObjectFactory

interface NamedDomainObjectFactory<T : Any>

A factory for named objects of type T.

NamedDomainObjectList

interface NamedDomainObjectList<T : Any> : NamedDomainObjectCollection<T>, MutableList<T>

A specialisation of org.gradle.api.NamedDomainObjectCollection that also implements java.util.List.

All object equality is determined in terms of object names. That is, calling remove() with an object that is NOT equal to an existing object in terms of equals, but IS in terms of name equality will result in the existing collection item with the equal name being removed.

NamedDomainObjectSet

interface NamedDomainObjectSet<T : Any> : NamedDomainObjectCollection<T>, MutableSet<T>

A specialisation of NamedDomainObjectCollection that also implements Set and orders objects by their inherent name.

All object equality is determined in terms of object names. That is, calling remove() with an object that is NOT equal to an existing object in terms of equals, but IS in terms of name equality will result in the existing collection item with the equal name being removed.

Namer

interface Namer<T : Any>

A namer is capable of providing a name based on some inherent characteristic of an object.

PathValidation

class PathValidation

An enumeration for describing validation policies for file paths.

Plugin

interface Plugin<T : Any>

A Plugin represents an extension to Gradle. A plugin applies some configuration to a target object. Usually, this target object is a org.gradle.api.Project, but plugins can be applied to any type of objects.

PolymorphicDomainObjectContainer

interface PolymorphicDomainObjectContainer<T : Any> : NamedDomainObjectContainer<T>

A NamedDomainObjectContainer that allows to create domain objects with different types.

Project

interface Project : Comparable<Project>, ExtensionAware, PluginAware

This interface is the main API you use to interact with Gradle from your build file. From a Project, you have programmatic access to all of Gradle's features.

Lifecycle

There is a one-to-one relationship between a Project and a {@value #DEFAULT_BUILD_FILE} file. During build initialisation, Gradle assembles a Project object for each project which is to participate in the build, as follows:

  • Create a org.gradle.api.initialization.Settings instance for the build.
  • Evaluate the {@value org.gradle.api.initialization.Settings#DEFAULT_SETTINGS_FILE} script, if present, against the org.gradle.api.initialization.Settings object to configure it.
  • Use the configured org.gradle.api.initialization.Settings object to create the hierarchy of Project instances.
  • Finally, evaluate each Project by executing its {@value #DEFAULT_BUILD_FILE} file, if present, against the project. The projects are evaluated in breadth-wise order, such that a project is evaluated before its child projects. This order can be overridden by calling #evaluationDependsOnChildren() or by adding an explicit evaluation dependency using #evaluationDependsOn(String).
Tasks

A project is essentially a collection of Task objects. Each task performs some basic piece of work, such as compiling classes, or running unit tests, or zipping up a WAR file. You add tasks to a project using one of the create() methods on TaskContainer, such as TaskContainer#create(String). You can locate existing tasks using one of the lookup methods on TaskContainer, such as org.gradle.api.tasks.TaskCollection#getByName(String).

Dependencies

A project generally has a number of dependencies it needs in order to do its work. Also, a project generally produces a number of artifacts, which other projects can use. Those dependencies are grouped in configurations, and can be retrieved and uploaded from repositories. You use the org.gradle.api.artifacts.ConfigurationContainer returned by #getConfigurations() method to manage the configurations. The returned by #getDependencies() method to manage the dependencies. The org.gradle.api.artifacts.dsl.ArtifactHandler returned by #getArtifacts() method to manage the artifacts. The org.gradle.api.artifacts.dsl.RepositoryHandler returned by method to manage the repositories.

Multi-project Builds

Projects are arranged into a hierarchy of projects. A project has a name, and a fully qualified path which uniquely identifies it in the hierarchy.

Plugins

Plugins can be used to modularise and reuse project configuration. Plugins can be applied using the PluginAware#apply(java.util.Map) method, or by using the org.gradle.plugin.use.PluginDependenciesSpec plugins script block.

Properties

Gradle executes the project's build file against the Project instance to configure the project. Any property or method which your script uses is delegated through to the associated Project object. This means, that you can use any of the methods and properties on the Project interface directly in your script.

For example:

 defaultTasks('some-task') // Delegates to Project.defaultTasks() reportsDir = file('reports') // Delegates to Project.file() and the Java Plugin 

You can also access the Project instance using the project property. This can make the script clearer in some cases. For example, you could use project.name rather than name to access the project's name.

A project has 5 property 'scopes', which it searches for properties. You can access these properties by name in your build file, or by calling the project's #property(String) method. The scopes are:

  • The Project object itself. This scope includes any property getters and setters declared by the Project implementation class. For example, #getRootProject() is accessible as the rootProject property. The properties of this scope are readable or writable depending on the presence of the corresponding getter or setter method.
  • The extra properties of the project. Each project maintains a map of extra properties, which can contain any arbitrary name -> value pair. Once defined, the properties of this scope are readable and writable. See extra properties for more details.
  • The extensions added to the project by the plugins. Each extension is available as a read-only property with the same name as the extension.
  • The convention properties added to the project by the plugins. A plugin can add properties and methods to a project through the project's Convention object. The properties of this scope may be readable or writable, depending on the convention objects.
  • The tasks of the project. A task is accessible by using its name as a property name. The properties of this scope are read-only. For example, a task called compile is accessible as the compile property.
  • The extra properties and convention properties are inherited from the project's parent, recursively up to the root project. The properties of this scope are read-only.

When reading a property, the project searches the above scopes in order, and returns the value from the first scope it finds the property in. If not found, an exception is thrown. See #property(String) for more details.

When writing a property, the project searches the above scopes in order, and sets the property in the first scope it finds the property in. If not found, an exception is thrown. See #setProperty(String, Object) for more details.

Extra Properties All extra properties must be defined through the "ext" namespace. Once an extra property has been defined, it is available directly on the owning object (in the below case the Project, Task, and sub-projects respectively) and can be read and updated. Only the initial declaration that needs to be done via the namespace.
 project.ext.prop1 = "foo" task doStuff { ext.prop2 = "bar" } subprojects { ext.${prop3} = false } 
Reading extra properties is done through the "ext" or through the owning object.
 ext.isSnapshot = version.endsWith("-SNAPSHOT") if (isSnapshot) { // do snapshot stuff } 
Dynamic Methods

A project has 5 method 'scopes', which it searches for methods:

  • The Project object itself.
  • The build file. The project searches for a matching method declared in the build file.
  • The extensions added to the project by the plugins. Each extension is available as a method which takes a closure or org.gradle.api.Action as a parameter.
  • The convention methods added to the project by the plugins. A plugin can add properties and method to a project through the project's Convention object.
  • The tasks of the project. A method is added for each task, using the name of the task as the method name and taking a single closure or org.gradle.api.Action parameter. The method calls the Task#configure(groovy.lang.Closure) method for the associated task with the provided closure. For example, if the project has a task called compile, then a method is added with the following signature: void compile(Closure configureClosure).
  • The methods of the parent project, recursively up to the root project.
  • A property of the project whose value is a closure. The closure is treated as a method and called with the provided parameters. The property is located as described above.

ProjectEvaluationListener

interface ProjectEvaluationListener

An ProjectEvaluationListener is notified when a project is evaluated. You add can add an ProjectEvaluationListener to a org.gradle.api.invocation.Gradle using .

ProjectState

interface ProjectState

ProjectState provides information about the execution state of a project.

Rule

interface Rule

A Rule represents some action to perform when an unknown domain object is referenced. The rule can use the domain object name to add an implicit domain object.

Script

interface Script

This interface is implemented by all Gradle scripts to add in some Gradle-specific methods. As your compiled script class will implement this interface, you can use the methods and properties declared by this interface directly in your script.

Generally, a Script object will have a delegate object attached to it. For example, a build script will have a Project instance attached to it, and an initialization script will have a instance attached to it. Any property reference or method call which is not found on this Script object is forwarded to the delegate object.

Task

interface Task : 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 to create and lookup task instances. For example, 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 {@value org.gradle.api.Project#PATH_SEPARATOR} 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. You can add actions to a task by calling 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 org.gradle.api.tasks.StopActionException. A task action can abort execution of the task and continue to the next task by throwing a org.gradle.api.tasks.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, 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.
  • A Task.
  • A closure. The closure may take a Task as parameter. It 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 TaskDependency object.
  • A Buildable object.
  • A org.gradle.api.file.RegularFileProperty or org.gradle.api.file.DirectoryProperty.
  • A 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.
  • A 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.
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 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.
  • 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 Convention object. 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.

Transformer

interface Transformer<OUT : Any, IN : Any>

A Transformer transforms objects of type.

Implementations are free to return new objects or mutate the incoming value.

XmlProvider

interface XmlProvider

Provides various ways to access the content of an XML document.

Annotations

HasImplicitReceiver

class HasImplicitReceiver

Marks a SAM interface as a target for lambda expressions / closures where the single parameter is passed as the implicit receiver of the invocation (this in Kotlin, delegate in Groovy) as if the lambda expression was an extension method of the parameter type.

 // copySpec(Action<CopySpec>) copySpec { from("./sources") // the given CopySpec is the implicit receiver } 

Incubating

class Incubating

Indicates that a feature is incubating. This means that the feature is currently a work-in-progress and may change at any time.

NonExtensible

class NonExtensible

Indicates that the type, when DSL enhanced, does not support extensibility.

This means that it will not be enhanced with org.gradle.api.plugins.ExtensionAware.

NonNullApi

class NonNullApi

Marks a type or a whole package as providing a non-null API by default. All parameter and return types are assumed to be Nonnull unless specifically marked as Nullable.

Nullable

class Nullable

Indicates that the value of an element can be null.

Exceptions

BuildCancelledException

open class BuildCancelledException : GradleException

A BuildCancelledException is thrown when a build is interrupted due to cancellation request.

CircularReferenceException

open class CircularReferenceException : GradleException

A CircularReferenceException is thrown if circular references exists between tasks, the project evaluation order or the project dependsOn order.

GradleException

open class GradleException : RuntimeException

GradleException is the base class of all exceptions thrown by Gradle.

GradleScriptException

open class GradleScriptException : GradleException

A GradleScriptException is thrown when an exception occurs in the compilation or execution of a script.

IllegalDependencyNotation

open class IllegalDependencyNotation : GradleException

This exceptions is thrown, if a dependency is declared with a illegal notation.

InvalidActionClosureException

open class InvalidActionClosureException : GradleException

Thrown when a Closure is given as an Action implementation, but has the wrong signature.

InvalidUserCodeException

open class InvalidUserCodeException : GradleException

A InvalidUserCodeException is thrown when user-provided code cannot be executed.

InvalidUserDataException

open class InvalidUserDataException : GradleException

A InvalidUserDataException is thrown, if a user is providing illegal data for the build.

ProjectConfigurationException

open class ProjectConfigurationException : GradleException

Indicates a problem that occurs during project configuration (evaluation) phase.

UncheckedIOException

open class UncheckedIOException : RuntimeException

UncheckedIOException is used to wrap an java.io.IOException into an unchecked exception.

UnknownDomainObjectException

open class UnknownDomainObjectException : GradleException

A UnknownDomainObjectException is the super class of all exceptions thrown when a given domain object cannot be located.

UnknownProjectException

open class UnknownProjectException : UnknownDomainObjectException

An UnknownProjectException is thrown when a project referenced by path cannot be found.

UnknownTaskException

open class UnknownTaskException : UnknownDomainObjectException

An UnknownTaskException is thrown when a task referenced by path cannot be found.