學習ant所作的筆記,根據官方網站所舉事例html
<?xml version="1.0" ?>java
<project name="ant-project" default="print-dir">apache
<property name="name" value="jojo" />app
<target name="print-dir">ide
<echo message="name: ${name} project: ${ant.project.name} "/>學習
</target>網站
</project>ui
運行結果以下:spa
[root@aliyun_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/build.xml xml
Buildfile: /home/huang/test/build.xml
print-dir:
[echo] name: jojo project: ant-project -----》${ant.project.name},系統變量
BUILD SUCCESSFUL
Total time: 0 seconds
上例中用戶設置了名爲name的屬性,這個屬性設置後,在下文中能夠經過 ${name}取得這兩個屬性值。
unzip參數:
[root@aliyun_test test]# cat unzip.xml
<project name="unzip_name" default="init">
<property name="src.dir" value="/home/huang/test/src"/>
<property name="dest.dir" value="/home/huang/test/dest"/>
<target name="init" description="compress the Task">
<unzip src="${src.dir}/build.zip" dest="${dest.dir}" />
</target>
</project>
mkdir建立一個src目錄:http://ant.apache.org/manual/Tasks/mkdir.html
[root@aliyun_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<target name="number">
<mkdir dir="${src.dir}"/>
</target>
</project>
運行結果:
[root@aliyun_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[mkdir] Created dir: /home/huang/test/src
BUILD SUCCESSFUL
Total time: 0 seconds
delete刪除一個目錄或者文件:http://ant.apache.org/manual/Tasks/delete.html
[root@aliyun_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<target name="number">
<delete dir="${src.dir}"/>
</target>
</project>
運行結果:
[root@aliyun_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[delete] Deleting directory /home/huang/test/src ---》刪除src目錄及其目錄下的全部文件
BUILD SUCCESSFUL
Total time: 0 seconds
刪除目錄中帶有.bak結尾的文件
<delete>
<fileset dir="." includes="**/*.bak"/> ----》刪除當前目錄下面到字目錄中有.bak的文件
</delete>
copy複製一個文件:http://ant.apache.org/manual/Tasks/copy.html
[root@aliyun_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<target name="number">
<copy file="unzip.xml" tofile="unzip.xml_copy"/> ----》複製一個單文件unzip.xml,unzip.xml_copy
</target>
</project>
運行結果:
[root@aliyun_test apache-ant-1.9.7]# bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[copy] Copying 1 file to /home/huang/test
BUILD SUCCESSFUL
Total time: 0 seconds
<copy file="unzip.xml" todir="${src.dir}"/> 複製單文件到目錄下
[root@aliyun_test test]# ll src/
total 4
-rw-r--r-- 1 root root 293 Sep 19 14:53 unzip.xml
copy複製一個目錄下的全部文件除了某特定的文件:http://ant.apache.org/manual/Tasks/copy.html
[root@aliyun_test test]# cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<property name="dest.dir" value="dest"/>
<target name="number">
<mkdir dir="${dest.dir}"/>
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" excludes="**/*.xml"/> ----》excludes:排除某個特定文件
</copy>
</target>
</project>
運行以下:
[huang@aliyun_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[copy] Copying 1 file to /home/huang/test/dest
BUILD SUCCESSFUL
Total time: 0 seconds
copy將某目錄下面的全部文件所有copy爲.bak模式
[huang@aliyun_test test]$ cat file.xml
<project name="create" default="number" basedir="/home/huang/test">
<property name="src.dir" value="src"/>
<property name="dest.dir" value="dest"/>
<target name="number">
<copy todir="${dest.dir}">
<fileset dir="${src.dir}"/>
<globmapper from="*" to="*.bak"/>
</copy>
</target>
</project>
運行效果以下:
[huang@aliyun_test apache-ant-1.9.7]$ bin/ant -f /home/huang/test/file.xml
Buildfile: /home/huang/test/file.xml
number:
[copy] Copying 2 files to /home/huang/test/dest
[copy] Copied 1 empty directory to 1 empty directory under /home/huang/test/dest
BUILD SUCCESSFUL
Total time: 0 seconds
[huang@aliyun_test test]$ ll dest/
total 4
-rw-rw-r-- 1 huang huang 0 Sep 19 15:40 test.txt.bak
-rw-rw-r-- 1 huang huang 293 Sep 19 15:40 unzip.xml.bak
一個jar程序的編譯運行過程:
<project name="HelloWorld" basedir="/home/huang/test/" default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="oata.HelloWorld"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
#######${ant.project.name}----->HelloWorld(<project name="HelloWorld")
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>
Now it's easier, just do a ant and you will get
Buildfile: build.xml
clean:
compile:
[mkdir] Created dir: C:\...\build\classes
[javac] Compiling 1 source file to C:\...\build\classes
jar:
[mkdir] Created dir: C:\...\build\jar
[jar] Building jar: C:\...\build\jar\HelloWorld.jar
run:
[java] Hello World
main:
BUILD SUCCESSFUL