How to use gradle properties in build.gradle

user1723095 picture user1723095 · May 11, 2015 · Viewed 34k times · Source

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

Answer

JB Nizet picture JB Nizet · May 11, 2015

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')