How to access Gradle extra properties in src code?

task in build.gradle file -

task automationTest(type: Test) {
    ext.env = rootProject.hasProperty('TS1') ? rootProject['TS2'] : 'TS1'
    println env //printing passed pararmeter successfully

now I want to access env in Login class in src/main/java

I know i can access variables from build.gradle to any java class by using system property like systemProperty ‘env’,[‘env’] ?: ‘dev’ command in build.gradle, from console gradle -Denv=test and then accessing System.getProperty(“env”) from anywhere in src but i want to understand if i create env as ext property or project property then how can i use env from any java class?
Can anyone please help?

Also what should be the ideal way to use variables(System property or project property) in src from Build.gradle?

can anyone please answer

The questions, as written, aren’t really answerable.

Gradle is a build automation tool. You can setup a build to do whatever your application needs, but there is no scenario where the application code in src/main/java is going to be aware of what’s in the Gradle build.

You need to define what your application needs (System properties? Command line args? Generated property files?) and then configure Gradle to provide or generate something that meets that requirement.

The build and the application are two separate contexts. The build can read / write files in the application source or provide configuration that can be passed when starting a JVM process, but they’re not going to share memory / variables like they’re a single application.

1 Like

Thank you for the explanation , I am a newbie in gradle and trying to set up a framework with it where I want to pass environment value from Gradle Terminal.
I am able to achieve it using system property env define in build file, giving gradle -Denv=test task command in terminal and accessing it using in java file. but when I am defining ext properties in gradl3e and running command gradle -Penv=test task in that case when I am calling variable env in my java file its unrecognizable.I am looking for the way to access -Penv=value passed from terminal to java file.

The project properties are a Gradle concept. Java code is not going to be able to access something that is just a Gradle project property. As you’ve mentioned, the Java code can access System properties. There’s nothing that prevents you from using any value you want for the System property that you provide. It doesn’t have to be another System property, but it’s up to you to map it. There’s no direct access. For example,

// Set the project extra property to the value of 'env' set via command line or default to 'dev'.
// You can use this many places in the project.
ext.env = findProperty('env') ?: 'dev'

test {
    // Set the system property 'env' for 'test' task to the value of the project property 'env'
    systemProperty 'env', env
1 Like

Thank you for the solution. Following the above, I am passing value from console and then able to access it in java code.
But here also we are first assigning ext property to system property and then accessing it. Rather direct I can create a system property and use it. which approach is better to use for a simple test framework.

Your original posts suggested that you specifically wanted to use project properties for this. I included a project property set using ext just to show how you could set the value at the command line or fall back to a default in such a way that you could also consume the value in other configuration, not just the test, if needed. You can set the System property with the project property directly if you don’t need this ability.

Gradle and your Java code are running in two different JVMs. The process of setting a System property on the JVM that runs your Java code occurs the same way no matter where the value originates. You’re not saving anything by using a System property on the Gradle JVM just because you’re going to use a System property in the Java code.

It doesn’t really matter. Design your code however you want to design it, then use Gradle to map whatever you need to configure to what your code needs. My preference is to always use project properties over System properties to configure what I need in my Gradle project. I only use System properties if I’m actually trying to configure something in the “System” being provided to me. Everything else is configuration of my project.

Thank you,
I have set up my gradle file like below -
If possible can you please advise any improvements
def suiteFile
def APITest = project.hasProperty(“APITest”)
def UITest = project.hasProperty(“UITest”)
def DBTest = project.hasProperty(“DBTest”)
ext.env = findProperty(‘env’) ?: ‘dev’

// enable TestNG support (default is JUnit)
dependsOn ‘cleanTest’
if(APITest) {
suiteFile = ‘APItestng.xml’
if(UITest) {
suiteFile = ‘testng.xml’
systemProperty ‘env’, env
if(DBTest) {
suiteFile = ‘DBtestng.xml’
def reportFolder= suiteFile.substring( 0, suiteFile.indexOf(“ng”))
reports.html.destination = file(“build/reports/tests/”+reportFolder)
useDefaultListeners = true
options.suites “src/test/resources/”+suiteFile
showStandardStreams = true
// show standard out and standard error of the test JVM(s) on the console
testLogging.showStandardStreams = true