我想基于前缀(听起来很简单)“映射”一堆蚂蚁属性。
我有一个解决方案,但它并不优雅(必须写出属性文件,然后再读回来!)
问题:在ANT中执行以下“load-propertyset”是否有更快/更通用/更简单/开箱即用/直接的方式? (...比我下面提供的例子)
(大致类似于Groovy > ConfigSlurper > Special Environment Configuration行为。)
例如:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Config">
<!-- Section 1. (These will be loaded from a property file...) -->
<property name="a.yyy" value="foo" />
<property name="a.zzz" value="cat" />
<property name="b.xxx" value="bar" />
<property name="b.zzz" value="dog" />
<macrodef name="load-propertyset">
<attribute name="prefix" />
<attribute name="outfile" default="123" />
<attribute name="propset" default="123" />
<sequential>
<propertyset id="@{propset}">
<propertyref prefix="@{prefix}" />
<globmapper from="@{prefix}.*" to="*" />
</propertyset>
<echo level="debug">Created propertyset - '@{propset}' from prefix='@{prefix}'</echo>
<tempfile property="@{outfile}" suffix=".properties" deleteonexit="true" />
<echo level="debug">Writing propset to temp file - '${@{outfile}}'</echo>
<echoproperties destfile="${@{outfile}}">
<propertyset refid="@{propset}"/>
</echoproperties>
<echo level="debug">Reading props from temp file - '${@{outfile}}'</echo>
<property file="${@{outfile}}" />
<delete file="${@{outfile}}" />
</sequential>
</macrodef>
<load-propertyset prefix="a" />
<load-propertyset prefix="b" />
<echo>>>> Using variables xxx=${xxx} yyy=${yyy} zzz=${zzz}</echo>
</project>
我确信我遗漏了一些简单的东西,例如:
答案 0 :(得分:0)
第三方Ant-Contrib有一个<antcallback>
task,它接受<antcall>
task并添加了将属性返回给调用者的功能:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Config" default="run">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<!-- Section 1. (These will be loaded from a property file...) -->
<property name="a.yyy" value="foo" />
<property name="a.zzz" value="cat" />
<property name="b.xxx" value="bar" />
<property name="b.zzz" value="dog" />
<macrodef name="load-propertyset">
<attribute name="prefix" />
<attribute name="return" />
<sequential>
<antcallback target="-empty-target" return="@{return}">
<propertyset>
<propertyref prefix="@{prefix}" />
<globmapper from="@{prefix}.*" to="*" />
</propertyset>
</antcallback>
</sequential>
</macrodef>
<target name="-empty-target"/>
<target name="run">
<property name="properties-to-return" value="xxx,yyy,zzz"/>
<load-propertyset prefix="a" return="${properties-to-return}"/>
<load-propertyset prefix="b" return="${properties-to-return}"/>
<echo>>>> Using variables xxx=${xxx} yyy=${yyy} zzz=${zzz}</echo>
</target>
</project>
properties-to-return
概念有点维护负担。幸运的是,<antcallback>
在被要求返回未设置的属性时不会失败。当您需要新的映射属性时,只需要修改properties-to-return
属性。