您的位置:首页 > 其它

Ant 入门基础

2013-03-28 01:01 190 查看
ant 入门基础

1)下载ant(Ant 1.7.1 为例)

ant主页

http://jakarta.apache.org/ant/index.html

下载地址:

http://ant.apache.org/bindownload.cgi

2)装载ant

(1)解开jakarta-ant-1.7.1-bin.zip到一个目录D:/Program Files/apache-ant-1.7.1,

这个目录就叫ant目录

(2)将ant目录(D:/Program Files/apache-ant-1.7.1/bin)添加到系统path中。

3)应用ant的例子

(1)获得例子的模板

假设tomcat被安装在E:/jakarta-tomcat-5.5.9

模板在如下目录中:

“E:/jakarta-tomcat-5.5.9\webapps\tomcat-docs\appdev\sample”

建议你将此目录下内容拷贝至其他地方,以供学习。如拷贝到:"E:/sample"

其中:

build.xml 是ant构建应用程式的脚本。

docs\ 是你自己提供给用程式文件(不包含javadoc)的地方

src\ 等同于tomcat应用结构中的“根目录\web-inf\classes\"目录,

web\ 等同于tomcat应用结构中的根目录

web\web-inf\ 对应于tomcat应用结构中的“根目录\web-inf\”目录

但不包括“根目录\web-inf\classes\"目录

“根目录\web-inf\”用来存放class和servlet及

其他不允许用户直接访问的东东。

web\web-inf\web.xml 等同于tomcat应用结构中的“根目录\web-inf\web.xml”文件,

即应用发布描述

(2)在终端DOS窗口中,进入"E:/sample\"目录,运行命令“ant”。

ant会自动在此目录下建立目录:build,并在其中建立编译后的应用程式结构。

ant能自动发布这个目录,即将其拷贝到tomcat的webapps目录下。

(3)(4)执行不同的任务

在这个模板中,主要提供以下任务:

运行“ant clean”,则清除编译产生的文件结构,即删除build目录

运行“ant build”,则创建build目录、编译构建应用程式

运行“ant deploy”,则先执行build任务,再将build目录下内容发布到tomcat

运行“ant javadoc”,则先执行build任务,再创建dist目录,

并在此目录下自动生成应用程式javadoc

(5)build.xml的分析和修改

现在E:/jakarta-tomcat-5.5.9下建立clasess和lib文件夹

<project name="mytry" default="compile" basedir=".">

<property name="app.name" value="myapp"/>

<property name="app.version" value="1.0"/>

<property name="build.home" value="build"/>

<property name="catalina.home" value="E:/jakarta-tomcat-5.5.9/"/> <!-- update this! -->

<property name="deploy.home" value="${catalina.home}/webapps/${app.name}"/>

<property name="dist.home" value="dist"/>

<property name="compile.debug" value="true"/>

<property name="compile.deprecation" value="false"/>

<property name="compile.optimize" value="true"/>

<path id="compile.classpath">

<pathelement location="${catalina.home}/common/classes"/>

<fileset dir="${catalina.home}/common/lib">

<include name="*.jar"/>

</fileset>

<pathelement location="${catalina.home}/classes"/>

<fileset dir="${catalina.home}/lib">

<include name="*.jar"/>

</fileset>

</path>

<target name="all" depends="clean,compile"

description="clean build and dist, then compile"/>

<target name="clean"

description="delete old build and dist directories">

<delete dir="${build.home}"/>

<delete dir="${dist.home}"/>

</target>

<target name="compile" depends="prepare"

description="compile java sources">

<mkdir dir="${build.home}/web-inf/classes"/>

<javac srcdir="src"

destdir="${build.home}/web-inf/classes"

debug="${compile.debug}"

deprecation="${compile.deprecation}"

optimize="${compile.optimize}">

<classpath refid="compile.classpath"/>

</javac>

<copy todir="${build.home}/library/classes">

<fileset dir="src" includes="**/*.properties"/>

</copy>

</target>

<target name="deploy" depends="compile"

description="deploy application to servlet container">

<mkdir dir="${deploy.home}"/>

<copy todir="${deploy.home}">

<fileset dir="${build.home}"/>

</copy>

<mkdir dir="${deploy.home}/web-inf/lib"/>

</target>

<target name="dist" depends="deploy,javadoc"

description="create binary distribution">

<copy todir="${dist.home}/docs">

<fileset dir="docs"/>

