Ant

My Ant Build File

Posted by Simon Dong on 2014-10-02

Eclipse中的设定:

需要设定一个Ant属性:build_dir,指明build.xml和相关文件的存放路径。
设定值在”Preferences->Ant->Runtime->Properties”,添加一个property,
name: build_dir
value:’

build.init.xml

这个Build文件用于根据project.template文件生成project.properties文件,目前仅存放项目的根路径,build.init.xml中的Target:SetBase仅需要运行一次即可。

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8" ?>
<project name="General Init project Root" default="setBase" >
<target name="setBase">
<copy file="${build_dir}/project.template" tofile="${build_dir}/project.properties" overwrite="true" force="true"/>
<replace file="${build_dir}/project.properties" token="@project_loc@" value="${project_loc}" />
<replaceregexp file="${build_dir}/project.properties" match="\\" flags="g" replace="/" byline="false"/>
<echo message="Project Base Set To: ${project_loc}" />
</target>
</project>

project.template

1
project_loc=@project_loc@

build.xml

target:copyMavenLib,将Maven依赖拷贝到WebContent/WEB-INF/lib目录下,这一任务依赖于maven-ant-tasks扩展,其jar包的下载地址maven-ant-tasks-2.1.3.jar

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?xml version="1.0" encoding="UTF-8" ?>
<project name="General Build File" default="generateSite" xmlns:artifact="urn:maven-artifact-ant" basedir=".">

<property file="${build_dir}/project.properties" />
<property name="SiteBase" value="${project_loc}/site" />
<property name="WebBase" value="${project_loc}/WebContent" />
<property name="ClassBase" value="${WebBase}/WEB-INF/classes" />
<property name="LibBase" value="${WebBase}/WEB-INF/lib" />
<property name="extLib" value="${project_loc}/extLib" />

<!--Target Begin -->
<target name="copyMavenLib">
<!--define Maven POM -->
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant">
<classpath>
<pathelement location="${build_dir}/maven-ant-tasks-2.1.3.jar" />
</classpath>
</typedef>

<artifact:pom file="${project_loc}/pom.xml" id="project.pom" />

<artifact:dependencies pathId="dependency.classpath" filesetId="dependency.fileset" usescope="runtime">
<pom refid="project.pom" />
</artifact:dependencies>

<copy todir="${LibBase}" verbose="true">
<fileset refid="dependency.fileset" />
<mapper type="flatten" />
</copy>
</target>

<target name="copyExtLib">
<copy todir="${LibBase}" verbose="true">
<fileset dir="${extLib}" includes="**/*" />
</copy>
</target>

<target name="copyToLib">
<mkdir dir="${LibBase}" />
<echo message="remove existing library files" />
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${LibBase}" includes="**/*"/>
</delete>

<echo message="copy maven dependency files" />
<antcall target="copyMavenLib" />

<echo message="copy ext dependency files" />
<antcall target="copyExtLib" />

</target>

<target name="generateSite" depends="copyToLib">
<mkdir dir="${SiteBase}" />
<echo message="remove all files in site" />
<delete includeEmptyDirs="true" failonerror="false">
<fileset dir="${SiteBase}" includes="**/*"/>
</delete>

<copy todir="${SiteBase}" overwrite="true" force="true" includeEmptyDirs="true" verbose="true">
<fileset dir="${WebBase}" includes="**/*" />
</copy>
</target>
</project>