问题描述
示例代码:
def ant = new AntBuilder()
ant.'antlib:org.jacoco.ant:agent'(
enabled: enabled,
property: 'agentvmparam')
当启用"参数为空时,我希望它不存在于蚂蚁任务转换中,而不仅仅是空"."empty" 被评估为 "true" http://ant.apache.org/manual/develop.html#set-magic 这不是我想要的.
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 构建器示例:
def xml = new MarkupBuilder()
xml.omitNullAttributes = true
xml.root(
requiredAttribute:'required',
optionalAttribute: optionalAttribute
) { }
如果 Groovy 参数计算为 null,omitNullAttributes"将确保optionalAttribute"xml 元素参数甚至不存在.
That "omitNullAttributes" will ensure that the "optionalAttribute" xml element parameter isn't even present if the Groovy parameter evaluates to null.
所以我明白了
<root requiredAttribute='required' />
代替
<root requiredAttribute='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 } )
即:使用 findAll
删除参数映射的空条目
ie: use a findAll
to remove the null entries of the param map
这篇关于Groovy AntBuilder,省略条件属性,如“setOmitNullAttributes";MarkupBulder 上的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!