Gradle Project Coordinates, State and Configured Properties

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Overview

Project-identifying information such as name, group and version are referred to as project coordinates. The project uses and exposes different other kinds of project properties: state properties, extra properties, externally-configured project properties that come from gradle.properties file hierarchy or from -P command-line options.

Coordinates

"Project coordinates" terminology comes from Maven, where a project name, group and version uniquely identify a project artifact. Gradle project coordinates map onto Maven project coordinates, as described in the Compatibility with Maven Project Coordinates section. Different project coordinates can be set in different locations. The project name can only be set in settings.gradle, while the group, version, description can only be set and read in build.gradle. The coordinates are available as values of DSL variables and programmatically via the Project API.

Compatibility with Maven Project Coordinates

Gradle produces by default artifacts that are compatible with Maven repositories, so Gradle translates its project coordinates into Maven project coordinates. The translation rules are described in the name, group and version sections: the project name becomes artifactId, the group name becomes groupId and the version becomes Maven version.

Name

The project name is a string that identifies the project. The project name is initialized by default with the name of its top-level directory, as specified in settings.gradle. Usually, that directory contains the project's build.gradle. The name is not necessarily unique within a project hierarchy, the unique identifier for the project in a hierarchy is its path.

If the project name must be set to a value different from the name of its directory, it can only be set in the build initialization phase, in settings.gradle, using the ProjectDescriptor interface, for both simple projects and multi-projects. An attempt to set the project name in the configuration phase or execution phase via the Project interface, for both single, root or sub-project will trigger a Gradle execution error.

For a simple project, rootProject and project(':') are the same, so the following assignments are equivalent::

rootProject.name = "blue"
project(':').name = "blue"

For a multi-project build, the name of the root project can be set as above, and the name of a sub-project can be set specifying its path:

project(':subproject-01').name = "green"

The project name can be read as value of the DSL element "name":

println "${name}"
println project.name

The project name can be also obtained programmatically during the build with Project.getName().

Maven artifactId

The project name automatically becomes the Maven project name coordinate 'artifactId'. If a Maven-compliant artifact is to be generated by the project, the name should be a valid Maven identifier ([A-Za-z0-9_\-.]+). Alternatively, a non-conforming project name can be used, but artifactId must be overridden in the Maven publishing plugin configuration. More details about Maven artifactId:

Maven artifactId

Group

The project group defaults to the path with dots as separators. For a simple project, the group is the empty string, and this has implications on Maven artifact publication.

The group can only be set in the build configuration phase, by assigning the desired value to the "group" DSL element in build.gradle. The group may not be set in the initialization phase because the ProjectDescriptor interface does not expose a group mutator.

For a simple project, the following assignments are equivalent::

project.group = "io.example"
group = "io.example"

For a multi-project build, a group that is not explicitly set defaults to the parent's group. It can be set either in the sub-project's build.gradle or in the root project build.gradle:

project(':subproject-01').group = "io.example"

If set in both places, the sub-project setting takes precedence. If all sub-projects should inherit root's group, this can be set up as follows:

subprojects {
  group = rootProject.group
}

The project group can be read as value of the DSL element "group":

println "${group}"
println project.group

The group can be also obtained programmatically during the build with Project.getGroup().

Maven groupId

Gradle group automatically becomes the Maven project name coordinate 'groupId'. A non-empty group information must be specified when generating a Maven artifact. If the group information is missing, Maven Publish plugin will complain that "groupId cannot be empty". More details about Maven groupId:

Maven groupId

Version

Gradle does not initialized version information by default, so if it is needed, it must be explicitly set in the configuration phase, in build.gradle. It may not be set in the initialization phase in settings.gradle. For a simple project, the following assignments are equivalent:

project.version = '1.0'

or

version = '1.0'

For a multi-project build sub-project, the version is undefined if not explicitly set. There is no default, and also there is no inheritance of the parent project version value, if the parent specifies its version. To set it, configure build.gradle as shown:

version = '2.0'

or

version = parent.version

Alternatively, the version can be set in the root build.gradle:

project(':subproject-A').version = '2.0'

or

project(':subproject-A').version = version

If set in both places, the sub-project build.gradle value takes precedence, as it is "closer" to the project.

If all sub-projects should inherit root's version, this can be set up in the root build.gradle as follows:

subprojects {
  version = rootProject.version;
}

Maven version

Gradle version automatically becomes the Maven project name coordinate 'version'. If the project does not configure a version, which is the default behavior, the corresponding Maven version will be empty - the artifact will not cary any version information. More details about Maven version:

Maven version

Project State Properties

Description

Can be set in build.gradle with:

description = "This is an example project"

Path

The path is the fully qualified name of the project, where the elements are separated by ":". While the name may not be unique, the path is. For a simple project, the path is ":". It can be read with the "path" build.gradle DSL element or with Project.getPath(). The path cannot be explicitly set, it is inferred.

rootProject

Maintains the reference to the root project. In case of a simple project, the project and the root project are the same.

Project Directory

Build Directory

The build directory is the path specified by the Project.getBuildDir() project property. The content of the build directory and the build directory itself is removed by the Base plugin clean task.

Extra Properties

Gradle Extra Properties

Externally-Configured Project Properties

gradle.properties Project Properties

Properties declared in gradle.properties file hierarchy are flattened according set precedence ruled described here and projected into the project object instance, where they can be accessed as project properties. See:

gradle.properties

-P Command-Line Options

Project properties can also be specified on command line by prefixing them with -P:

gradle -P<property-name>=<property-value> ...
gradle -Pshape=square display

The properties declared as such are available as project properties:

task display {
  doLast {
    // equivalent property references
    println project.shape 
    println shape
  }
}

If a property with the same name is specified in multiple locations, it is subject to priority rules.

The project version can be overridden this way:

gradle -Pversion=1.0.0 build

Project Properties Priority Rules

If a property with the same name is configured on command line with -P and in gradle.properties, the command line value takes precendence.

TO DEPLETE

Gradle Project Properties TODEPLETE