原創|其它|編輯:郝浩|2009-09-30 10:53:04.000|閱讀 481 次
概述:本文介紹Ant基本模版示例詳細介紹。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
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>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自:IT專家網