一、構建Ant環境
①免安裝構建:如果你已經配置好Eclipse環境(3.0以上),那Ant環境就已經構建好了;
②從零開始手工配置:
A:配置好Java環境;
安裝JDK,然后設置好環境變量,如:
Java 環境配置:
Java安裝目錄:E:\Java\jdk1.5.0_12
在“系統”-》“高級”-》“環境變量”-》“系統參數”中
1、新增JAVA_HOME:
變量名:JAVA_HOME
變量值:E:"Java\jdk1.5.0_12
2、新增CLASSPATH:
變量名:CLASSPATH
變量值:.;%JAVA_HOME%\lib\tool.jar;%JAVA_HOME%\lib\dt.jar;
3、在Path變量中增加Java的bin目錄
已存在的變量名:Path
在最前面新增的值:.;%JAVA_HOME%\bin;
B、下載Ant包:
地址:http://ant.apache.org/bindownload.cgi
配置Ant環境變量:
Ant 環境配置:
Ant安裝目錄:D:\ant
在“系統”-》“高級”-》“環境變量”-》“系統參數”中
1、新增ANT_HOME:
變量名:ANT_HOME
變量值:D:\ant
2、在Path變量中增加Ant的bin目錄
已存在的變量名:Path
在最前面新增的值:.;%ANT_HOME%"bin;
測試:run-》cmd,輸入ant,如果出現下面的畫面,測表明Ant環境已經配置好了。

