简体   繁体   English

将Groovy AntBuilder()与可选的JVM参数一起使用

[英]Using Groovy AntBuilder() with an optional JVM parameter

I'm trying to update a maven plugin that's written in groovy to use an external JVM if it's available, otherwise, just use the default. 我正在尝试更新使用groovy编写的Maven插件,以使用外部JVM(如果可用),否则,请使用默认值。 My code changes look something like this: 我的代码更改如下所示:

def jvmExecutable = null;
if (someCondtion = true) {
 jvmExecutable = "something"
}

def ant = new AntBuilder()
ant.java(fork: "${fork}", jvm: "${jvmExecutable}"....)

Is there a way in Groovy to leave off the jvm: "${jvmExecutable}" directive if jvmExecutable is null? 如果jvmExecutable为null,在Groovy中是否有办法摆脱jvm: "${jvmExecutable}"指令? The Groovy Ant task expects an executable there if jvm is specified, but I'd like it to use it's default if I don't specify something. Groovy Ant任务在指定了jvm的情况下期望那里有一个可执行文件,但是如果我不指定内容,我希望它使用默认值。

Essentially, if jvmExecutable != null do this 本质上,如果jvmExecutable != null请执行此操作

ant.java(fork: "${fork}", jvm: "${jvmExecutable}", ....)

or if jvmExecutable == null do this 或者如果jvmExecutable == null请执行此操作

ant.java(fork: "${fork}", ....)

Thank you! 谢谢!

when you pass named parameters into method you are actually building hashmap 当您将命名参数传递给方法时,您实际上是在构建哈希图

so this code 所以这段代码

ant.echo(message:"hello", level:"error")

equals to this one 等于这个

ant.echo( [message:"hello", level:"error"] )

finally you want to keep in the map only valid values. 最后,您只想在地图中保留有效值。 like this: 像这样:

ant.echo( [message:"hello", level:null].findAll{it.value!=null} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM