使用ant编程(转)

本文介绍了基于Ant的项目构建配置,包括目录结构划分,如work/common下各配置文件及lib、template目录用途。详细说明了build_common.xml等文件内容,以及与Tomcat配合的操作和配置。还提及用Ant弥补CVS版本控制不足,最后给出使用Ant环境进行工程的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 目录结构划分
  假设你有一个工作目录为: /home/camry/work, 以下简称 work
  为了适应多个项目的进行, 我配置了一套基本的配置文件放在 work/common
  下. 目录结构是这样的:

  work/common/
      build_common.xml        # 这个文件包含基本的构建操作
      common.xml              # 这个文件为 build_common.xml 作配置,
                                基本上不用改动.
      build_tomcat.xml        # 这个文件包含了与tomcat合作的基本操作.
      tomcat.xml              # 这个文件为 build_tomcat.xml 作配置,
                                基本上只需要配置一次.
      build.xml               # 这个文件是每个项目都需要的 build 配置,
                                但是基本上也不需要改动了.
      build.properties        # 这个文件为 build.xml 做配置,
                                与具体的项目相关.
      usage.txt               # 这个文件说明了构建过程中的各种操作.

      lib/                    # 这个目录放置一些公用的 jar 包免得重复.
        checkstyle-all-3.4.jar    # 用于代码检查
        httpunit.jar              # 用于 http 单元测试
        junit.jar                 # 用于单元测试
        servlet-api.jar           # 用于编写 servlet 相关文件

      template/               # 这个目录是套项目模版, 一个项目开始时
                                将把这个目录的数据复制到项目目录下以便
                                直接使用.
        bin/                  # 这个目录放置可执行文件
        build/                # 这个目录放置构建时需要的辅助文件
          checkstyle_checks.xml   # 这个文件是 checkstyle 的配置文件
          java.header             # 这个文件也是 checkstyle 配置文件
                                    用于说明你的 java 文件的头部构造
        dist/                 # 这个目录放置目标文件
          classes/            # 编译后产生的 class 文件放在这
          lib/                # 打包后产生的 jar 文件放这
        lib/                  # 这个目录放置项目相关的 jar 文件
        src/                  # 这个目录放置源代码
          build.xml           # 这个文件是用于与 cvs 配合工作的配置文件
          main/               # 这个目录中放置主要的源代码
          test/               # 这个目录中放置测试代码

摘要: build_common.xml, common.xml 及 usage.txt

2. 介绍 build_common.xml, common.xml 及 usage.txt

. 以下为 build_common.xml 文件内容.


<?xml version="1.0" encoding="ISO-8859-1"?>
<!--

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->
<project name="Common build file" default="all" basedir="">

  <description>
    这是一个通用的 ant build 文件. Version 1.0.
    感谢: 这篇文档借用了很多 Johan 的想法, 他的主页在 http://dev.kanngard.net
  </description>

  <!-- 读入 common.xml 配置 -->
  <xmlproperty  file="${path.common}/common.xml"
                semanticAttributes="true"
                keepRoot="false"/>

  <!-- 设置编译时的 classpath -->
  <path id="compile.classpath">
    <fileset dir="${path.common}/lib">
      <include name="*.jar"/>
    </fileset>
    <fileset dir="${lib}">
      <include name="*.jar"/>
    </fileset>
    <pathelement location="${dist.classes}"/>
  </path>

  <!-- 设置运行时的 classpath -->
  <path id="run-time.classpath">
    <path refid="compile.classpath"/>
  </path>

  <!-- 初始化过程, 会调用具体项目的初始化过程先 -->
  <target name="init"
          depends="project_init"
          description="Initialize environment">
  </target>

  <!-- 显示使用说明 -->
  <target name="usage">
    <loadfile property="usage.message"
              srcFile="${path.common}/usage.txt"/>
    <echo message="${usage.message}"/>
  </target>

  <!-- 预备, 通常在项目刚建立的时候运行此任务 -->
  <target name="common_prepare"
          depends="init" description="Prepare build directory">
    <copy todir="." preservelastmodified="true">
      <fileset dir="${path.common}/template/">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>

  <!-- 配置 checkstyle , 只检查 src/main 目录下的源代码 -->
  <target name="checkstyle" depends="init">
    <taskdef  resource="checkstyletask.properties"
              classpath="${path.common}/lib/checkstyle-all-3.4.jar"/>
    <checkstyle config="${build}/checkstyle_checks.xml">
      <fileset dir="${src.main}" includes="**/*.java"/>
      <property key="checkstyle.cache.file"
                file="${path.common}/lib/checkstyle.cache"/>
      <classpath refid="compile.classpath"/>
    </checkstyle>
  </target>

  <!-- 编译 src 下源文件, class 文件放到 dist/classes 下 -->
  <target name="compile" depends="init" description="Compile source">
    <mkdir dir="${dist}/classes"/>
    <javac  debug="${compile.debug}"
            deprecation="${compile.deprecation}"
            destdir="${dist}/classes"
            target="${compile.jdk-version.target}"
            source="${compile.jdk-version.source}"
            optimize="${compile.optimize}"
            srcdir="${src}">
      <classpath refid="compile.classpath"/>
    </javac>
  </target>

  <!-- 清除 dist 目录 -->
  <target name="clean"
          depends="project_clean"
          description="Wipeout all generated files">
    <delete dir="${dist}/classes"/>
    <delete dir="${dist.lib}"/>
    <mkdir dir="${dist}/classes"/>
    <mkdir dir="${dist.lib}"/>
  </target>

  <!-- 打包 -->
  <target name="jar"
          depends="checkstyle, compile"
          description="Create binary distribution">
    <mkdir dir="${dist}/classes"/>
    <mkdir dir="${dist.lib}"/>
    <copy file="LICENSE" todir="${dist}/classes"/>
    <delete>
      <fileset dir="${dist.lib}" includes="*.jar"/>
    </delete>
    <jar  basedir="${dist}/classes"
          jarfile="${dist.lib}/${component.name}-${component.version}.jar">
      <include name="**/*.class"/>
      <exclude name="**/Test*.class"/>
    </jar>

    <delete>
      <fileset dir="${dist}/classes">
        <include name="LICENSE"/>
      </fileset>
    </delete>
  </target>

  <!-- 运行 dist/classes 目录下的所有以 Test 开头的测试类 -->
  <target name="test" depends="compile" description="run junit tests">
    <junit printsummary="on"
           fork="false"
           haltonfailure="false"
           failureproperty="tests.failed"
           showoutput="true">
      <classpath refid="run-time.classpath"/>
      <formatter type="brief" usefile="false"/>
      <batchtest>
        <fileset dir="${dist}/classes">
          <include name="**/Test*.*"/>
        </fileset>
      </batchtest>
    </junit>

    <fail if="tests.failed">
    *********************************************************
    *********************************************************
    ****  One or more tests failed! Check the output...  ****
    *********************************************************
    *********************************************************
    </fail>
  </target>

  <!-- 发布, 会调用具体项目的发布任务 -->
  <target name="deploy"
          depends="project_deploy" description="Deploy application"/>

  <!-- 创建文档 -->
  <target name="javadoc"
          depends="compile"
          description="Create component Javadoc documentation">
    <delete dir="${docs.api}"/>
    <mkdir dir="${docs.api}"/>
    <javadoc  author="true"
              bottom="${component.title}"
              destdir="${docs.api}"
              source="${compile.jdk-version.source}"
              doctitle="${component.title}"
              packagenames="*"
              access="protected"
              sourcepath="${src.main}"
              version="true"
              windowtitle="${component.title} (Version ${component.version})">
      <classpath refid="compile.classpath"/>
    </javadoc>
  </target>

  <!--  根据 cvs 配置, 从 cvs 服务器上下载源代码 -->
  <target name="fetch"
          depends="project_fetch" description="fetch current source from cvs">
  </target>

  <!-- 将源代码文件打包, 备份 -->
  <target name="src-zip"
          depends="checkstyle,compile"
          description="Creates source distribution">
    <copy file="LICENSE" todir="${src.main}"/>
    <delete>
      <fileset dir="${dist.lib}" includes="*-src.zip"/>
    </delete>
    <zip  basedir="."
          destfile="${dist.lib}/${component.name}-${component.version}-src.zip"
          whenempty="fail">
      <include name="**/*.*"/>
      <include name="*"/>
      <include name="**/*"/>
      <exclude name="${dist}/**/*.*"/>
      <exclude name="*.*~"/>      <!-- vi(JEdit) backups -->
      <exclude name=".nbattrs"/>  <!-- Netbeans filesystem attributes -->
      <exclude name="*.old"/>
    </zip>
  </target>

  <!--
    将源文件上传到服务器备份, 将目标文件上传到服务器发布.
    此任务中内容由各位自行设定, 一般是运行 ftp 任务.
  -->
  <target name="publish"
          depends="clean,jar,javadoc,src-zip" description="publish project">
  </target>

  <!-- 构建 -->
  <target name="all"
          depends="clean,fetch,test,jar,javadoc,deploy"
          description="build project"/>
