Read Defined Variable In Gradle
Gradle is a powerful build automation tool used in many Java projects. One common requirement is to define build-time variables (like version numbers, build timestamps, environment names, etc.) in the build.gradle file and access them in Java code. In this article we will show you how to read a defined variable in Gradle in java.
1. Generating a Java Class
This method involves generating a Java class during the build process that contains static constants initialized with Gradle-defined values. It’s particularly useful when values need to be available at compile-time.
1.1 Define the task in build.gradle
The following Gradle task dynamically generates a BuildConfig.java file under a designated directory during the build.
def generatedDir = "$buildDir/generated-src"
task generateBuildConfig {
doLast {
def outputDir = file(generatedDir)
outputDir.mkdirs()
def file = new File(outputDir, "BuildConfig.java")
file.text = """package com.example;
public final class BuildConfig {
public static final String APP_VERSION = "${project.version}";
public static final String ENVIRONMENT = "PROD";
}
"""
}
}
sourceSets.main.java.srcDirs += generatedDir
compileJava.dependsOn generateBuildConfig
1.2 Access it in your Java code
Once generated, the Java class can be imported and its constants can be accessed just like any other class:
package com.example;
public class MainApp {
public static void main(String[] args) {
System.out.println("Version: " + BuildConfig.APP_VERSION);
System.out.println("Environment: " + BuildConfig.ENVIRONMENT);
}
}
When you run the code, you’ll get this output:
Version: 1.0 Environment: PROD
2. Generating a Property File
Another flexible approach is to write the Gradle variables into a properties file that can be read by your application at runtime. This is ideal for apps requiring configurable environments or frequent redeployments without recompiling code.
2.1 Generate the file in Gradle
This task writes selected properties to a build.properties file inside the resources directory.
task generatePropertiesFile {
def propsDir = "$buildDir/resources/generated"
inputs.property("version", project.version)
outputs.file("$propsDir/build.properties")
doLast {
def file = file("$propsDir/build.properties")
file.parentFile.mkdirs()
file.text = "version=${project.version}\nenvironment=DEV\n"
}
}
processResources.dependsOn generatePropertiesFile
sourceSets {
main {
resources {
srcDir "$buildDir/resources/generated"
}
}
}
2.2 Load in Java
Here’s how you can read the generated build.properties file from Java using the Properties class.
import java.io.InputStream;
import java.util.Properties;
public class AppWithProperties {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
try (InputStream input = AppWithProperties.class.getClassLoader()
.getResourceAsStream("build.properties")) {
props.load(input);
}
System.out.println("Version: " + props.getProperty("version"));
System.out.println("Environment: " + props.getProperty("environment"));
}
}
When you run the code, you’ll get this output:
Version: 1.0 Environment: DEV
3. Injecting the Variable as an Environment or System Property
For situations like CI/CD pipelines or dynamic environment configurations, injecting variables as system properties during application launch is a robust strategy. These variables are available at runtime and don’t require additional files or generated code.
3.1 Define the run task in the build.gradle
Configure the run task to pass system properties that your Java code can later retrieve.
run {
systemProperty "build.version", project.version
systemProperty "env", "STAGING"
}
3.2 Read it in Java
Use System.getProperty() to read the injected properties.
public class EnvSystemPropsApp {
public static void main(String[] args) {
String version = System.getProperty("build.version", "unknown");
String env = System.getProperty("env", "default");
System.out.println("Version: " + version);
System.out.println("Environment: " + env);
}
}
When you run the code, you’ll get this output:
Version: 1.0 Environment: STAGING
Sign up

