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

编程入门 行业动态 更新时间:2024-10-27 09:40:09
本文介绍了将字符串列表传递给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任务属性而不使用括号

本文发布于:2023-11-24 08:30:24,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   括号   字符串   属性   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!