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

Eclipse快速上手指南之使用ANT

2013-11-23 20:15:16  來源: Java開源技術 

  Ant是Java平台下非常棒的批處理命令執行程序能非常方便地自動完成編譯測試打包部署等等一系列任務大大提高開發效率如果你現在還沒有開始使用Ant那就要趕快開始學習使用使自己的開發水平上一個新台階
  
  Eclipse中已經集成了Ant我們可以直接在Eclipse中運行Ant
  
  以前面建立的Hello工程為例創建以下目錄結構
  
 

  新建一個buildxml放在工程根目錄下buildxml定義了Ant要執行的批處理命令雖然Ant也可以使用其它文件名但是遵循標准能更使開發更規范同時易於與別人交流
  
  通常src存放Java源文件classes存放編譯後的class文件lib存放編譯和運行用到的所有jar文件web存放JSP等web文件dist存放打包後的jar文件doc存放API文檔
  
  然後在根目錄下創建buildxml文件輸入以下內容
  
  <?xml version=?>
  <project name=Hello world default=doc
  
  <! properies
  <property name=srcdir value=src />
  <property name=reportdir value=report />
  <property name=classesdir value=classes />
  <property name=libdir value=lib />
  <property name=distdir value=dist />
  <property name=docdir value=doc/>
  
  <! 定義classpath
  <path id=masterclasspath
  <fileset file=${libdir}/*jar />
  <pathelement path=${classesdir}/>
  </path>
  
  <! 初始化任務
  <target name=init
  </target>
  
  <! 編譯
  <target name=compile depends=init description=compile the source files
  <mkdir dir=${classesdir}/>
  <javac srcdir=${srcdir} destdir=${classesdir} target=
  <classpath refid=masterclasspath/>
  </javac>
  </target>
  
  <! 測試
  <target name=test depends=compile description=run junit test
  <mkdir dir=${reportdir}/>
  <junit printsummary=on
  haltonfailure=false
  failureproperty=testsfailed
  showoutput=true
  <classpath refid=masterclasspath />
  <formatter type=plain/>
  <batchtest todir=${reportdir}
  <fileset dir=${classesdir}
  <include name=**/*Test*/>
  </fileset>
  </batchtest>
  </junit>
  <fail if=testsfailed
  ***********************************************************
  **** One or more tests failed! Check the output ****
  ***********************************************************
  </fail>
  </target>
  
  <! 打包成jar
  <target name=pack depends=test description=make jar file
  <mkdir dir=${distdir} />
  <jar destfile=${distdir}/hellojar basedir=${classesdir}
  <exclude name=**/*Test* />
  <exclude name=**/Test** />
  </jar>
  </target>
  
  <! 輸出api文檔
  <target name=doc depends=pack description=create api doc
  <mkdir dir=${docdir} />
  <javadoc destdir=${docdir}
  author=true
  version=true
  use=true
  windowtitle=Test API
  <packageset dir=${srcdir} defaultexcludes=yes
  <include name=example/** />
  </packageset>
  <doctitle><![CDATA[<h>Hello test</h>]]></doctitle>
  <bottom><![CDATA[<i>All Rights Reserved</i>]]></bottom>
  <tag name=todo scope=all description=To do: />
  </javadoc>
  </target>
  </project>
  
  以上xml依次定義了init(初始化)compile(編譯)test(測試)doc(生成文檔)pack(打包)任務可以作為模板
  
  選中Hello工程然後選擇ProjectPropertiesBuildersNew…選擇Ant Build
  
 

  填入NameAnt_BuilderBuildfilebuildxmlBase Directory${workspace_loc:/Hello}(按Browse Workspace選擇工程根目錄)由於用到了junitjar包搜索Eclipse目錄找到junitjar把它復制到Hello/lib目錄下並添加到Ant的Classpath中
  
 

  然後在Builder面板中鉤上Ant_Build去掉Java Builder
  

  再次編譯即可在控制台看到Ant的輸出
  
  Buildfile: F:\eclipseprojects\Hello\buildxml
  
  init:
  
  compile:
  [mkdir] Created dir: F:\eclipseprojects\Hello\classes
  [javac] Compiling source files to F:\eclipseprojects\Hello\classes
  
  test:
  [mkdir] Created dir: F:\eclipseprojects\Hello\report
  [junit] Running exampleHelloTest
  [junit] Tests run: Failures: Errors: Time elapsed: sec
  
  pack:
  [mkdir] Created dir: F:\eclipseprojects\Hello\dist
  [jar] Building jar: F:\eclipseprojects\Hello\dist\hellojar
  
  doc:
  [mkdir] Created dir: F:\eclipseprojects\Hello\doc
  [javadoc] Generating Javadoc
  [javadoc] Javadoc execution
  [javadoc] Loading source files for package example
  [javadoc] Constructing Javadoc information
  [javadoc] Standard Doclet version _
  [javadoc] Building tree for all the packages and classes
  [javadoc] Building index for all the packages and classes
  [javadoc] Building index for all classes
  [javadoc] Generating F:\eclipseprojects\Hello\doc\stylesheetcss
  [javadoc] Note: Custom tags that could override future standard tags: @todo To avoid potential overrides use at least one period character () in custom tag names
  [javadoc] Note: Custom tags that were not seen: @todo
  BUILD SUCCESSFUL
  Total time: seconds
  
  Ant依次執行初始化編譯測試打包生成API文檔一系列任務極大地提高了開發效率將來開發JEE項目時還可加入部署等任務並且即使脫離了Eclipse環境只要正確安裝了Ant配置好環境變量ANT_HOME=<Ant解壓目錄>Path=…;%ANT_HOME%\bin在命令行提示符下切換到Hello目錄簡單地鍵入ant即可
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28185.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.