不使用括号将字符串列表传递给gradle任务属性
我有一个像这样的不使用括号将字符串列表传递给gradle任务属性,gradle,groovy,build.gradle,Gradle,Groovy,Build.gradle,我有一个像这样的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 D
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
块中是events
:我们可以传递一个逗号分隔的字符串列表,不带括号
test {
outputs.upToDateWhen { false }
testLogging {
showStandardStreams true
exceptionFormat 'short'
events 'passed', 'failed', 'skipped' // this is beautiful
}
}
我的问题是:如何编写我的ExampleTask
,这样我就可以将列表
写成一个简单的逗号分隔字符串列表而忽略括号
我的完美世界场景是能够这样表达任务:
task ListOfStrings(type: ExampleTask, description: 'Prove we can pass string list without parentheses') {
TheList 'one', 'two', 'three'
}
您在
test.testlogging
中提供的示例与您显示的代码示例略有不同,因为testlogging使用扩展,您正在创建一个任务。以下是如何定义作为任务输入的自定义扩展:
public class CustomExtension{
final Project project
CustomExtension(final Project project) {
this.project = project
}
public void theList(String... theStrings){
project.tasks.create('printStrings'){
doLast{
theStrings.each { println it }
}
}
}
}
project.extensions.create('List', CustomExtension, project)
List{
theList 'one', 'two', 'three'
}
现在运行的gradle printStrings
提供:
gradle printstrings
:printStrings
one
two
three
BUILD SUCCESSFUL
Total time: 3.488 secs
您不需要定义自定义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)
}
}
没有必要定义扩展。你说得对@Opal我是在使用OPs问题中的
test.testlogging
。刚意识到,这并不是他们想要的。虽然没有准确回答问题,但这很有帮助。非常感谢@ragethemethodname的例子。魔术使用camelCase,它会工作,使用TitleCase,它会爆炸。是的,我也看到了。另一个问题的好例子。