Project

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:

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 create. You can locate existing tasks using one of the lookup methods on TaskContainer, such as getByName.

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 getRepositories 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 apply method, or by using the org.gradle.plugin.use.PluginDependenciesSpec plugins script block.

Dynamic Project 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 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 org.gradle.api.plugins.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 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 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 org.gradle.api.plugins.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 configure 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.

Properties

Link copied to clipboard
The default build directory name.
Link copied to clipboard
val DEFAULT_BUILD_FILE: String = "build.gradle"
The default project build file name.
Link copied to clipboard
val DEFAULT_STATUS: String = "release"
Link copied to clipboard
val DEFAULT_VERSION: String = "unspecified"
Link copied to clipboard

The extra properties extension in this object's extension container.

Link copied to clipboard
val GRADLE_PROPERTIES: String = "gradle.properties"
Link copied to clipboard
The hierarchy separator for project and task path names.
Link copied to clipboard
val SYSTEM_PROP_PREFIX: String = "systemProp"

Functions

Link copied to clipboard
abstract fun absoluteProjectPath(path: String): String
Converts a name to an absolute project path, resolving names relative to this project.
Link copied to clipboard
abstract fun afterEvaluate(@DelegatesTo(value = Project::class) closure: Closure)
Adds a closure to call immediately after this project is evaluated.
abstract fun afterEvaluate(action: Action<in Project>)
Adds an action to call immediately after this project is evaluated.
Link copied to clipboard
abstract fun allprojects(@DelegatesTo(value = Project::class) configureClosure: Closure)
abstract fun allprojects(action: Action<in Project>)
Configures this project and each of its sub-projects.
Link copied to clipboard
abstract fun ant(@DelegatesTo(value = AntBuilder::class) configureClosure: Closure): AntBuilder
Executes the given closure against the AntBuilder for this project.
abstract fun ant(configureAction: Action<in AntBuilder>): AntBuilder
Executes the given action against the AntBuilder for this project.
Link copied to clipboard
abstract fun apply(closure: Closure)
Link copied to clipboard
inline fun <T : Plugin<Project>> Project.apply()
inline fun <T : Plugin<*>> PluginAware.apply()

Applies the plugin of the given type T. Does nothing if the plugin has already been applied.

inline fun PluginAware.apply(vararg options: Pair<String, Any?>)

Kotlin extension function for org.gradle.api.plugins.PluginAware.apply.

fun PluginAware.apply(from: Any? = null, plugin: String? = null, to: Any? = null)

Applies the given plugin or script.

Link copied to clipboard
inline fun <T : Plugin<*>> PluginAware.applyTo(vararg targets: Any)

Applies the plugin of the given type T to the specified object. Does nothing if the plugin has already been applied.

Link copied to clipboard
abstract fun artifacts(@DelegatesTo(value = ArtifactHandler::class) configureClosure: Closure)
abstract fun artifacts(configureAction: Action<in ArtifactHandler>)
Configures the published artifacts for this project.
Link copied to clipboard
fun Project.artifacts(configuration: ArtifactHandlerScope.() -> Unit)

Configures the artifacts for this project.

Link copied to clipboard
abstract fun beforeEvaluate(closure: Closure)
Adds a closure to call immediately before this project is evaluated.
abstract fun beforeEvaluate(action: Action<in Project>)
Adds an action to call immediately before this project is evaluated.
Link copied to clipboard
abstract fun buildscript(configureClosure: Closure)
Configures the build script classpath for this project.
Link copied to clipboard

Configures the build script classpath for this project.

Link copied to clipboard
abstract fun compareTo(p: T): Int
Link copied to clipboard
abstract fun components(configuration: Action<in SoftwareComponentContainer>)
Configures software components.
Link copied to clipboard
abstract fun configurations(configureClosure: Closure)
Configures the dependency configurations for this project.
Link copied to clipboard
abstract fun configure(objects: Iterable<out Any>, configureClosure: Closure): Iterable<out Any>
Configures a collection of objects via a closure.
abstract fun <T> configure(objects: Iterable<T>, configureAction: Action<in T>): Iterable<T>
Configures a collection of objects via an action.
abstract fun configure(object: Any, configureClosure: Closure): Any
Configures an object via a closure, with the closure's delegate set to the supplied object.
Link copied to clipboard
inline fun <T : Any> Project.configure(noinline configuration: T.() -> Unit)

Executes the given configuration block against the project extension of the specified type.

inline fun <T : Any> ExtensionAware.configure(noinline configuration: T.() -> Unit)

Executes the given configuration block against the extension of the specified type.

Link copied to clipboard
abstract fun <T> container(type: Class<T>): NamedDomainObjectContainer<T>
abstract fun <T> container(type: Class<T>, factoryClosure: Closure): NamedDomainObjectContainer<T>
Creates a container for managing named objects of the specified type.
Link copied to clipboard
inline fun <T : Any> Project.container(noinline factory: (String) -> T): NamedDomainObjectContainer<T>

Creates a container for managing named objects of the specified type.

inline fun <T : Any> Project.container(type: KClass<T>, factory: NamedDomainObjectFactory<T>): NamedDomainObjectContainer<T>

Kotlin extension function taking kotlin.reflect.KClass for org.gradle.api.Project.container.

Link copied to clipboard
abstract fun copy(@DelegatesTo(value = CopySpec::class) closure: Closure): WorkResult
abstract fun copy(action: Action<in CopySpec>): WorkResult
Copies the specified files.
Link copied to clipboard
abstract fun copySpec(): CopySpec
abstract fun copySpec(@DelegatesTo(value = CopySpec::class) closure: Closure): CopySpec
abstract fun copySpec(action: Action<in CopySpec>): CopySpec
Creates a CopySpec which can later be used to copy files or create an archive.
Link copied to clipboard
Creates an additional AntBuilder for this project.
Link copied to clipboard
abstract fun defaultTasks(defaultTasks: Array<String>)
Sets the names of the default tasks of this project.
Link copied to clipboard
inline fun Project.defaultTasks(vararg tasks: Task)

Sets the default tasks of this project. These are used when no tasks names are provided when starting the build.

Link copied to clipboard
abstract fun delete(paths: Array<Any>): Boolean
Deletes files and directories.
abstract fun delete(action: Action<in DeleteSpec>): WorkResult
Deletes the specified files.
Link copied to clipboard
abstract fun dependencies(configureClosure: Closure)
Configures the dependencies for this project.
Link copied to clipboard

Configures the dependencies for this project.

Link copied to clipboard
abstract fun dependencyLocking(configuration: Action<in DependencyLockingHandler>)
Configures dependency locking
Link copied to clipboard
abstract fun depthCompare(otherProject: Project): Int
Compares the nesting level of this project with another project of the multi-project hierarchy.
Link copied to clipboard
abstract fun evaluationDependsOn(path: String): Project
Declares that this project has an evaluation dependency on the project with the given path.
Link copied to clipboard
Declares that this project has an evaluation dependency on each of its child projects.
Link copied to clipboard
abstract fun exec(@DelegatesTo(value = ExecSpec::class) closure: Closure): ExecResult
abstract fun exec(action: Action<in ExecSpec>): ExecResult
Executes an external command.
Link copied to clipboard
abstract fun file(path: Any): File
Resolves a file path relative to the project directory of this project.
abstract fun file(path: Any, validation: PathValidation): File
Resolves a file path relative to the project directory of this project and validates it using the given scheme.
Link copied to clipboard
abstract fun files(paths: Array<Any>): ConfigurableFileCollection
Returns a ConfigurableFileCollection containing the given files.
abstract fun files(paths: Any, @DelegatesTo(value = ConfigurableFileCollection::class) configureClosure: Closure): ConfigurableFileCollection
abstract fun files(paths: Any, configureAction: Action<in ConfigurableFileCollection>): ConfigurableFileCollection
Creates a new ConfigurableFileCollection using the given paths.
Link copied to clipboard
abstract fun fileTree(baseDir: Any): ConfigurableFileTree
abstract fun fileTree(baseDir: Any, @DelegatesTo(value = ConfigurableFileTree::class) configureClosure: Closure): ConfigurableFileTree
abstract fun fileTree(baseDir: Any, configureAction: Action<in ConfigurableFileTree>): ConfigurableFileTree
Creates a new ConfigurableFileTree using the given base directory.
abstract fun fileTree(args: Map<String, out Any>): ConfigurableFileTree
Creates a new ConfigurableFileTree using the provided map of arguments.
Link copied to clipboard
inline fun Project.fileTree(vararg args: Pair<String, Any?>): ConfigurableFileTree

Kotlin extension function for org.gradle.api.Project.fileTree.

Link copied to clipboard
@Nullable
abstract fun findProject(path: String): Project
Locates a project by path.
Link copied to clipboard
@Nullable
abstract fun findProperty(propertyName: String): Any
Returns the value of the given property or null if not found.
Link copied to clipboard
abstract fun getAllprojects(): Set<Project>
Returns the set containing this project and its subprojects.
Link copied to clipboard
abstract fun getAllTasks(recursive: Boolean): Map<Project, Set<Task>>
Returns a map of the tasks contained in this project, and optionally its subprojects.
Link copied to clipboard
abstract fun getAnt(): AntBuilder
Returns the AntBuilder for this project.
Link copied to clipboard
Returns a handler for assigning artifacts produced by the project to configurations.
Link copied to clipboard
abstract fun getBuildDir(): File
Returns the build directory of this project.
Link copied to clipboard
abstract fun getBuildFile(): File
The build script for this project.
Link copied to clipboard
Returns the build script handler for this project.
Link copied to clipboard
Returns a path to the project for the full build tree.
Link copied to clipboard
Returns the direct children of this project.
Link copied to clipboard
Returns the software components produced by this project.
Link copied to clipboard
Returns the configurations of this project.
Link copied to clipboard
abstract fun getConvention(): Convention
Returns the org.gradle.api.plugins.Convention for this project.
Link copied to clipboard
abstract fun getDefaultTasks(): List<String>
Returns the names of the default tasks of this project.
Link copied to clipboard
Returns the dependency handler of this project.
Link copied to clipboard
Provides access to methods to create various kinds of Dependency instances.
Link copied to clipboard
Provides access to configuring dependency locking
Link copied to clipboard
abstract fun getDepth(): Int
Returns the nesting level of a project in a multi-project hierarchy.
Link copied to clipboard
@Nullable
abstract fun getDescription(): String
Returns the description of this project, if any.
Link copied to clipboard
abstract fun getDisplayName(): String
Returns a human-consumable display name for this project.
Link copied to clipboard
Allows adding DSL extensions to the project.
Link copied to clipboard
abstract fun getGradle(): Gradle
Returns the org.gradle.api.invocation.Gradle invocation which this project belongs to.
Link copied to clipboard
abstract fun getGroup(): Any
Returns the group of this project.
Link copied to clipboard
Returns an immutable view of this project, safe for use with isolated projects.
Link copied to clipboard
abstract fun getLayout(): ProjectLayout
Provides access to various important directories for this project.
Link copied to clipboard
abstract fun getLogger(): Logger
Returns the logger for this project.
Link copied to clipboard
abstract fun getLogging(): LoggingManager
Returns the org.gradle.api.logging.LoggingManager which can be used to receive logging and to control the standard output/error capture for this project's build script.
Link copied to clipboard
abstract fun getName(): String
Returns the name of this project.
Link copied to clipboard
Provides access to configuring input normalization.
Link copied to clipboard
abstract fun getObjects(): ObjectFactory
Provides access to methods to create various kinds of model objects.
Link copied to clipboard
@Nullable
abstract fun getParent(): Project
Returns the parent project of this project, if any.
Link copied to clipboard
abstract fun getPath(): String
Returns the path of this project.
Link copied to clipboard
Link copied to clipboard
abstract fun getPlugins(): PluginContainer
Link copied to clipboard
abstract fun getProject(): Project
Returns this project.
Link copied to clipboard
abstract fun getProjectDir(): File
The directory containing the project build file.
Link copied to clipboard
abstract fun getProperties(): Map<String, out Any>
Returns the properties of this project.
Link copied to clipboard
Provides access to methods to create various kinds of Provider instances.
Link copied to clipboard
Returns a handler to create repositories which are used for retrieving dependencies and uploading artifacts produced by the project.
Link copied to clipboard
Provides access to resource-specific utility methods, for example factory methods that create various resources.
Link copied to clipboard
abstract fun getRootDir(): File
Returns the root directory of this project.
Link copied to clipboard
abstract fun getRootProject(): Project
Returns the root project for the hierarchy that this project belongs to.
Link copied to clipboard
abstract fun getState(): ProjectState
Returns the evaluation state of this project.
Link copied to clipboard
abstract fun getStatus(): Any
Returns the status of this project.
Link copied to clipboard
abstract fun getSubprojects(): Set<Project>
Returns the set containing the subprojects of this project.
Link copied to clipboard
abstract fun getTasks(): TaskContainer
Returns the tasks of this project.
Link copied to clipboard
abstract fun getTasksByName(name: String, recursive: Boolean): Set<Task>
Returns the set of tasks with the given name contained in this project, and optionally its subprojects.
Link copied to clipboard
abstract fun getVersion(): Any
Returns the version of this project.
Link copied to clipboard
fun Project.gradleKotlinDsl(): Dependency

Creates a dependency on the API of the current version of the Gradle Kotlin DSL.