</copy>

<jar jarfile="${dist.home}/${app.name}.war"

basedir="${deploy.home}"/>

</target>

<target name="javadoc" depends="compile"

description="create javadoc api documentation">

<mkdir dir="${dist.home}/docs/api"/>

<javadoc sourcepath="src"

destdir="${dist.home}/docs/api"

packagenames="mypackage.*"/>

</target>

<target name="prepare">

<mkdir dir="${build.home}"/>

<copy todir="${build.home}">

<fileset dir="web"/>

</copy>

</target>

</project>

============================================================================

以下是build.xml及其修改说明:(需修改的地方用汉字说明)

只需修改3到4处!适用于所有tomcat应用。

------------------ build.xml example for tomcat-------------------------------

<!-- a "project" describes a set of targets that may be requested

when ant is executed. the "default" attribute defines the

target which is executed if no specific target is requested,

and the "basedir" attribute defines the current working directory

from which ant executes the requested task. this is normally

set to the current working directory.

-->

<project name="my project" default="compile" basedir=".">

//将name的值改为应用程式的名字,即发布到tomcat的名字

//将default的值改为你需要的缺省任务(运行"ant"不指明任务时执行的任务)

//例如:<project name="mytry" default="deploy" basedir=".">

<!-- ===================== property definitions =========================== -->

<!--

each of the following properties are used in the build script.

values for these properties are set by the first place they are

defined, from the following list:

* definitions on the "ant" command line (ant -dcatalina.home=xyz compile)

* definitions from a "build.properties" file in the top level

source directory

* definitions from a "build.properties" file in the developers

home directory

* default definitions in this build.xml file

you will note below that property values can be composed based on the

contents of previously defined properties. this is a powerful technique

that helps you minimize the number of changes required when your development

environment is modified. note that property composition is allowed within

"build.properties" files as well as in the "build.xml" script.

-->

<!-- ==================== file and directory names ======================== -->

<!--

these properties generally define file and directory names (or paths) that

affect where the build process stores its outputs.

app.name base name of this application, used to

construct filenames and directories.

defaults to "myapp".

app.version version identifier for this application.

build.home the directory into which the "prepare" and

"compile" targets will generate their output.

defaults to "build".

catalina.home the directory in which you have installed

a binary distribution of tomcat 4. this will

be used by the "deploy" target.

deploy.home the name of the directory into which the

deployment hierarchy will be created, and into

which the build directory will be copied.

defaults to "${catalina.home}/webapps/${app.name}".

dist.home the name of the base directory in which

distribution files are created.

defaults to "dist".

-->

<property name="app.name" value="myapp"/>

//将value的值改为应用程式的名字,即发布到tomcat的名字

//例如:<property name="app.name" value="mytry"/>

<property name="app.version" value="1.0"/>

<property name="build.home" value="build"/>

<property name="catalina.home" value="../../../.."/> <!-- update this! -->

//将value的值改为你安装tomcat的路径

//例如:<property name="catalina.home" value="g:\jakarta-tomcat-4.0.1\"/>

<property name="deploy.home" value="${catalina.home}/webapps/${app.name}"/>

<property name="dist.home" value="dist"/>

<!-- ==================== compilation control options ==================== -->

<!--

these properties control option settings on the javac compiler when it

is invoked using the <javac> task.

compile.debug should compilation include the debug option?

compile.deprecation should compilation include the deprecation option?

compile.optimize should compilation include the optimize option?

-->

<property name="compile.debug" value="true"/>

<property name="compile.deprecation" value="false"/>

<property name="compile.optimize" value="true"/>

<!-- ==================== external dependencies =========================== -->

<!--

use property values to define the locations of external jar files on which

your application will depend. in general, these values will be used for

two purposes:

* inclusion on the classpath that is passed to the javac compiler

* being copied into the "/web-inf/lib" directory during execution

of the "deploy" target.

because we will automatically include all of the java classes that tomcat 4

exposes to web applications, we will not need to explicitly list any of those

dependencies. you only need to worry about external dependencies for jar

files that you are going to include inside your "/web-inf/lib" directory.

-->

<!-- dummy external dependency -->

<!--

<property name="foo.jar"

value="/path/to/foo.jar"/>

-->

<!-- ==================== compilation classpath =========================== -->

<!--

rather than relying on the classpath environment variable, ant includes

features that makes it easy to dynamically construct the classpath you

need for each compilation. the example below constructs the compile

