Read Defined Variable In Gradle
Gradle is a powerfulbuild 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 thebuild.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 aBuildConfig.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 += generatedDircompileJava.dependsOn generateBuildConfig1.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.0Environment: PROD
2. Generating a Property File
Another flexible approach is to write the Gradle variables into aproperties 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 abuild.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 generatePropertiesFilesourceSets { main { resources { srcDir "$buildDir/resources/generated" } }}2.2 Load in Java
Here’s how you can read the generatedbuild.properties file from Java using theProperties 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.0Environment: 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 therun 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
UseSystem.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.0Environment: STAGING
4. Conclusion
Reading Gradle-defined variables in Java can be accomplished in multiple ways. Depending on your needs—compile-time availability, runtime flexibility, or external configurability—you can choose one of the following approaches: Java Class Generation, which is best for compile-time constants; Property File, ideal for runtime configurations; and System Properties, useful for dynamic environments like CI/CD. With these techniques, you can bridge the gap between your Gradle build configuration and your Java application logic efficiently.

Thank you!
We will contact you soon.



