【问题标题】:How can I build up an AntBuilder call programmatically in Groovy?如何在 Groovy 中以编程方式构建 AntBuilder 调用?
【发布时间】:2018-03-08 16:28:05
【问题描述】:

假设我有这个 Groovy 代码:

ant.exec(executable:"cmd",osfamily:"windows",dir:bin) {
    arg(value: "/c")
    arg(value: "add-user.bat")
    arg(value: user)
    arg(value: pw)
    arg(value: "--silent")                                                                                                      
}

我的代码中经常使用不同数量的参数进行此类 exec 调用,因此我认为它可能是带有对象数组参数的函数:

private void execute(Object... argumens) {
    ant.sequential {
        exec(executable:"cmd",osfamily:"windows",dir:bin) {
            arg(value: "/c")
            //What should I do here
        }
    }
}

//It would be called like this:
execute("add-user.bat",user,pw,"--silent");

我应该在 exec 元素中写什么?是否有可能在该 exec 内部进行迭代?

请耐心等待,我是一名 Java 人,想在 Maven 中编写一些脚本,所以我不了解 Groovy 的 AntBuilder 中发生的魔法。如果您对 Groovy 中的 AntBuilder 的工作原理有一些易于理解的解释,我们将不胜感激。

【问题讨论】:

  • 你试过argumens.each { arg(value: it) }
  • 当然可以循环播放。
  • 顺便说一句,对于 java 人:arg(value: "/c") 正在调用一个方法 arg(Map),映射 key="value"value="/c"delegate 对象上。
  • @tim_yates 谢谢,它有效。
  • @daggett 感谢您的解释。

标签: groovy ant antbuilder


【解决方案1】:

你应该可以做到:

private void execute(Object... argumens) {
    ant.sequential {
        exec(executable:"cmd",osfamily:"windows",dir:bin) {
            arg(value: "/c")
            argumens.each {
                arg(value: it)
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-01-22
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多