Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use gradle properties in build.gradle

When I run this task:

task tmpTask << {     project.properties.each {println "   $it"} } 

I see:

org.gradle.java.home=/usr/lib/jvm/java-6-oracle 

But how to use this variable? I've tried both:

task tmpTask << {     println org.gradle.java.home     println project.properties.org.gradle.java.home } 

But none of this works. First print gives error:

Could not find property 'org' on task ':tmpTask'. 

while second fails with:

Cannot get property 'gradle' on null object 
like image 428
user1723095 Avatar asked May 11 '15 14:05

user1723095


People also ask

How do I pass system properties to Gradle build?

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. properties files with the prefix systemProp.

How do you define a property in build gradle?

Defining properties via system properties We use the -D command-line option just like in a normal Java application. The name of the system property must start with org. gradle. project , followed by the name of the property we want to set, and then by the value.


2 Answers

project.properties is a Map<String, ?>

So you can use

project.properties['org.gradle.java.home'] 

You can also use the property() method (but that looks in additional locations):

project.property('org.gradle.java.home') 
like image 133
JB Nizet Avatar answered Sep 28 '22 10:09

JB Nizet


For anyone else's benefit. If the property you define is not dot separated, then you can simply refer to it directly.

In your gradle.properties:

myProperty=This is my direct property my.property=This is my dotted property with\t\t tabs \n and newlines 

In your build.gradle:

// this works println myProperty println project.property('my.property')  // this will not println my.property 
like image 27
al. Avatar answered Sep 28 '22 09:09

al.