问题
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 String
s.
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