歡迎您光臨本站 註冊首頁

Ant基本模版和進階範例

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  1. 編譯、打jar包、運行程序的一個完整例子


<?xml version="1.0" encoding="UTF-8" ?>
<project name="HelloWorld" default="run" basedir=".">
<property name="src" value="src" />
<property name="dest" value="classes" />
<property name="hello_jar" value="hello1.jar" />
<target name="init">
<mkdir dir="${dest}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" />
</target>
<target name="build" depends="compile">
<jar jarfile="${hello_jar}" basedir="${dest}" />
</target>
<target name="run" depends="build">
<java classname="test.ant.HelloWorld" classpath="${hello_jar}" />
</target>
<target name="clean">
<delete dir="${dest}" />
<delete file="${hello_jar}" />
</target>
<target name="rerun" depends="clean,run">
<ant target="clean" />
<ant target="run" />
</target>
</project>

  2. 若干個模塊,分別都已經有了上面的build.xml和源代碼,可以用下面的build.xml集成它們:


<?xml version="1.0" encoding="UTF-8" ?>
<project name="main" default="build" basedir=".">
<property name="bin" value="${basedir}bin"/>
<property name="src1" value="${basedir}src1"/>
<property name="src2" value="${basedir}src2"/>
<property name="src3" value="${basedir}src3"/>
<target name="init">
<mkdir dir="${bin}"/>
</target>
<target name="run">
<ant dir="${src1}" target="run"/>
<ant dir="${src2}" target="run"/>
<ant dir="${src3}" target="run"/>
</target>
<target name="clean">
<ant dir="${src1}" target="clean"/>
<ant dir="${src2}" target="clean"/>
<ant dir="${src3}" target="clean"/>
</target>
<target name="build" depends="init">
<ant dir="${src1}" target="build"/>
<ant dir="${src2}" target="build"/>
<ant dir="${src3}" target="build"/>
<copy todir="${bin}">
<fileset dir="${src1}">
<include name="*.jar"/>
</fileset>
<fileset dir="${src2}">
<include name="*.jar"/>
</fileset>
<fileset dir="${src3}">
<include name="*.jar"/>
</fileset>
</copy>
</target>
<target name="rebuild" depends="build,clean">
<ant target="clean"/>
<ant target="build"/>
</target>
</project>

  3. 利用property簡化屬性

  新建all.properties文件,裡面的內容


src1=F:\TestAnt\blog\src1
src2=F:\TestAnt\blog\src2
src3=F:\TestAnt\blog\src3

  然後,在build.xml里這樣寫就可以了.


<property file="all.properties"/>
<property name="bin" value="${basedir}bin"/>

  4.利用include xml在多個build.xml里添加同樣的內容

  如include.xml:


<?xml version="1.0" encoding="UTF-8" ?>
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<target name="test">
<ant target="run"/>
</target>

  在build.xml里這樣寫:


<?xml version="1.0" encoding="UTF-8" ?>
<!--include a xml file, it can be common property, can be also a target-->
<!DOCTYPE project[
<!ENTITY share-variable SYSTEM "file:../include.xml">
]>
<project name="HelloWorld" default="run" basedir=".">
<!--use the include-->
&share-variable;
...
</project>


[火星人 ] Ant基本模版和進階範例已經有554次圍觀

http://coctec.com/docs/java/show-post-60910.html