android基礎知識38:ant 01——編寫ant:build.xml的方法

來源:互聯網
上載者:User

ant 是apache的java子項目"jakarta"的子項目.你可以選擇當前的版本,,window版

解壓後ant_home用來方便訪問。並確保你也設定了java_home 。
set ant_home=D:\java\kit\ant\jakarta-ant-1.5.1 這是我的目錄

hello ant

我們要開發一個java類:其內容只有一句,輸出"hello ant"字串。並使用ant完成編譯和運行工作,這個例子只是為了跑通ant,不附加多餘的東西。

下面是:“hello.ant.HelloAnt.java”檔案。

package hello.ant;public class HelloAnt{public static void main(String[] args){System.out.println("hello ant,ant 的第一次接觸,好棒!");} } 

在項目根目錄(hello-ant\)寫1個檔案:ant執行設定檔build.xml

“build.xml”檔案

<?xml version="1.0" encoding="GB2312" ?><!-- 一個項目,可包含很多工作群組(target) --><project default="main" basedir="."><!-- 項目中的一個工作群組,可包含很多任務(task:javac,java...) --><target name="main"><!--編譯--><javac srcdir="src\main\hello\ant" destdir="build\classes"/><!--運行--><java classname="hello.ant.HelloAnt"><classpath><pathelement path="build\classes"/></classpath></java></target></project> 

ok,一切大功告成,哦,不,還沒有運行它。

dos下進入hello-ant的目錄,即build.xml所在的目錄,我們要用ant工具執行它 ,

執行: %ant_home%/bin/ant -file build.xml 用ant工具執行目前的目錄下的設定檔build.xml

或 :ant -file build.xml 你如果設定%ant_home%/bin到path中

這次ok了,這是答案:

命令提示字元視窗

D:\temp\hello-ant>ant -file build.xmlBuild build.xmlmain:[javac] Compiling 1 source file to D:\temp\hello-ant\build\classes[java] hello ant,ant 的第一次接觸,好棒!BUILD SUCCESSFULTotal time: 2 secondsD:\temp\hello-ant> 

檢查一下build/classes目錄,哦,看到編譯過的檔案就在這裡:
build/classes/hello/ant/HelloAnt.class.

hello ant 進級

我們要改進build.xml,讓它做更多的事情:

定義全域變數
初始化,主要是建立目錄
編譯 (已有)
打包為jar
建立API documentation
產生distribution產品
凡事都講究平衡,你要ant給你做更多事,當然要累一點點,不過只用累一次,以後的代碼修改後的構建都是"一鍵式"完成,我們製作一個hello的簡單例子,你可以自己做j2ee的練習。

我們要擴充目錄結構,使它更像回事:

:\src,\docs,\lib是自己組織的檔案結構,\build,\dist是ant動態產生的成品。\src 源檔案:java源,源,jsp源,xml配置.....\src\main java源\src\ window,unix,liunx的執行,我們的簡單只有一個:run.bat: java hello.ant.HelloAnt\docs 手寫說明文檔\lib 程式所需類庫的jar,比如j2ee.jar,mail,jar...\build 用ant動態產生的構建目錄\build\classes 編譯的類檔案\build\docs copy "\docs"的手寫說明文檔,和ant產生的api文檔\build\lib 放置我們自己的HelloAnt.class打包成品hello-ant.jar\dist\bin copy "\src\" 得執行檔案\dist\docs copy "\build\docs" 的文檔\dist\lib 除了copy "\build\lib"下的hello-ant.jar外,還應copy "\lib"的程式所需jar,這裡我們沒有。

以上是我學老外的檔案組織,大家可以按照自己的愛好組織

我們編寫必要的檔案:

hello.ant. HelloAnt.java

src\.bat 

@echo offecho ========================================================echo 請先設定 Environmentecho .echo JAVA_HOME: %JAVA_HOME%echo ======================================================%java_home%\bin\java -classpath ..\lib\hello-ant.jar hello.ant.HelloAntpause 

\docs\index.html 隨便寫一個手寫的文檔
hello ant 軟體項目手冊docs
--------------------------------------------------------------------------------

訪問api文檔
\build.xml 設定檔

