Gangmax Blog

The 'if-else' structure in Apache Ant

今天我需要在ant build中实现这样一个功能:如果满足条件A,则运行task a;否则运行task b。查找资料后使用”ant-contrib“的”if”标签功能可以满足。

使用”ant-contrib”有几点要注意:

  1. 必须在ant build.xml文件中,显式地声明”ant-contrib”的jar文件所在的位置;

  2. “if-else”中包含的最小单位是task。也就是说,task内部的内容,比如fileset等等,是不能使用的。

最后的代码大概的样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${basedir}/lib/ant-contrib.jar"/>
</classpath>
</taskdef>
<target name="buildWithSource">
<property name="needBuildWithSource" value="true"/>
<antcall target="build" />
</target>
<target name="build">
<if>
<isset property="needBuildWithSource"/>
<then>
<copy todir="${export.dir}/src">
<fileset dir="${basedir}/src" />
</copy>
<jar file="${export.dir}/plugin/bundles/${ant.project.name}-${plugin.version}.jar" manifest="${export.dir}/META-INF/MANIFEST.MF" duplicate="fail">
<fileset dir="${export.dir}/bin"/>
<fileset dir="${export.dir}/src" />
<fileset dir=".">
<include name="OSGI-INF/"/>
</fileset>
<path refid="additional.bundle.includes"/>
</jar>
</then>
<else>
<jar file="${export.dir}/plugin/bundles/${ant.project.name}-${plugin.version}.jar" manifest="${export.dir}/META-INF/MANIFEST.MF" duplicate="fail">
<fileset dir="${export.dir}/bin"/>
<fileset dir=".">
<include name="OSGI-INF/"/>
</fileset>
<path refid="additional.bundle.includes"/>
</jar>
</else>
</if>
</target>

Comments