Reputation: 1621
Is there a way to include
or exclude
certain files based on a condition in Apache Ant (1.8.3)? For instance, I have a macrodef
that takes an attribute. I would like to include
certain files if the attribute's value matches xyz
:
<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>
For instance, if myattr
value is xyz
, I would like to include
the commented file in portion above.
Upvotes: 1
Views: 7256
Reputation: 10377
Fileset can use nested include/exlucde patternsets with if/unless
Attribute. The pattern is included/excluded when a named property is set or not, so no ant addons needed.
Some snippet :
<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>
--EDIT after comment --
The if/unless attribute
after include name/exclude name
checks whether the given value is a property which is set (when using if="..") or not set (when using unless="..") in the ant project scope - it doesn't check for a specific value.
<include name="*.xml" unless="foo"/>
means include is only active if no property named foo is set in your project
<include name="*.xml" if="foo"/>
means include is only active if property named foo is set in your project
Works fine for me , used Ant 1.7.1, had no Ant 1.8.x around right now :
<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>
output :
[echo] ${ant.version} => Apache Ant version 1.7.1 compiled on June 27 2008
[echo] switchant.bat;foobar.bat;foo.bat
-- EDIT after comment --
Using serveral include patterns works fine :
...
<fileset dir="C:/whatever" id="foobar">
<include name="*.xml" if="pass" />
<include name="*.bat" if="pass"/>
<include name="**/*.txt" if="pass"/>
</fileset>
...
maybe you're using the wrong patterns ?
-- EDIT after comment --
Here is the reference to <local>
task which is needed to work with the properties in the current scope, in this case, <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>
Upvotes: 3
Reputation: 31
if you can use ant-contrib, then try this:
<contrib:if>
<equals arg1="${myattr}" arg2="xyz" />
<then>
<include name="PATH/TO/file.xml" />
</then>
</contrib:if>
if you can't use ant-contrib, try this:
http://jaysonlorenzen.wordpress.com/2010/03/10/apache-ant-if-else-condition-without-ant-contrib/
Upvotes: 2