classpath to include the servlet.jar file, as well as the other components

that tomcat makes available to web applications automatically, plus anything

that you explicitly added.

-->

<path id="compile.classpath">

<!-- include all jar files that will be included in /web-inf/lib -->

<!-- *** customize here as required by your application *** -->

<!--

<pathelement location="${foo.jar}"/>

-->

<!-- include all elements that tomcat exposes to applications -->

<pathelement location="${catalina.home}/common/classes"/>

<fileset dir="${catalina.home}/common/lib">

<include name="*.jar"/>

</fileset>

<pathelement location="${catalina.home}/classes"/>

<fileset dir="${catalina.home}/lib">

<include name="*.jar"/>

</fileset>

</path>

<!-- ==================== all target ====================================== -->

<!--

the "all" target is a shortcut for running the "clean" target followed

by the "compile" target, to force a complete recompile.

-->

<target name="all" depends="clean,compile"

description="clean build and dist, then compile"/>

<!-- ==================== clean target ==================================== -->

<!--

the "clean" target deletes any previous "build" and "dist" directory,

so that you can be ensured the application can be built from scratch.

-->

<target name="clean"

description="delete old build and dist directories">

<delete dir="${build.home}"/>

<delete dir="${dist.home}"/>

</target>

<!-- ==================== compile target ================================== -->

<!--

the "compile" target transforms source files (from your "src" directory)

into object files in the appropriate location in the build directory.

this example assumes that you will be including your classes in an

unpacked directory hierarchy under "/web-inf/classes".

-->

<target name="compile" depends="prepare"

description="compile java sources">

<!-- compile java classes as necessary -->

<mkdir dir="${build.home}/web-inf/classes"/>

<javac srcdir="src"

destdir="${build.home}/web-inf/classes"

debug="${compile.debug}"

deprecation="${compile.deprecation}"

optimize="${compile.optimize}">

<classpath refid="compile.classpath"/>

</javac>

<!-- copy associated resource files -->

<copy todir="${build.home}/library/classes">

<fileset dir="src" includes="**/*.properties"/>

</copy>

</target>

<!-- ==================== deploy target =================================== -->

<!--

the "deploy" target copies the contents of the build directory into a

location required by our servlet container, and picks up any external

dependencies along the way. after restarting the servlet container, you

can now test your web application.

-->

<target name="deploy" depends="compile"

description="deploy application to servlet container">

<!-- copy the contents of the build directory -->

<mkdir dir="${deploy.home}"/>

<copy todir="${deploy.home}">

<fileset dir="${build.home}"/>

</copy>

<!-- copy external dependencies as required -->

<!-- *** customize here as required by your application *** -->

<mkdir dir="${deploy.home}/web-inf/lib"/>

<!--

<copy todir="${deploy.home}/web-inf/lib" file="${foo.jar}"/>

-->

</target>

<!-- ==================== dist target ===================================== -->

<!--

the "dist" target creates a binary distribution of your application

in a directory structure ready to be archived in a tar.gz or zip file.

note that this target depends on two others:

* "deploy" so that the entire web application (including external

dependencies) will have been assembled

* "javadoc" so that the application javadocs will have been created

-->

<target name="dist" depends="deploy,javadoc"

description="create binary distribution">

<!-- copy documentation subdirectory -->

<copy todir="${dist.home}/docs">

<fileset dir="docs"/>

</copy>

<!-- create application jar file -->

<jar jarfile="${dist.home}/${app.name}.war"

basedir="${deploy.home}"/>

<!-- copy additional files to ${dist.home} as necessary -->

</target>

<!-- ==================== javadoc target ================================== -->

<!--

the "javadoc" target creates javadoc api documentation for the java

classes included in your application. normally, this is only required

when preparing a distribution release, but is available as a separate

target in case the developer wants to create javadocs independently.

-->

<target name="javadoc" depends="compile"

description="create javadoc api documentation">

<mkdir dir="${dist.home}/docs/api"/>

<javadoc sourcepath="src"

destdir="${dist.home}/docs/api"

packagenames="mypackage.*"/>

//改为需要制作javadoc的包名。如果不做javadoc,这里不必改。

//例如:packagenames="see.*"/>

</target>

<!-- ==================== prepare target ================================== -->

<!--

the "prepare" target is used to create the "build" destination directory,

and copy the static contents of your web application to it. if you need

to copy static files from external dependencies, you can customize the