<?xml version="1.0" encoding="GB2312" ?><!--=======================================================================hello-ant 項目 ,學習ant工具的第2個build file.參照ant的jakarta-ant-1.6alpha的build.xmlCopyright (c) 2002 The Neusoft Software Foundation. All rightsreserved.=======================================================================--><!--文檔結構為:<project><property/> 全域變數的定義<property/>...<target name="1"> 工作群組(tasks)<javac></javac> 一項javac任務...<oneTask></ontTask> 一項其它任務</target><target name="2"><javac></javac>...<oneTask></ontTask></target></project>project代表一個項目,default:運行到名稱為"dist"的target(工作群組)basedir:基準路徑。--><project default="dist" basedir="."><!--===================================================================定義屬性(property tasks)最好把用到的路徑呀,名稱呀都在這裡定義成全域變數例:定義<property name="a" ="hello"/>以後就可以這樣用它:<property name="b" ="${a}/b"/>現在:b=="hello/b"===================================================================--><!--主要的系統內容屬性--><property environment="env"/><!--取window,unix...的環境變數--><property name="java.home" ="${env.JAVA_HOME}"/><property name="ant.home" ="${env.ANT_HOME}"/><!--主要的app環境屬性--><property name="app.name" ="hello-ant"/><property name="app.jar" ="${app.name}.jar"/><property name="app.copyright" =" Copyright (c) 2002 The Neusoft Software Foundation. All rights reserved."/><!--app中src的屬性--><property name="src.dir" ="src" /><property name="src.main" ="${src.dir}/main"/><property name="src." ="${src.dir}/"/><!--app用到的lib--><property name="lib.dir" ="lib"/><!--app的build目錄中--><property name="build.dir" ="build" /><property name="build.classes" ="${build.dir}/classes"/><property name="build.docs" ="${build.dir}/docs"/><property name="build.docs.api" ="${build.docs}/api"/><property name="build.lib" ="${build.dir}/lib"/><!--app的dist (distribution) 目錄中--><property name="dist.dir" ="dist"/><property name="dist.bin" ="${dist.dir}/bin"/><property name="dist.docs" ="${dist.dir}/docs"/><property name="dist.lib" ="${dist.dir}/lib"/><!--app的docs目錄中--><property name="docs.dir" ="docs"/><!--定義一組路徑以後可以通過id重用這組路徑 ,例:<javac srcdir="src/main" destdir="build/classes"><classpath refid="classpath"/></javac>--><path id="classpath"><!--本項目只有一個java,用不上classpath,這裡只是做個例子--><pathelement location="${build.classes}"/><pathelement path="${java.home}/lib/tools.jar"/></path><!--===================================================================init 準備目錄(File Tasks)主要的目錄結構通常是不會變的,一起產生他們===================================================================--><target name="init"><!--清除以前目錄--><delete dir="${build.dir}" fail="false" /><delete dir="${dist.dir}" fail="false"/><!--準備目錄--><mkdir dir="${build.dir}"/><mkdir dir="${build.classes}"/><mkdir dir="${build.docs}"/><mkdir dir="${build.docs.api}"/><mkdir dir="${build.lib}"/><mkdir dir="${dist.dir}"/><mkdir dir="${dist.bin}"/><mkdir dir="${dist.lib}"/></target><!--===================================================================Build the code (Compile Tasks,File Tasks)===================================================================--><target name="build" depends="init"><!--編譯--><javac srcdir="${src.main}" destdir="${build.classes}"><classpath refid="classpath"/></javac></target><!--===================================================================打包文檔(Archive Tasks)Create the project jars: xxx1.jar and xxx2.jar===================================================================--><target name="jars" depends="build"><jar basedir="${build.classes}" jarfile="${build.lib}/${app.jar}"/></target><!--===================================================================Creates the API documentation===================================================================--><target name="javadocs"depends="jars"deion="--> creates the API documentation"><!--copy docs 手冊... --><copy todir="${build.docs}"><fileset dir="${docs.dir}"/></copy><javadoc packagenames="hello.ant.*"sourcepath="${src.main}"defaultexcludes="yes"destdir="${build.docs.api}"author="true"version="true"use="true"windowtitle="Docs API"><doctitle><![CDATA[<h1>hello ant Docs API</h1>]]></doctitle><bottom><![CDATA[<i>${app.copyright}</i>]]></bottom><tag name="todo" scope="all" deion="To do:" /></javadoc></target><!--===================================================================Create the distribution that can run (Archive Tasks)主要是從各目錄中把該copy的copy上===================================================================--><target name="dist" depends="javadocs"><!--copy bin 執行檔案 --><copy todir="${dist.bin}"><fileset dir="${src.}/"/></copy><copy todir="${dist.docs}"><fileset dir="${build.docs}/"/></copy><!-- copy lib 檔案 --><copy todir="${dist.lib}"><fileset dir="${build.lib}/"/></copy></target><!--===================================================================Cleans everything(File Tasks)例如可以刪除build中的檔案,留給你發揮吧===================================================================--></project> 

build.xml多了些,但其實很簡單:(注釋比較詳細可以參照,這裡再簡單說一下)

一個build.xml包含一個工程的自動化處理的完整xml說明,並且基本由3種東東組成:

<project >

1.全域變數的定義
<property/>

2.工作群組
<target>
3.許多單項任務... 像copy,delete,javac,jar...
<task1/>
<task2/>
<task3/>
</target>

</project>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.