熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Ant基本模版和進階范例

2022-06-13   來源: Java核心技術 

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

  
<?xml version= encoding=UTF ?> 
<project name=HelloWorld default=run basedir=
  <property name=src value=src /> 
  <property name=dest value=classes /> 
  <property name=hello_jar value=hellojar />    
  <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=testantHelloWorld classpath=${hello_jar} /> 
  </target> 
  <target name=clean
    <delete dir=${dest} /> 
    <delete file=${hello_jar} /> 
  </target> 
  <target name=rerun depends=cleanrun
    <ant target=clean /> 
    <ant target=run /> 
  </target> 
</project>

   若干個模塊分別都已經有了上面的buildxml和源代碼可以用下面的buildxml集成它們

  
<?xml version= encoding=UTF ?> 
<project name=main default=build basedir=
  <property name=bin value=${basedir}\bin/> 
  <property name=src value=${basedir}\src/> 
  <property name=src value=${basedir}\src/> 
  <property name=src value=${basedir}\src/> 
  <target name=init
    <mkdir dir=${bin}/> 
  </target> 
  <target name=run
    <ant dir=${src} target=run/> 
    <ant dir=${src} target=run/> 
    <ant dir=${src} target=run/> 
  </target> 
  <target name=clean
    <ant dir=${src} target=clean/> 
    <ant dir=${src} target=clean/> 
    <ant dir=${src} target=clean/> 
  </target> 
  <target name=build depends=init
    <ant dir=${src} target=build/> 
    <ant dir=${src} target=build/> 
    <ant dir=${src} target=build/> 
    <copy todir=${bin}
      <fileset dir=${src}
        <include name=*jar/> 
      </fileset> 
      <fileset dir=${src}
        <include name=*jar/> 
      </fileset> 
      <fileset dir=${src}
        <include name=*jar/> 
      </fileset> 
    </copy> 
  </target> 
  <target name=rebuild depends=buildclean
    <ant target=clean/> 
    <ant target=build/> 
  </target> 
</project>

   利用property簡化屬性

  新建allproperties文件裡面的內容

  
src=F:\\TestAnt\\blog\\src 
src=F:\\TestAnt\\blog\\src 
src=F:\\TestAnt\\blog\\src

  然後在buildxml裡這樣寫就可以了

  
<property file=allproperties/> 
<property name=bin value=${basedir}\bin/>

  利用include xml在多個buildxml裡添加同樣的內容

  如includexml:

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

  在buildxml裡這樣寫

  
<?xml version= encoding=UTF ?> 
<!include a xml file it can be common property can be also a target
<!DOCTYPE project[ 
<!ENTITY sharevariable SYSTEM file:/includexml
]> 
<project name=HelloWorld default=run basedir=
  <!use the include
  &sharevariable; 
   
</project>


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25956.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.