contents of this task.

normally, this task is executed indirectly when needed.

-->

<target name="prepare">

<!-- create build directory and copy static content -->

<mkdir dir="${build.home}"/>

<copy todir="${build.home}">

<fileset dir="web"/>

</copy>

<!-- copy static files from external dependencies as needed -->

</target>

</project>

========================================================================

补充我们常用的ANT

bulild,xml

<?xml version="1.0" encoding="gb2312" ?>

<!--

======================================================================

Created on 2005-2-22

$id$

======================================================================

-->

<project name="UNICOM_MODULE" default="usage" basedir=".">

<description>XX应用程序模块工程构建脚本模板</description>

<!--

=================================

Global Properties.

=================================

-->

<property file="build.properties"/>

<!--

=================================

Global Path.

=================================

-->

<path id="compile.dependency">

<pathelement location="${lib}"/>

<fileset dir="${lib}">

<include name="**/*.jar"/>

</fileset>

</path>

<!--

=================================

target: compile.clean

=================================

-->

<!--

<target name="compile.clean">

<delete includeemptydirs="true">

<fileset dir="${build.classes}" includes="**/*" />

</delete>

</target>

-->

<!--

=================================

target: compile

=================================

-->

<target name="compile">

<javac destdir="${classes}" debug="on">

<src path="${src}"/>

<classpath refid="compile.dependency"/>

</javac>

<copy todir="${classes}">

<fileset dir="${src}" excludes="**/*.java"/>

</copy>

</target>

<!--

=================================

target: compile.rmi

=================================

-->

<!--

<target name="compile.rmi" depends="compile">

<rmic base="${build.classes}" includes="**/*Remote.class" excludes="**/I*Remote.class"/>

</target>

-->

<!--

=================================

target: build.jar

=================================

-->

<target name="build.jar">

<jar destfile="${build}/${project}-${module}.jar">

<fileset dir="${classes}">

<include name="**/*"/>

<exclude name="*"/>

</fileset>

</jar>

</target>

<!--

=================================

target: build.src

=================================

-->

<target name="build.src">

<zip destfile="${build}/${project}-${module}.zip">

<fileset dir="${src}">

<include name="**/*"/>

<exclude name="*"/>

</fileset>

</zip>

</target>

<!--

=================================

target: build.web

=================================

-->

<target name="build.web">

<copy todir="${web}/WEB-INF/lib" flatten="true">

<fileset dir="${build}">

<include name="*.jar"/>

</fileset>

<fileset dir="lib">

<include name="**/*.jar"/>

<exclude name="**/servlet.jar"/>

<exclude name="**/ejb.jar"/>

<exclude name="**/jta.jar"/>

</fileset>

</copy>

<copy todir="${web}/WEB-INF/classes">

<fileset dir="classes">

<include name="*.*"/>

</fileset>

</copy>

</target>

<!--

=================================

target: build.web

=================================

-->

<target name="build.web.all" depends="compile,build.jar,build.src,build.web"/>

<!--

=================================

target: build.ear

=================================

-->

<target name="build.ear" depends="build.web.all">

<jar destfile="${earapp}/${module}.war">

<fileset dir="${web}">

<include name="**/*"/>

</fileset>

</jar>

<jar destfile="${build}/${project}-${module}.ear">

<fileset dir="${earapp}">

<include name="**/*"/>

</fileset>

</jar>

</target>

<!--

=================================

target: build.clean

=================================

-->

<target name="build.clean">

<delete includeemptydirs="true">

<fileset dir="${web}/WEB-INF" includes="**/*" excludes="*"/>

<fileset dir="${build}" includes="*"/>

<fileset dir="${earapp}" includes="*" excludes="META-INF"/>

<fileset dir="${classes}" includes="**/*"/>

</delete>

</target>

<!--

=================================

target: build.clean

=================================

-->

<target name="build.cleanjbx">

<delete includeemptydirs="true">

<fileset dir="${src}" includes="**/*.jbx" excludes="*"/>

</delete>

</target>

<!--

=================================

target: usage

=================================

-->

<target name="usage">

<echo>${workspace}</echo>

</target>

</project>

下面是读取的build.properties文件

# $id$

workspace = ..

project =pf-1.0.2-BETA

module = 091218

# websphere, weblogic

server = weblogic

classes = sample/WEB-INF/classes

src = src

test = test

lib = lib

web = sample

build = build

earapp = earapp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: