简体   繁体   中英

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. 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? 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.

Essentially, if jvmExecutable != null do this

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

or if jvmExecutable == null do this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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