二、簡單上手:
build.xml
<?xml version="1.0"?>
<project default="main" basedir=".">
<target name="main">
<javac srcdir="src\main\hello\ant" destdir="bin"/>
<java classname="main.hello.ant.HelloAnt">
<classpath>
<pathelement path="bin" />
</classpath>
</java>
</target>
</project>
HelloAnt.java
package main.hello.ant;
public class HelloAnt {
public static void main(String[] args) {
System.out.println("Hello World From HelloAnt!");
}
}
目錄結構:
運行結果:
三、Ant提高
改進build.xml,讓它做更多的事情:
定義全局變量
初始化,主要是建立目錄
編譯(已有)
打包為jar
建立API documentation
生成 發布(distribution) 產品
build.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project default="dist" basedir=".">
<!-- 主要的系統環境屬性 -->
<!-- 取Window,Unix
的環境變量 -->
<property environment="env" />
<property name="java.home" value="${env.JAVA_HOME}" />
<property name="ant.home" value="${env.ANT_HOME}" />
<!-- 主要的app環境屬性 -->
<property name="app.name" value="hello-ant" />
<property name="app.jar" value="${app.name}.jar" />
<property name="app.copyright" value=" Copyright(c) 2007 CoderDream's Studio All rights reserved." />
<!-- app中的src屬性 -->
<property name="src.dir" value="src" />
<property name="src.main" value="${src.dir}/main" />
<property name="src.script" value="${src.dir}/script" />
<!-- app用到的lib -->
<property name="lib.dir" value="lib" />
<!-- app的build目錄中 -->
<property name="build.dir" value="build" />
<property name="build.classes" value="${build.dir}/classes" />
<property name="build.docs" value="${build.dir}/doc" />
<property name="build.docs.api" value="${build.docs}/api" />
<property name="build.lib" value="${build.dir}/lib" />
<!-- app的dist(distribution發布)目錄中 -->
<property name="dist.dir" value="dist" />
<property name="dist.bin" value="${dist.dir}/bin" />
<property name="dist.docs" value="${dist.dir}/docs" />
<property name="dist.lib" value="${dist.dir}/lib" />
<!-- app的docs目錄中 -->
<property name="docs.dir" value="docs" />
<property name="report" value="report" />
<path id="classpath">
<pathelement location="${build.classes}" />
<pathelement path="${java.home}/lib/tools.jar" />
</path>
<target name="init">
<!-- 清除以前的目錄 -->
<delete dir="${build.dir}" failonerror="false" />
<delete dir="${dist.dir}" failonerror="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>
<target name="build" depends="init">
<javac srcdir="${src.dir}" destdir="${build.classes}">
<classpath refid="classpath" />
</javac>
</target>
<target name="jars" depends="build">
<jar basedir="${build.classes}" jarfile="${build.lib}/${app.jar}" />
</target>
<target name="javadocs" depends="jars" description="-> creates the API documentation">
<!-- copy docs 手冊 -->
<copy todir="${build.docs}">
<fileset dir="${docs.dir}" />
</copy>
<javadoc packagenames="main.hello.ant.*"
sourcepath="${src.dir}"
defaultexcludes="yes"
destdir="${build.docs.api}"
author="true"
version="true"
use="true"
windowtitle="Docs API">
<doctitle>
<![CDATA[<h1>hello and Docs API</h1>]]>
</doctitle>
<bottom>
<![CDATA[<i>${app.copyright}</i>]]>
</bottom>
<tag name="todo" scope="all" description="To do:" />
</javadoc>
</target>
<target name="dist" depends="javadocs">
<!-- copy bin 執行文件 -->
<copy todir="${dist.bin}">
<fileset dir="${src.script}/" />
</copy>
<!-- copy doc 執行文件 -->
<copy todir="${dist.docs}">
<fileset dir="${build.docs}/" />
</copy>
<!-- copy lib 執行文件 -->
<copy todir="${dist.lib}">
<fileset dir="${build.lib}/" />
</copy>
</target>
<target name="junitreport" depends="build">
<junit printsummary="true" failureproperty="tests.failed">
<test name="main.hello.ant.TestHelloAnt"/>
<classpath>
<pathelement location="${build.classes}"/>
</classpath>
</junit>
<junitreport todir="${report}">
<fileset dir="${report}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${report}"/>
</junitreport>
<fail if="tests.failed">
--fail!--
</fail>
</target>
</project>
執行結果:
1、Console信息:
Buildfile: E:\XL\workspace\Ant03\build.xml
init:
[delete] Deleting directory E:\XL\workspace\Ant03\build
[delete] Deleting directory E:\XL\workspace\Ant03\dist
[mkdir] Created dir: E:\XL\workspace\Ant03\build
[mkdir] Created dir: E:\XL\workspace\Ant03\build\classes
[mkdir] Created dir: E:\XL\workspace\Ant03\build\doc
[mkdir] Created dir: E:\XL\workspace\Ant03\build\doc\api
[mkdir] Created dir: E:\XL\workspace\Ant03\build\lib
[mkdir] Created dir: E:\XL\workspace\Ant03\dist
[mkdir] Created dir: E:\XL\workspace\Ant03\dist\bin
[mkdir] Created dir: E:\XL\workspace\Ant03\dist\lib
build:
[javac] Compiling 1 source file to E:\XL\workspace\Ant03\build\classes
jars:
[jar] Building jar: E:\XL\workspace\Ant03\build\lib\hello-ant.jar
javadocs:
[javadoc] Generating Javadoc
[javadoc] Javadoc execution
[javadoc] Loading source files for package main.hello.ant
[javadoc] Constructing Javadoc information
[javadoc] Standard Doclet version 1.5.0_12
[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 E:\XL\workspace\Ant03\build\doc\api\stylesheet.css
[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
dist:
[copy] Copying 17 files to E:\XL\workspace\Ant03\dist\docs
[copy] Copying 1 file to E:\XL\workspace\Ant03\dist\lib
BUILD SUCCESSFUL
Total time: 4 seconds
2、生成jar文件和docs API
源代碼下載
參考:
1、
Ant入門教程
2、
ant使用教程
3、劉曉濤 第.2.章:.Java就業特訓--2、Java.構建工具.ANT 視頻
posted on 2007-09-18 16:51
CoderDream 閱讀(2272)
評論(1) 編輯 收藏 所屬分類:
經驗點滴