Today I'm trying to use the system property in my code .When I enter ./gradlew -Dorg.gradle.project.env=demo test
,the NullPointExcepetion happens,though I println env in script successfully!Then I try another way , entering ./gradlew -Denv=demo test
and my code get the env set in command line successfully .So my question is What's the defference between "-Dorg.gradle.project.env=demo" and "-Denv=demo" in gradle?P.s. This link(12.2. Gradle properties and system properties in https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_properties_and_system_properties) told me to use org.gradle.project to set system property.I guess when you use org.gradle.project, you should use another method to get system property ,not using
System.getProperty("env")

- 53
- 2
1 Answers
I guess when you use
org.gradle.project
, you should use another method to get system property, not usingSystem.getProperty("env")
You're right. This two syntaxes are different and serve different purposes.
The latter one, -Denv
is a standard way of passing system properties in Java world. If you run java -help
you'll see:
-D<name>=<value> set a system property
So, when you use it, env
system property becomes available via System.getProperty("env")
and it's value will be demo
.
The first one -Dorg.gradle.project.env
is actually a system property too! It's obvious after reading the lines above. However, it sets a system property named org.gradle.project.env
, not just env
. So, unless your test expect this name, it won't work. And your tests must no expect this name, because they should, generally, be unaware of the build tool.
What Gradle docs says is:
Gradle offers a variety of ways to add properties to your build. With the -D command line option you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command.
Gradle can also set project properties when it sees specially-named system properties or environment variables. This feature is very useful when you don’t have admin rights to a continuous integration server and you need to set property values that should not be easily visible, typically for security reasons. In that situation, you can’t use the -P option, and you can’t change the system-level configuration files. The correct strategy is to change the configuration of your continuous integration build job, adding an environment variable setting that matches an expected pattern. This won’t be visible to normal users on the system.
If the environment variable name looks like ORG_GRADLE_PROJECT_prop=somevalue, then Gradle will set a prop property on your project object, with the value of somevalue. Gradle also supports this for system properties, but with a different naming pattern, which looks like
org.gradle.project.prop
.
Differently saying, Gradle allows you to set project proprties by providing system properties with special names, and that is what you did. You've set a Project
's property named env
to a value demo
by providing a system property with a name org.gradle.project.env
. This property is available in you build script via project.env
and can be used to tweak builds in various ways.
-
1Thanks fo answering,u did help me a lot ! – Williams Liu Nov 16 '17 at 02:20
-
You are a Gradle god. – wonsuc Mar 08 '19 at 05:41