</project>

 


. 以下为 common.xml 文件内容.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Common Ant build environment
-->

<root>
  <!--
    路径配置, 基本不用改动.
    但是假如你在一个陌生的环境中使用这套 build 工具,
    也许需要稍为修改一下这些路径的配置.
  -->
  <bin value="bin"/>
  <build value="build">
    <lib value="${build}/lib"/>
  </build>
  <dist value="dist">
    <lib value="${dist}/lib"/>
    <classes id="dist.classes" location="${dist}/classes"/>
  </dist>
  <docs value="docs">
    <api value="${docs}/api"/>
  </docs>
  <lib value="lib"/>
  <src value="src">
    <main value="${src}/main"/>
    <test value="${src}/test"/>
  </src>

  <!-- 编译选项 -->
  <compile>
    <debug value="true"/>
    <deprecation value="true"/>
    <jdk-version>
      <source value="1.4"/>
      <target value="1.4"/>
    </jdk-version>
    <optimize value="true"/>
  </compile>

</root>

 


. 以下为 usage.txt 文件内容.

 ${component.name} build file
---------------------------------------------

Available targets are:

prepare             --> 初始化项目环境
checkstyle          --> 检查源文件编码规范
compile             --> 编译
jar                 --> 打包
build               --> 构建
test                --> 测试
clean               --> 清除
deploy              --> 发布到 web 服务器运行
publish             --> 发布产品(源码及目标文件)
fetch               --> 从 cvs 上获取最新资源
docs                --> 创建 api 文档
all                 --> 执行 clean, fetch, build, test, doc, deploy
main                --> 默认的构建过程(自行修改)


摘要: build.xml, build.properties

2. 介绍 build.xml, build.properties

. 以下为 build.xml 文件内容.


<?xml version="1.0" encoding="ISO-8859-1"?>
<!--

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->
<project name="App" basedir="." default="compile">
  <property file="build.properties"/>

  <!-- 载入 common build 内容 -->
  <import file="${path.common}/build_common.xml"/>
  <!--
    假如项目要和 tomcat 一起配合, 那么载入 tomcat 的配置 -->
    <import file="${path.common}/build_tomcat.xml"/>
  -->

  <!-- 项目初始化内容 -->
  <target name="project_init" description="Initialize project environment">
    <available property="junit.present" classname="junit.framework.TestCase"/>
    <!-- 这里可以加入你自己的项目任务 -->
  </target>

  <!-- 项目预备内容, 一般在新建立一个项目的时候运行本任务 -->
  <target name="prepare"
          depends="common_prepare" description="Prepare build directory">
    <!-- 这里可以加入你自己的项目任务 -->
  </target>

  <!-- 从 cvs 服务器上下载源程序 -->
  <target name="project_fetch"
          depends="init" description="fetch src from cvs or another location">
    <ant dir="${src}" target="fetch"/>
  </target>

  <!-- 清除 -->
  <target name="project_clean"
          depends="init" description="Project-level prepare phase">
    <!-- 这里可以加入你自己的项目任务 -->
  </target>

  <!-- 发布 -->
  <target name="project_deploy" description="Deploy application">
  <!--
    假如项目要和 tomcat 一起配合, 那么调用 tomcat 的任务
    <antcall target="deploy_tomcat"/>
  -->
  </target>

  <!-- 反发布 -->
  <target name="undeploy" description="Un-Deploy application">
  <!--
    假如项目要和 tomcat 一起配合, 那么调用 tomcat 的任务
    <antcall target="undeploy_tomcat"/>
  -->
  </target>

  <!-- 构建应用系统 -->
  <target name="main" depends="test,jar,javadoc,publish" description="build">
    <!-- 这里可以加入你自己的项目任务 -->
  </target>
</project>

 


. 以下为 build.properties 文件内容 # 初始化环境

# common 文件的位置, 假如在 windows 下可能要这么写: D:/work/common
path.common=/home/camry/work/common

# 项目相关信息
component.name=App
component.package=com.vitular.app
component.title=App
component.version=0.1a

# cvs root 位置
CVSROOT=:pserver:camry@x.x.x.x:/usr/cvsroot

# ftp 设置
# ftp.server=x.x.x.x              # 远程服务器位置
# ftp.user=rat                      # 用户名
# ftp.pass=*****                  # 用户密码
# remote.dir=/opt/release         # 远程发布目录位置


摘要: checkstyle_checks.xml, java.header

现在已经有了目录结构, 有了 build_common.xml, common.xml, usage.txt,
build.xml, build.properties 等文件, 已经可以完成大部分任务了.
现在介绍如何进行辅助任务, 比如代码检查.

代码检查在 build_common.xml 中已经有这个任务了, 但是还需要两个配置
文件配合, 还需要到这里下载 checkstyle 的 jar 包并将
它放到 work/common/lib/ 目录下.

这两个配置文件是: work/common/template/build/ 目录下的
checkstyle_checks.xml 文件和 java.header 文件
checkstyle_checks.xml 文件说明了对 java 文件应该如何进行检查,
java.header 文件指出每个 java 文件的头部构造.

. checkstyle_checks.xml 文件如下, 是借用的Johan
的源文件, 作了一点儿修改. 其中每项配置的具体说明都有给出链接, 不明白的
话可以仔细看看.


<?xml version="1.0" encoding="iso-8859-1"?>
<!--
    Copyright 2004 Johan K�ng�d, http://dev.kanngard.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->
<!DOCTYPE module PUBLIC
    "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
    "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
<!--
  Based on the Checkstyle configuration file sun_checks.xml with some minor
  modifications..
  Checkstyle is very configurable. Be sure to read the documentation at
  http://checkstyle.sf.net
-->
<module name="Checker">

    <!-- Checks that a package.html file exists for each package.     -->
    <!-- See http://checkstyle.sf.net/config_javadoc.html#PackageHtml -->
    <module name="PackageHtml"/>

    <!-- Checks whether files end with a new line.                        -->
    <!-- See http://checkstyle.sf.net/config_misc.html#NewlineAtEndOfFile -->
    <module name="NewlineAtEndOfFile"/>

    <!-- Checks that property files contain the same keys.         -->
    <!-- See http://checkstyle.sf.net/config_misc.html#Translation -->
    <module name="Translation"/>

    <module name="TreeWalker">
        <!-- Checks for Javadoc comments.                     -->
        <!-- See http://checkstyle.sf.net/config_javadoc.html -->
        <module name="JavadocMethod"/>
        <module name="JavadocType"/>
        <module name="JavadocVariable"/>
        <module name="JavadocStyle"/>

        <!-- Checks for Naming Conventions.                  -->
        <!-- See http://checkstyle.sf.net/config_naming.html -->
        <module name="ConstantName"/>
        <module name="LocalFinalVariableName"/>
        <module name="LocalVariableName"/>
        <module name="MemberName">
            <property name="format" value="^[_A-Z][a-zA-Z0-9]*$"/>
        </module>
        <module name="MethodName"/>
        <module name="PackageName"/>
        <module name="ParameterName"/>
        <module name="StaticVariableName"/>
        <module name="TypeName"/>

        <!-- Checks for Headers                                -->
        <!-- See http://checkstyle.sf.net/config_header.html   -->
        <module name="Header">
            <!-- The follow property value demonstrates the ability     -->
            <!-- to have access to ANT properties. In this case it uses -->
            <!-- the ${basedir} property to allow Checkstyle to be run  -->
            <!-- from any directory within a project. See property      -->
            <!-- expansion,                                             -->
            <!-- http://checkstyle.sf.net/config.html#properties        -->
            <property
                name="headerFile"
                value="${basedir}/build/java.header"/>
            <property name="ignoreLines" value="3,5,8"/>
        </module>

        <!-- Following interprets the header file as regular expressions. -->
        <!-- <module name="RegexpHeader"/>                                -->

        <!-- Checks for imports                              -->
        <!-- See http://checkstyle.sf.net/config_import.html -->
        <module name="AvoidStarImport"/>
        <module name="IllegalImport"/> <!-- defaults to sun.* packages -->
        <module name="RedundantImport"/>
        <module name="UnusedImports"/>

        <!-- Checks for Size Violations.                    -->
        <!-- See http://checkstyle.sf.net/config_sizes.html -->
        <module name="FileLength"/>
        <module name="LineLength"/>
        <module name="MethodLength"/>
        <module name="ParameterNumber"/>

        <!-- Checks for whitespace                               -->
        <!-- See http://checkstyle.sf.net/config_whitespace.html -->
        <module name="EmptyForIteratorPad"/>
        <module name="MethodParamPad"/>
        <module name="NoWhitespaceAfter"/>
        <module name="NoWhitespaceBefore"/>
        <module name="OperatorWrap"/>
        <module name="ParenPad"/>
        <module name="TypecastParenPad"/>
