Passing Configuration to a Gradle Build via Custom Environment Variables: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:


* [[Gradle_Variables_and_Properties#Passing_Configuration_to_a_Gradle_Build_via_Custom_Environment_Variables|Gradle Variables and Properties]]
* [[Gradle_Variables_and_Properties#Passing_Configuration_to_a_Gradle_Build_via_Custom_Environment_Variables|Gradle Variables and Properties]]
* [[Gradle_Operations#Pass_Configuration_via_Custom_Environment_Variables|Gradle Operations]]


=Overview=
=Overview=
Line 13: Line 14:
=Procedure=
=Procedure=


The method consists in defining a {{Gradle_Variables_and_Properties#Extra_Properties|project extra property}} that is initialized with the value of the custom environment variable, or with a default value if the environment variable is not defined.
The method consists in defining a [[Gradle_Variables_and_Properties#Extra_Properties|project extra property]] that is initialized with the value of the custom environment variable, or to a default value if the environment variable is not defined. The following code sequence is added to build.gradle, in the [[Gradle_Variables_and_Properties#ext_Closure|ext closure]]:
 
<syntaxhighlight lang='groovy'>
ext {
 
    projectExtPropertyInformedByEnvironmentVariable = "default value"
 
    String s = System.getenv("CUSTOM_ENVIRONMENT_VARIABLE");
    if (s != null) {
 
        projectExtPropertyInformedByEnvironmentVariable = s;
    }
}
</syntaxhighlight>
 
The following task displays the property value:
 
<syntaxhighlight lang='groovy'>
ask display {
 
    println projectExtPropertyInformedByEnvironmentVariable
}
</syntaxhighlight>
 
If CUSTOM_ENVIRONMENT_VARIABLE is not set, executing the "display" task results in:
 
gradle display
> Configure project :
default value
BUILD SUCCESSFUL in 0s
 
Setting the environment variable propagates it to the build:
 
export CUSTOM_ENVIRONMENT_VARIABLE=BLAH
gradle display
> Configure project :
BLAH
BUILD SUCCESSFUL in 0s
 
If you need to stop the build with an error if the environment variable is not found in the environment, see: {{Internal|Stop a Gradle Build with an Error|Stop a Gradle Build with an Error}}

Latest revision as of 19:57, 20 March 2019

Internal

Overview

This article describes a generic method to pass configuration to a Gradle build using custom environment variables. This method could prove useful when Gradle builds are executed in containers, as it is the case for AWS CodeBuild builds.

Playground

https://github.com/ovidiuf/playground/tree/master/gradle/custom-environment-variables

Procedure

The method consists in defining a project extra property that is initialized with the value of the custom environment variable, or to a default value if the environment variable is not defined. The following code sequence is added to build.gradle, in the ext closure:

ext {

    projectExtPropertyInformedByEnvironmentVariable = "default value"

    String s = System.getenv("CUSTOM_ENVIRONMENT_VARIABLE");
    if (s != null) {

        projectExtPropertyInformedByEnvironmentVariable = s;
    }
}

The following task displays the property value:

ask display {

    println projectExtPropertyInformedByEnvironmentVariable
}

If CUSTOM_ENVIRONMENT_VARIABLE is not set, executing the "display" task results in:

gradle display
> Configure project :
default value
BUILD SUCCESSFUL in 0s

Setting the environment variable propagates it to the build:

export CUSTOM_ENVIRONMENT_VARIABLE=BLAH
gradle display
> Configure project :
BLAH
BUILD SUCCESSFUL in 0s

If you need to stop the build with an error if the environment variable is not found in the environment, see:

Stop a Gradle Build with an Error