我有一个像这样的 build.gradle
,带有一个很小的自定义任务:
task ListOfStrings(type: ExampleTask, description: 'Prove we can pass string list without parentheses') {
TheList ('one', 'two', 'three') // this works but it's not beautiful
}
public class ExampleTask extends DefaultTask {
public void TheList(String... theStrings) {
theStrings.each {
println it
}
}
}
在 test.testLogging
block 中是 events
:我们可以传递一个以逗号分隔的字符串列表,不带括号。
test {
outputs.upToDateWhen { false }
testLogging {
showStandardStreams true
exceptionFormat 'short'
events 'passed', 'failed', 'skipped' // this is beautiful
}
}
我的问题是:如何编写我的 ExampleTask
以便我可以将 TheList
编写为逗号分隔字符串的简单列表省略括号?
我的完美世界场景是能够像这样表达任务:
task ListOfStrings(type: ExampleTask, description: 'Prove we can pass string list without parentheses') {
TheList 'one', 'two', 'three'
}
最佳答案
并不是说您需要定义自定义 DSL/扩展来解决这个问题。您需要定义一个方法,而不是一个字段。这是一个工作示例:
task ListOfStrings(type: ExampleTask, description: 'Prove we can pass string list without parentheses') {
theList 'one', 'two', 'three'
}
public class ExampleTask extends DefaultTask {
List l = []
@TaskAction
void run() {
l.each { println it }
}
public void theList(Object... theStrings) {
l.addAll(theStrings)
}
}
关于gradle - 将字符串列表传递给 gradle 任务属性而不使用括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35306205/