Groovy AntBuilder, omit conditional attributes, like "setOmitNullAttributes" functionality on MarkupBulder
sample code:
def ant = new AntBuilder()
ant.'antlib:org.jacoco.ant:agent'(
enabled: enabled,
property: 'agentvmparam')
When that "enabled" parameter is null, I'd like it to be not present in the ant task conversion, not merely "empty". "empty" gets evaluated to "true" http://ant.apache.org/manual/develop.html#set-magic which isn't what I want.
xml builder example:
def xml = new MarkupBuilder()
xml.omitNullAttributes = true
xml.root(
requiredAttribute:'required',
optionalAttribute: optionalAttribute
) { }
That "omitNullAttributes" will ensure that the "optionalAttribute" xml element parameter isn't even present if the Groovy parameter evaluates to null.
so I get
<root requiredAttribute='required' />
instead of
<root requiredAtt开发者_开发问答ribute='required' optionalAttribute='' />
Bit of a possible workaround, but does this work?
def ant = new AntBuilder()
ant.'antlib:org.jacoco.ant:agent'( [ enabled:enabled,
property:'agentvmparam' ].findAll { it.value != null } )
ie: use a findAll
to remove the null entries of the param map
精彩评论