Link copied to clipboard
abstract fun hasProperty(propertyName: String): Boolean
Determines if this project has the given property.
Link copied to clipboard
abstract fun javaexec(@DelegatesTo(value = JavaExecSpec::class) closure: Closure): ExecResult
Executes a Java main class.
abstract fun javaexec(action: Action<in JavaExecSpec>): ExecResult
Executes an external Java process.
Link copied to clipboard
abstract fun mkdir(path: Any): File
Creates a directory and returns a file pointing to it.
Link copied to clipboard
abstract fun normalization(configuration: Action<in InputNormalizationHandler>)
Configures input normalization.
Link copied to clipboard

Nested plugins blocks are NOT allowed, for example:

Link copied to clipboard
abstract fun project(path: String): Project
Locates a project by path.
abstract fun project(path: String, @DelegatesTo(value = Project::class) configureClosure: Closure): Project
Locates a project by path and configures it using the given closure.
abstract fun project(path: String, configureAction: Action<in Project>): Project
Locates a project by path and configures it using the given action.
Link copied to clipboard
@Nullable
abstract fun property(propertyName: String): Any
Returns the value of the given property.
Link copied to clipboard
operator fun Project.provideDelegate(any: Any?, property: KProperty<*>): PropertyDelegate

Locates a property on Project.

Link copied to clipboard
abstract fun <T> provider(value: Callable<out @Nullable T>): Provider<T>
Creates a Provider implementation based on the provided value.
Link copied to clipboard
abstract fun relativePath(path: Any): String
Returns the relative path from the project directory to the given path.
Link copied to clipboard
abstract fun relativeProjectPath(path: String): String
Converts a name to a project path relative to this project.
Link copied to clipboard
abstract fun repositories(configureClosure: Closure)
Configures the repositories for this project.
Link copied to clipboard
fun Project.repositories(configuration: RepositoryHandler.() -> Unit)

Configures the repositories for this project.

Link copied to clipboard
abstract fun setBuildDir(path: File)
abstract fun setBuildDir(path: Any)
Sets the build directory of this project.
Link copied to clipboard
abstract fun setDefaultTasks(defaultTasks: List<String>)
Sets the names of the default tasks of this project.
Link copied to clipboard
abstract fun setDescription(@Nullable description: String)
Sets a description for this project.
Link copied to clipboard
abstract fun setGroup(group: Any)
Sets the group of this project.
Link copied to clipboard
abstract fun setProperty(name: String, @Nullable value: Any)
Sets a property of this project.
Link copied to clipboard
abstract fun setStatus(status: Any)
Sets the status of this project.
Link copied to clipboard
abstract fun setVersion(version: Any)
Sets the version of this project.
Link copied to clipboard
abstract fun subprojects(@DelegatesTo(value = Project::class) configureClosure: Closure)
Configures the sub-projects of this project.
abstract fun subprojects(action: Action<in Project>)
Configures the sub-projects of this projectThis method executes the given Action against the sub-projects of this project.
Link copied to clipboard
abstract fun sync(action: Action<in SyncSpec>): WorkResult
Synchronizes the contents of a destination directory with some source directories and files.
Link copied to clipboard
abstract fun tarTree(tarPath: Any): FileTree
Creates a new FileTree which contains the contents of the given TAR file.
Link copied to clipboard
abstract fun task(name: String): Task
abstract fun task(name: String, @DelegatesTo(value = Task::class) configureClosure: Closure): Task
abstract fun task(name: String, configureAction: Action<in Task>): Task
abstract fun task(args: Map<String, out Any>, name: String): Task
abstract fun task(args: Map<String, out Any>, name: String, configureClosure: Closure): Task
Creates a Task with the given name and adds it to this project.
Link copied to clipboard
inline fun <type : Task> Project.task(name: String): type

Creates a Task with the given name and type, and adds it to this project tasks container.

inline fun Project.task(name: String, vararg args: Pair<String, Any?>): Task

Kotlin extension function for org.gradle.api.Project.task.

inline fun <type : Task> Project.task(name: String, noinline configuration: type.() -> Unit): type

Creates a Task with the given name and type, configures it with the given configuration action, and adds it to this project tasks container.

fun <T : Task> Project.task(name: String, type: KClass<T>, configuration: T.() -> Unit): T
Link copied to clipboard
inline fun <T : Any> Project.the(): T
fun <T : Any> Project.the(extensionType: KClass<T>): T

Returns the project extension of the specified type.

inline fun <T : Any> ExtensionAware.the(): T

Returns the extension of the specified type.

fun <T : Any> ExtensionAware.the(extensionType: KClass<T>): T

Returns the extension of the specified extensionType.

Link copied to clipboard
abstract fun uri(path: Any): URI
Resolves a file path to a URI, relative to the project directory of this project.
Link copied to clipboard
abstract fun zipTree(zipPath: Any): FileTree
Creates a new FileTree which contains the contents of the given ZIP file.