是否有一种方法可以基于Apache Ant(1.8.3)中的条件来include
或exclude
某些文件?例如,我有一个macrodef
,它具有一个属性。如果属性的值与xyz
:匹配,我想include
某些文件
<macrodef name="pkgmacro">
<attribute name="myattr" />
<sequential>
<zip destfile="${dist}/@{myattr}.war">
<fileset dir="${dist}/webapp" >
<include name="**/@{myattr}/**" />
<exclude name="WEB-INF/config/**" />
<!-- if @{myattr} = "xyz", then
<include name="PATH/TO/file.xml" />
-->
</fileset>
<zipfileset dir="${ear}/@{myattr}/WEB-INF/" includes="*.xml" prefix="WEB-INF/" />
</zip>
</sequential>
</macrodef>
例如,如果myattr
的值是xyz
,我想include
上面部分中的注释文件。
文件集可以使用带有if/inther的嵌套include/exrucde模式集属性无论是否设置了命名属性,都会包含/排除模式,因此不需要任何附加组件
一些片段:
<project>
<!-- property that triggers your include/exclude
maybe set via condition in some other target .. -->
<property name="foo" value="bar"/>
<macrodef name="pkgmacro">
<attribute name="myattr" />
<sequential>
<fileset dir="C:/whatever" id="foobar">
<!-- alternatively
<include name="*.bat" unless="@{myattr}"/>
-->
<include name="*.bat" if="@{myattr}"/>
</fileset>
<!-- print fileset contents -->
<echo>${toString:foobar}</echo>
</sequential>
</macrodef>
<pkgmacro myattr="foo"/>
</project>
-评论后编辑
include name/exclude name
之后的if/unless attribute
检查给定值是否是在ant项目范围中设置的属性(当使用if=".."时)或未设置的属性,当使用except=".."时)-它不检查特定值<include name="*.xml" unless="foo"/>
意味着只有在项目中设置了名为foo的no属性时,include才处于活动状态<include name="*.xml" if="foo"/>
意味着只有在项目中设置了名为foo的属性时,include才处于活动状态
对我来说效果很好,使用了Ant 1.7.1,现在没有Ant 1.8.x:
<project>
<echo>$${ant.version} => ${ant.version}</echo>
<macrodef name="pkgmacro">
<attribute name="myattr"/>
<sequential>
<condition property="pass">
<equals arg1="@{myattr}" arg2="xyz" />
</condition>
<fileset dir="C:/whatever" id="foobar">
<include name="*.bat" if="pass" />
</fileset>
<echo>${toString:foobar}</echo>
</sequential>
</macrodef>
<pkgmacro myattr="xyz"/>
</project>
输出:
[echo] ${ant.version} => Apache Ant version 1.7.1 compiled on June 27 2008
[echo] switchant.bat;foobar.bat;foo.bat
-评论后编辑
使用服务器包含模式效果良好:
...
<fileset dir="C:/whatever" id="foobar">
<include name="*.xml" if="pass" />
<include name="*.bat" if="pass"/>
<include name="**/*.txt" if="pass"/>
</fileset>
...
也许你用错模式了?
-评论后编辑
以下是对<local>
任务的引用,该任务需要使用当前范围中的属性,在本例中为<sequential>
:
<macrodef name="pkgmacro">
<attribute name="myattr"/>
<sequential>
<!-- make property pass mutable -->
<local name="pass"/>
<condition property="pass">
<equals arg1="@{myattr}" arg2="xyz" />
</condition>
<fileset dir="C:/whatever" id="foobar">
<include name="*.xml" if="pass" />
<include name="*.bat" if="pass" />
<include name="**/*.txt" if="pass" />
</fileset>
<!-- print value of property pass and fileset contents -->
<echo>$${pass} = ${pass}${line.separator}${toString:foobar}</echo>
</sequential>
</macrodef>
如果你可以使用ant contrib,那么试试这个:
<contrib:if>
<equals arg1="${myattr}" arg2="xyz" />
<then>
<include name="PATH/TO/file.xml" />
</then>
</contrib:if>
如果你不能使用ant contrib,试试这个:
http://jaysonlorenzen.wordpress.com/2010/03/10/apache-ant-if-else-condition-without-ant-contrib/