How to use gradle properties in build.gradle
Asked Answered
D

3

50

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
Divagate answered 11/5, 2015 at 14:36 Comment(1)
Your method gives me Could not find method leftShift() for arguments [build_3vobal41blt05kb5pvio1jbbm$_run_closure12@33fabf91] on task ':tmpTask' of type org.gradle.api.DefaultTaskPerales
Y
59

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')
Yonit answered 11/5, 2015 at 15:5 Comment(0)
D
31

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
Durazzo answered 27/3, 2018 at 23:45 Comment(2)
So like, this is exactly what I wanted to have working, and yet it doesn't for me with a gradle.properties with one line VER_SPOTLESS=3.13.0 I can't print it in a task. Could not get unknown property 'VER_SPOTLESS' for task ':hello' of type org.gradle.api.DefaultTask.Carrara
It also doesn't work for me. Perhaps its because I'm using a build.gradle.kts fileThermaesthesia
F
12

From gradle.properties

Add prop to the file gradle.properties

hi1=hi

From CommandLine

Add -Pxxx end of command line.

./gradlew -q readPropertiesTask -Phi2=tete

Several properties:

./gradlew -q readPropertiesTask -Phi2=tete -Phi3=rr

How to read?

val propFromFile = project.properties["hi1"]
println("propFromFile = $propFromFile")
Fatshan answered 15/8, 2019 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.