How can I declare list in gradle.properties file?

谁说胖子不能爱 提交于 2021-02-07 14:50:34

问题


When I try to write something like this in the gradle.properties file:

defaultTasks=['deploy']

I get next message:

BUILD FAILED
FAILURE: Build failed with an exception.


* What went wrong:
Cannot cast object '['deploy']' with class 'java.lang.String' to class 'java.util.List'

回答1:


I suppose, it's not possible to make it this way, because this is a plain java properties and the property value is a String by default. But you can add some initialization logic to your script, to read custom properties and use them to initialize the defaultTasks property.

Add a custom property into the gradle.properties file

extDefaultTasks=temp1,temp2

temp1 and temp2 are task names (this tasks should exists). And in the build script, read this property, parse it and initialize defaultTasks with it:

//load custom property value and split it into the task names
def String[] tasksToUseAsDefault = extDefaultTasks.split(',')
//use task names to initialize defaultTasks
tasksToUseAsDefault.each {defaultTasks.add(it.trim())}

This configuration should be added into to the root of the script, in order to be done at the configuration phase of the build




回答2:


The values in .properties files are always read as Strings.

You can just declare the array in your .properties file like the following.

defaultTasks=deploy,build,run,compile 
// or just defaultTasks=deploy if you have only one task

And convert it into an Array in your .gradle file like the following.

tasks = properties.getProperty("defaultTasks").split(",")


来源:https://stackoverflow.com/questions/37808495/how-can-i-declare-list-in-gradle-properties-file

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