How to use gradle properties in build.gradle

限于喜欢 提交于 2019-12-03 11:10:48

问题


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

回答1:


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



回答2:


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



回答3:


From gradle.properties

Add prop to the file gradle.properties

hi1=hi

From CommandLine

Add -Pxxx end of commandline.

./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")


来源:https://stackoverflow.com/questions/30170539/how-to-use-gradle-properties-in-build-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!