开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