ant安裝與使用 – JAVA編程語言程序開發技術文章

ant是jakarta一個非常好的OpenSource子項目,是基於java的編譯工具。下面簡單介紹一下在linux環境中如何安裝ant:

  1.下載
  從 http://ant.apache.org/bindownload.cgi 可以下載最新的tar包:apache-ant-1.6.2.tar.gz,如果是windows環境則是zip文件,解壓後,在系統環境變量裡設置 ANT_HOME為f:project oolsapache-ant-1.6.2,並將f:project oolsapache- ant-1.6.2in目錄添加到classpath中,然後就可以使用瞭

  2./l安裝,解壓到/usrocal下
> tar zxpvf apache-ant-1.6.2.tar.gz
> ln -s apache-ant-1.6.2 ant

  3.設置環境
  將ANT_HOME設置到當前用戶的.bash_profile文件/home/admin/.bash_profile

[admin@tangtang home]$ su – admin
[admin@tangtang home]$ vi .bash_profile
export ANT_HOME=/usr/local/ant
export PATH=/usr/local/ant/bin:$PATH

  如果是windows環境,需要設置%ANT_HOME%,並把%ANT_HOME%in目錄全路徑加入到%path%中

  4.測試
  用ant命令測試運行情況
[admin@tangtang home]$ ant
Buildfile: build.xml does not exist!
Build failed

[admin@tangtang home]$ ant -version
Apache Ant version 1.6.2 compiled on July 16 2004

  若出現這樣的錯誤:Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/Launcher
  這是linux系統默認安裝瞭一些ant的lib,修改 /etc/ant.conf中 ANT_HOME=/usr/share/ant 為你正確安裝ant的地址,如 /usr/local/ant

  5、build腳本
  如果是在linux環境中,下面是build腳本的一個范例:

#build腳本
#! /bin/sh
# 進入到上級目錄
cd `dirname $0`/..
# 獲取當前目錄為PROJECT_HOME
PROJECT_HOME=`pwd`
# 設置JAVA_HOME
export JAVA_HOME=/usr/cyber/java
# 得到CLASSPATH
CLASSPATH1=$CLASSPATH
# 添加CLASSPATH
CLASSPATH=${PROJECT_HOME}webappsWEB-INFconf:${PROJECT_HOME}webapps
WEB-INFclasses:$CLASSPATH
# ant build,-buildfile 參數,是使用自定義的build.xml文件,$@是參數數組
/usr/local/ant/bin/ant -buildfile ${PROJECT_HOME}/build/build.xml “$@”
# build腳本結束

  如果是在windows環境中,下面是build.bat腳本的一個范例:
# build.bat 
# 關閉echo顯示
@echo off
# 設置%JAVA_HOME%
if “%JAVA_HOME%”==”” set JAVA_HOME=f: oolsjava
# 設置%ANT_HOME%
if “%ANT_HOME%”==”” set ANT_HOME=f: oolsant
# 設置PROJECT_HOME
set PROJECT_HOME = %CD%..
set CLASSPATH_BAK=%CLASSPATH%
set CLASSPATH=
# 執行build
%ANT_HOME%inant.bat -buildfile ..uilduild.xml %1 %2 %3 %4 %5 %6 %7 %8 %9

  6、build配置文件
  在${PROJECT_HOME}/build目錄下面,需要定義兩個文件,一個是build.properties,一個是build.xml
  build.properties文件定義瞭build的一些常量
# build.properties
project = tangtang
version = 1.1.1
# 采用classic編譯,即采用ant編譯
build.compiler = classic
# 采用jikes編譯
#build.compiler = jikes

year = 2004
debug = on
optimize = on
deprecation = on

os = linux
author = tangtang
email = syvin_tom@hotmail.com
url = www.tangtang.org
company = tangtang.org

  build.xml文件是ant編譯的主要配置文件,ant功能強大,需要通過相應的配置項來表現。
<?xml version=”1.0″ encoding=”gb2312″?>

<!– Build file for project –>

<project name=”cyber” default=”jar” basedir=”.”>
<property file=”${user.home}/.ant.properties” />
<!– ant build properties –>
<property file=”build.properties”/>

<property name=”build.dir” value=”..”/>
<property name=”build.src” value=”${build.dir}/webapps/WEB-INF/src/java”/>
<property name=”build.dest” value=”${build.dir}/webapps/WEB-INF/classes”/>
<property name=”build.lib” value=”${build.dir}/webapps/WEB-INF/lib”/>
<property name=”build.ext” value=”./lib”/>
<property name=”build.tpl” value=”${build.dir}/webapps/WEB-INF/templates”/>
<property name=”build.encoding” value=”gb2312″/>

<property name=”src.java.dir” value=”../src/java”/>
<property name=”javadoc.destdir” value=”../docs/api”/>
<property name=”javadoc.link” value=”http://www.tangtang.org/java/docs/api/”/>
<property name=”final.name” value=”${project}-${version}”/>
<property name=”dist.root” value=”${build.dir}/dist”/>
<property name=”dist.dir” value=”${dist.root}/${final.name}”/>

<path id=”classpath”>
<pathelement path=”${java.class.path}/”/>
<fileset dir=”${build.lib}”>
<include name=”*.jar”/>
</fileset>
<fileset dir=”${build.ext}”>
<include name=”*.jar”/>
</fileset>
</path>

<property name=”classpath” refid=”classpath”/>
<!– =================================================================== –>
<!– prints the environment –>
<!– =================================================================== –>
<target name=”env”>
<echo message=”build.compiler = ${build.compiler}”/>
<echo message=”java.home = ${java.home}”/>
<echo message=”user.home = ${user.home}”/>
<!–echo message=”java.class.path = ${java.class.path}”/–>
<echo message=”classpath = ${classpath}”/>
</target>

<!– =================================================================== –>
<!– Prepares the build directory –>
<!– =================================================================== –>
<target name=”prepare” depends=”env”>

<tstamp/>
<filter token=”year” value=”${year}”/>
<filter token=”version” value=”${version}”/>
<filter token=”date” value=”${DSTAMP}”/>
<!–
<mkdir dir=”${build.dir}”/>
<mkdir dir=”${build.dest}”/>
<mkdir dir=”${build.src}”/>
–>
<!– chose a class thats from j2ee.

發佈留言