Gradle Project Coordinates, State and Configured Properties: Difference between revisions
Line 149: | Line 149: | ||
gradle -Pshape=square display | gradle -Pshape=square display | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== | ==Project Properties Priority Rules== | ||
=TO DEPLETE= | =TO DEPLETE= | ||
{{Internal|Gradle Project Properties TODEPLETE|Gradle Project Properties TODEPLETE}} | {{Internal|Gradle Project Properties TODEPLETE|Gradle Project Properties TODEPLETE}} |
Revision as of 06:09, 5 October 2020
Internal
Overview
Project-identifying information such as name, group and version are referred to as "project coordinates". The 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. More details are provided in their corresponding section. 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.
Coordinates
Name
The project name is a string that identifies the project. The project name is initialized by default with the name of the directory that 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 org.gradle.api.initialization.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:
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 org.gradle.api.initialization.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:
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:
Other 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
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, similarly to extra properties.
If gradle.properties declares:
shape = 'square'
the configured value can be accessed from the project (build.gradle) simply using the property name:
task display {
doLast {
// equivalent property references
println project.shape
println shape
}
}
-P Command-Line Options
Project properties can also be specified on command line by prefixing them with -P:
gradle -Pshape=square display