<!--        <module name="TabCharacter"/>-->
        <module name="WhitespaceAfter"/>
        <module name="WhitespaceAround"/>

        <!-- Modifier Checks                                    -->
        <!-- See http://checkstyle.sf.net/config_modifiers.html -->
        <module name="ModifierOrder"/>
        <module name="RedundantModifier"/>

        <!-- Checks for blocks. You know, those {}'s         -->
        <!-- See http://checkstyle.sf.net/config_blocks.html -->
        <module name="AvoidNestedBlocks"/>
        <module name="EmptyBlock"/>
        <module name="LeftCurly"/>
        <module name="NeedBraces"/>
        <module name="RightCurly"/>

        <!-- Checks for common coding problems               -->
        <!-- See http://checkstyle.sf.net/config_coding.html -->
        <module name="AvoidInlineConditionals"/>
        <module name="DoubleCheckedLocking"/>    <!-- MY FAVOURITE -->
        <module name="EmptyStatement"/>
        <module name="EqualsHashCode"/>
        <module name="HiddenField">
            <property name="tokens" value="VARIABLE_DEF"/>
        </module>

        <module name="IllegalInstantiation"/>
        <module name="InnerAssignment"/>
        <module name="MagicNumber"/>
        <module name="MissingSwitchDefault"/>
        <module name="RedundantThrows"/>
        <module name="SimplifyBooleanExpression"/>
        <module name="SimplifyBooleanReturn"/>

        <!-- Checks for class design                         -->
        <!-- See http://checkstyle.sf.net/config_design.html -->
        <!-- <module name="DesignForExtension"/>         -->
        <module name="FinalClass"/>
        <module name="HideUtilityClassConstructor"/>
        <module name="InterfaceIsType"/>
        <module name="VisibilityModifier"/>

        <!-- Miscellaneous other checks.                   -->
        <!-- See http://checkstyle.sf.net/config_misc.html -->
        <module name="ArrayTypeStyle"/>
        <module name="FinalParameters"/>
        <module name="GenericIllegalRegexp">
            <property name="format" value="/s+$"/>
            <property name="message" value="Line has trailing spaces."/>
        </module>
        <module name="TodoComment"/>
        <module name="UpperEll"/>
    </module>
</module>

 


. 以下是我的 java 文件头部构造, 当然每个团队可以有自己的约定.
因为第3,5,8行是随文件的改变而改变的, 因此我忽略了对他们的检查,
这一点可以从上面的 checkstyle_checks.xml 文件中看出来.

/*
 * -----------------------------------------------------------
 * file name  : _filename_
 * authors    : camry(rat@gmail.com)
 * created    : _datetime_
 * copyright  : (c) 2003 Vitular Inc. All Rights Reserved.
 *
 * modifications:
 *
 * -----------------------------------------------------------
 */

 


有的朋友可能觉得每次都在文件前写这么一个头岂不是很麻烦, 但是对于我来说,
这一点非常容易就可以让 vim 做到了. 当我用 vim 新建一个 java 文件时,
它会自动加入这个头部说明, 并用适当的文件名和时间替换 _filename_ 和
_datetime_ 这两个参数. 如何做到这一点将来会做说明.

