Gradle Variables and Properties: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 12: Line 12:
=Extra Properties=
=Extra Properties=


An ''extra property'' is a property that can be declared on any model object instance available to the closure that makes the declaration. In case of a settings script, the available model objects are Settings, Gradle, ProjectDescriptor, etc. and the most common target for defining extra properties are the ProjectDescriptor instances. In case of a build script, extra property can be defined on any available object model instances.
An ''extra property'' is a property that can be declared on any model object instance available to the closure that makes the declaration. In case of a settings script, the available model objects are Settings, Gradle, ProjectDescriptor, etc., but of those ProjectDescriptor does not allow defining extra properties, and Settings, even if allows defining extra properties, cannot be easily accessed at configuration and execution phase, so the best candidate for defining extra properties is Gradle. For build scripts, extra properties can be defined on any available object model instances.


==Declaring Extra Properties==


==Declaring Extra Properties==
===At Initialization Phase===
 
===At Configuration Phase===


==Accessing Extra Properties==
==Accessing Extra Properties==

Revision as of 01:58, 20 May 2018

Internal

Overview

Configuration scripts can declare variables during the initialization and configuration phases, and the values of those variables can be accessed and updated during the execution phase and used to drive build logic. There are two types of variables that can be declared: local variables and extra properties.

Local Variables

Extra Properties

An extra property is a property that can be declared on any model object instance available to the closure that makes the declaration. In case of a settings script, the available model objects are Settings, Gradle, ProjectDescriptor, etc., but of those ProjectDescriptor does not allow defining extra properties, and Settings, even if allows defining extra properties, cannot be easily accessed at configuration and execution phase, so the best candidate for defining extra properties is Gradle. For build scripts, extra properties can be defined on any available object model instances.

Declaring Extra Properties

At Initialization Phase

At Configuration Phase

Accessing Extra Properties

Variables 


There are two types of variables that can be declared: local variables and extra properties. They will be explained below. However, regardless of type,



There are two kinds of variables that can be declared: local variables and extra properties.



There are two kinds of variables that can be declared in a build script: local variables and extra properties.

Local Variables

Local variables, which are a feature of the underlying Groovy language, are declared with the "def" keyword. They are only visible in the scope where they have been declared.

def myVariable = "something"
...
println myVariable

Extra Properties

Extra properties can be declared as follows:

...
ext {
    log4jVersion = "2.1.7"
    emailNotification = "build@example.com"
}
...

Internal

TODO: experiment with

gradle properties

Overview

The properties can be displayed with:

gradle properties

The command displays project properties, and the properties added by various plugins.

Auto-Delegation

Properties not defined in the build script are auto-delegating to the Project instance. For example:

name

and

project.name

are equivalent. Normally, using "name" is sufficient. In case you define a property or a method which has the same name as a member of the Project object, you will need to use the project property.

Predefined Properties

Project Coordinates

name

A String containing the name of the project directory. It automatically becomes the project's name coordinate. See Gradle Project Coordinates.

group

See Gradle Project Coordinates.

version

See Gradle Project Coordinates.

parent

Other Properties

project

The Project instance.

projectDir

The File instance corresponding to the directory containing the build script.

path

A String containing the absolute path of the project. The path of the root project is ":"

description

A String containing the description for the project.

displayName

buildDir

A File representing <projectDir>/build.

buildFile

rootDir

rootProject

rootProject.name

state

status

Plugin Properties

Plugins add specific properties. For a list of properties added by various plugins, consult the plugin pages.

Extra Properties

All objects in Gradle's domain model can hold extra user-defined properties. They can be declared in build.gradle, using a special syntax. Once the properties have been added, they can be read and set like predefined properties.Extra properties can be accessed from anywhere their owning object can be accessed, giving them a wider scope than local variables. Extra properties on a project are visible from its subprojects.

By requiring special syntax for adding a property, Gradle can fail fast when an attempt is made to set a predefined or extra property but the property is misspelled or does not exist.


TODO: Read Gradle_Java_Plugin#Properties and experiment with the properties added by the Java plugin.