将字符串列表传递给gradle任务属性而不使用括号

将字符串列表传递给gradle任务属性而不使用括号

本文介绍了将字符串列表传递给gradle任务属性而不使用括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 build.gradle 就像这样,有一个小小的自定义任务:

 任务ListOfStrings(类型:ExampleTask,描述:'证明我们可以传递没有括号的字符串列表')$ b $ TheList('one','two','three')//这个工作正常它不是很漂亮

public class ExampleTask extends DefaultTask {
public void TheList(String ... theStrings){
theStrings.each {
println it





测试中.testLogging block是 events :并且我们可以传递逗号分隔的字符串列表,不带圆括号

  test {
outputs.upToDateWhen {false}
testLogging {
showStandardStreams true
exceptionFormat 'short'
events'passed','failed','skipped'// This is beautiful
}
}

我的问题是:如何编写我的 ExampleTask ,这样我就可以编写 TheList 作为逗号分隔字符串的简单列表省略括号



我的完美世界场景是能够像这样表达任务:

$ p $ 任务ListOfStrings(类型:ExampleTask,描述:'证明我们可以传递没有括号的字符串列表'){
TheList'one','two','three'
}


解决方案

这不是真的,你需要定义自定义的DSL /扩展来解决这个问题。您需要定义方法而不是字段。下面是一个工作示例:
$ b $ pre $ 任务ListOfStrings(类型:ExampleTask,描述:'证明我们可以传递没有括号的字符串列表'){
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)
}
}


I have a build.gradle like so, with a tiny little custom task:

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
        }
    }
}

In the test.testLogging block is events: and we can pass a comma-separated list of strings without parentheses.

test {
    outputs.upToDateWhen { false }
    testLogging {
        showStandardStreams true
        exceptionFormat 'short'
        events 'passed', 'failed', 'skipped' // this is beautiful
    }
}

My question is: how do I write my ExampleTask so I can write TheList as a simple list of comma-separated strings omitting the parentheses?

My perfect-world scenario is to be able to express the task like so:

task ListOfStrings(type: ExampleTask, description: 'Prove we can pass string list without parentheses') {
    TheList 'one', 'two', 'three'
}
解决方案

That's not true that you need to define custom DSL/extension to solve this problem. You need to define a method instead of a field. Here's a working example:

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任务属性而不使用括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 22:20