使用这个 checkstyle 时会有这么一个麻烦的地方: 它不允许在行尾有多余的
空格. 对于程序员来说, 谁在乎这么几个空格呢, 可是程序是非常严谨的:)
使用vim的朋友可以在这里发现如何轻松消除行尾空格.

摘要: build_tomcat.xml, tomcat.xml

现在介绍一下如何与 tomcat 一起配合工作.
java 的程序很大部分都涉及 web, 自己测试时使用 tomcat 还是很方便的,
所以有必要介绍一下.

在 work/common/ 下的 build_tomcat.xml, tomcat.xml 这两个文件是与
tomcat 配合工作的配置文件, 它的内容很简单.

. 以下为 build_tomcat.xml 文件内容:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc..
-->
<project name="tomcat target file" default="start" basedir="">

  <description>
      与tomcat相互配合的任务. Version 1.0.
  </description>

  <!-- 读入 tomcat 配置 -->
  <xmlproperty  file="${path.common}/tomcat.xml"
                semanticAttributes="true" keepRoot="false"/>

  <!-- 发布到 tomcat -->
  <target name="deploy_tomcat"
          depends="compile" description="Deploy application">
    <copy todir="${path.war}/WEB-INF/classes" preservelastmodified="true">
      <fileset dir="${dist}/classes">
        <include name="**/*"/>
      </fileset>
    </copy>
    <copy todir="${path.war}/WEB-INF/lib" preservelastmodified="true">
      <fileset dir="${dist}/lib">
        <include name="**/*"/>
      </fileset>
    </copy>
    <copy todir="${path.deploy}/${component.name}" preservelastmodified="true">
      <fileset dir="${path.war}">
        <include name="**/*"/>
      </fileset>
    </copy>
  </target>

  <!-- 撤销发布 -->
  <target name="undeploy_tomcat" description="Un-Deploy application">
    <delete>
      <fileset dir="${tomcat.home}/webapps/${component.name}">
        <include name="**/*"/>
      </fileset>
    </delete>
  </target>

  <!-- 发布为 war 形式 -->
  <target name="deploywar"
          depends="jar" description="Deploy application as a WAR file">
    <war destfile="${component.name}.war" webxml="${path.war}/WEB-INF/web.xml">
      <fileset dir="${path.war}">
        <include name="**/*"/>
      </fileset>
    </war>
    <copy todir="${path.deploy}" preservelastmodified="true">
      <fileset dir=".">
        <include name="*.war"/>
      </fileset>
    </copy>
  </target>

  <!-- install 工程 -->
  <taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- reload 工程 -->
  <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- 查看工程列表 -->
  <taskdef name="list" classname="org.apache.catalina.ant.ListTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- 启动 tomcat -->
  <taskdef name="start" classname="org.apache.catalina.ant.StartTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- 停止 tomcat -->
  <taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
    <classpath>
      <path location="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>

  <!-- install 工程 -->
  <target name="install" description="Install application in Tomcat">
    <install  url="${tomcat.manager.url}"
              username="${tomcat.manager.username}"
              password="${tomcat.manager.password}"
              path="/${component.name}"
              war="${component.name}.war"/>
  </target>

  <!-- reload 工程 -->
  <target name="reload" description="Reload application in Tomcat">
    <reload url="${tomcat.manager.url}"
            username="${tomcat.manager.username}"
            password="${tomcat.manager.password}"
            path="/${component.name}"/>
  </target>

  <!-- 启动 tomcat -->
  <target name="start" description="Start Tomcat application">
    <start  url="${tomcat.manager.url}"
            username="${tomcat.manager.username}"
            password="${tomcat.manager.password}"
            path="/${component.name}"/>
  </target>

  <!-- 停止 tomcat -->
  <target name="stop" description="Stop Tomcat application">
    <stop url="${tomcat.manager.url}"
          username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"
          path="/${component.name}"/>
  </target>

  <!-- 查看工程列表 -->
  <target name="list" description="List Tomcat applications">
    <list url="${tomcat.manager.url}"
          username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"/>
  </target>
</project>

 


以下为 tomcat.xml 的内容.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Ant build environment for tomcat
-->

