作者:手机用户2502893987 | 来源:互联网 | 2014-05-28 10:53
ant是jakarta一个非常好的OpenSource子项目,是基于java的编译工具。下面简单介绍一下在linux环境中如何安装ant:1.下载从http://ant.apache.org/bindownload.cgi可以下载最新的tar包:apache-ant-1.6.2.tar.gz,如果是windows环境则
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/tools/apache-ant-1.6.2,并将f:/project/tools/apache-
ant-1.6.2/bin目录添加到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%/bin目录全路径加入到%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}/webapps/WEB-INF/conf:${PROJECT_HOME}/webapps/WEB-INF/classes:$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:/tools/java
# 设置%ANT_HOME%
if "%ANT_HOME%"=="" set ANT_HOME=f:/tools/ant
# 设置PROJECT_HOME
set PROJECT_HOME = %CD%/..
set CLASSPATH_BAK=%CLASSPATH%
set CLASSPATH=
# 执行build
%ANT_HOME%/bin/ant.bat -buildfile ../build/build.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功能强大,需要通过相应的配置项来表现。
property="J2EE.present">
********************************************************
**
** J2EE has not been found and is needed for the target
** you have chosen
**
** Since CLASSPATH is an evil idea, just link or drop
** a copy of your j2ee.jar into build/lib directory.
**
*********************************************************
build init
build compile
sourcepath="${build.src}"
overview="${build.dir}/docs/overview.html"
packagenames="org.tangtang.*"
destdir="${build.dir}/docs/api"
encoding="${build.encoding}"
author="true"
version="true"
use="true"
link="${javadoc.link}"
windowtitle="${project} ${version} API"
doctitle="${project} ${version} API"
bottom="Copyright © ${year} tangtang.org. All Rights Reserved."
>
destdir="${build.dest}"
cache="${build.dest}">
destdir="${build.dest}"
debug="${debug}"
deprecation="${deprecation}"
optimize="${optimize}">
includes="**/*"
excludes="**/*.bat"
/>