Renato Ivancic - Software Engineer

Background image

Gradle Properties

Gradle

Project Properties Debugging

Want to see at runtime which properties were passed to your project. You can use the following script to print properties in following order.

  • Gradle extra properties
  • All Gradle properties
  • Arguments to Gradle JVM
  • All system properties
import java.lang.management.RuntimeMXBean
import java.lang.management.ManagementFactory

println("==== START EXT Gradle Properties ====")
project.properties.ext.properties.forEach{key, value ->
  println("KEY: $key, VALUE is: $value")
}
println("==== START Gradle Properties ====")
project.properties.forEach{key, value ->
  println("KEY: $key, VALUE is: $value")
}
println("==== START Gradle JVM Arguments ====")
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean()
List<String> jvmArgs = runtimeMXBean.getInputArguments()
for (String arg : jvmArgs) {
  System.out.println(arg)
}
println("==== START System Properties ====")
System.getenv()
System.getProperties().forEach{key, value ->
  println("KEY: $key, VALUE is: $value")
}

Be aware this can print many properties containing that with sensitive information.

Unfortunately you won’t be able to get access to all command line arguments that were passed with Gradle CLI from the IDE, except if they were passed as project or system properties.

Example resutl when running following command gradlew testTask -i -PprojectProperty=projectProperty -DsystemProperty=systemProperty'

==== START EXT Gradle Properties ====

KEY: projectProperty, VALUE is: projectProperty
KEY: privateArchivaUser, VALUE is: xxx
KEY: privateArchivaPassword, VALUE is: xxx
==== END EXT Gradle Properties ====


==== START Gradle Properties ====
KEY: parent, VALUE is: null
KEY: classLoaderScope, VALUE is: org.gradle.api.internal.initialization.DefaultClassLoaderScope@f6968f5e
KEY: objects, VALUE is: org.gradle.api.internal.model.DefaultObjectFactory@f3c0c06
KEY: logger, VALUE is: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger@7bf14e43
KEY: rootDir, VALUE is: /Users/rivancic/Projects/experiment/springboot/beans
...
==== END Gradle Properties ====


==== START Gradle JVM Arguments ====
-Xoptionsfile=/Users/rivancic/Library/Java/JavaVirtualMachines/adopt-openj9-15.0.2/Contents/Home/lib/options.default
-Duser.dir=/Users/rivancic/.gradle/daemon/7.4.1
-XX:MaxMetaspaceSize=256m
-Xms256m
-Xmx512m
-Dfile.encoding=UTF-8
-Duser.country=AT
-Duser.language=de
-Duser.variant
-Djava.class.path=/Users/rivancic/.gradle/wrapper/dists/gradle-7.4.1-bin/58kw26xllvsiedyf3nujyarhn/gradle-7.4.1/lib/gradle-launcher-7.4.1.jar
...
==== END Gradle JVM Arguments ====


==== START System Properties ====
KEY: java.specification.version, VALUE is: 15
KEY: kotlinx.coroutines.debug, VALUE is: off
KEY: java.vendor.url, VALUE is: https://adoptopenjdk.net/
KEY: file.separator, VALUE is: /
KEY: java.runtime.version, VALUE is: 15.0.2+7
KEY: user.name, VALUE is: rivancic
...
==== END System Properties ====

Sources

How to print all command line arguments passed to gradle? - StackOverflow

Writing Build Scripts - Gradle Userguide

#Gradle #Build tool #Gradle Properties