<root>
  <tomcat>
    <!-- 配置 tomcat.home 位置 -->
    <home value="/usr/tomcat.home"/>
    <!-- 配置 tomcat 的管理界面 -->
    <manager>
      <url>http://127.0.0.1:8080/manager</url>
      <username>camry</username>
      <password>xxxxx</password>
    </manager>
  </tomcat>

  <path>
    <!-- 配置发布的源 war 位置 -->
    <war>war</war>
    <!-- 发布到 tomcat 的 webapps 目录下 -->
    <deploy>${tomcat.home}/webapps</deploy>
  </path>
</root>

 

 

在使用的时候, 要注意先在 work/工程目录/ 下建立 war 目录,
war 目录结构为:
  work/工程目录/war
      WEB-INF/
        classes/                  # 放置 class 文件
        lib/                      # 放置需要的 jar 包
        web.xml                   # web 配置文件

有了这些配置, 在与 tomcat 配合时只要启动 tomcat 先, 随后就不用
再管 tomcat 了, 只要调用 ant deploy, ant stop, ant start 等等
任务就可以了.

摘要: 与 cvs 一起

很多人用 cvs 作为版本控制工具, 但是它有一点点麻烦, 就是 commit 的时候
没有 -r 选项, 不能把子目录数据也传上去. 不要紧, 可以用 ant 把这个缺口
补上, 只不过需要先做一些准备工作.
在 work/common/template/src/ 目录下有一个 build.xml 文件, 这个文件
是进行 cvs 任务的.
新建立一个项目工程后, 在 src 的每一个子目录下都放置这样一个 build.xml
文件来操作当前目录及其子目录的版本控制命令, 这样就可以实现递归操作了.
麻烦的是需要每个目录都创建这样的文件, 并且 ant 调用 cvs 时不支持通配符,
必须写清楚每个文件的名称. 不过这样也更能保证安全性就是了.
假如哪位朋友有更好的方法, 希望能够告诉我一下.

. 以下是这个 build.xml 文件的内容


<?xml version="1.0"?>

<!-- ========================================================= -->
<!-- $Id: build.xml,v 1.2 2004/03/19 08:34:15 cvs Exp $      -->
<!-- ========================================================= -->

<project name="src-cvs" default="cvs" basedir=".">
  <target name="init" if="${CVSROOT}">
    <property file="build.properties"/>
  </target>

  <!-- 更新源文件 -->
  <target name="update" depends="init">
    <!-- 调用 cvs 命令进行更新 -->
    <cvs dest="." command="update"/>
  </target>

  <!-- 提交 -->
  <target name="commit" depends="init">
    <tstamp/>
    <!-- 调用 cvs 命令进行提交 -->
    <cvs dest="." command="commit -m ${TODAY} *.java"/>
    <!-- 显示提交的子目录 -->
    <echo message="commit src -- directory"/>
    <!-- 调用子目录中的提交命令 -->
    <ant dir="sub-dir" target="commit"/>
  </target>

  <!-- cvs -->
  <target name="cvs" depends="commit">
    <ant dir="sub-dir" target="cvs"/>
  </target>

  <!-- fetch -->
  <target name="fetch" depends="update">
    <ant dir="sub-dir" target="fetch"/>
  </target>
</project>


摘要: 如何使用这个 ant 环境

现在已经有了整个 ant 环境, 放在 work/common/ 目录下, 配置文件也都
齐全了, 可以开始进行工程了.

进行工程的步骤:
1. 给工程起个响亮的名字, 就叫 Proj 吧:)
2. cd 到 work/ 下
3. mkdir Proj
4. 将 work/common/build.xml 和 build.properties 文件拷贝到 Proj 下
5. 修改 build.properties 把和项目相关的东西改一下, 比如名字什么的.
6. cd 到 Proj 下
7. ant usage (可以看到使用方法打印出来)
8. ant prepare ( 把目录结构搭建起来 )
9. 好了, 开始敲代码吧. 到 src/main/ 下建立目录 com/vitular/proj/
10. 在 src/main/com/vitular/proj/ 下建立 Hello.java 文件.
11. ant checkstyle (检查一下是否合格)
    发现目录中少了 package.html
    并且不让我创建 public 的构造函数. (只要含有 main() 函数它就这么说)
    只好修改, 改完再检查就 ok 了.
11. ant compile (编译一下)
12. ant jar (打包)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值