Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing properties to a gradle build

Tags:

gradle

I admit I am quite new to gradle but I did not expect to be unable to understand something as simple as the example below. I can read the gradle documentation about checking whether a project property have been set or not using a hasProperty(String propertyName) call and I am sitting here and have no idea why something so basic does not work.

I believe my mind must be so much "ant like" oriented that for sure I am missing something ordinary basic

task printSystem() << {
    println system
    println "has property: " + hasProperty("system")
}

and invoking that task with the command below:

$gradle printSystem -Psystem=mySystem
mySystem
has property: null

So my questions would be:

  1. Why system is printed out but hasProperty returns null?
  2. How should I check for the existence of the project property called "system"?
  3. Is there a different way for testing for a project property as opposed to a system property?
  4. How would you pass a system property from the command line?

This is from, the gradle documentation and I believe I am reading it right

19.2.1. Checking for project properties

You can access a project property in your build script simply by using its name as you would use a variable. If this property does not exist, an exception will be thrown and the build will fail. If your build script relies on optional properties the user might set, perhaps in a gradle.properties file, you need to check for existence before you access them. You can do this by using the method hasProperty('propertyName') which returns true or false.

like image 465
Julian Avatar asked Jun 26 '15 04:06

Julian


People also ask

How do you pass properties in Gradle?

Using 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. You can also set system properties in gradle.

How do I pass JVM options to Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.


1 Answers

You need to explicitly invoke hasProperty on the project instance - without it, hasProperty is invoked on some local context. The following example works:

task printSystem() << {
    println system
    println "has property: " + project.hasProperty("system")
}
  1. Because non-existing properties (system is not defined in the script) are taken from the project instance. If you won't pass the system property, an exception will be thrown on println.
  2. project.hasProperty('propName')
  3. Not sure if I understood right, but you can access project properties via the project instance and system properties via the System class.
  4. Using -D switch - gradle -Dprop=value
like image 110
Opal Avatar answered Sep 24 '22 00:09

Opal