用 ant 让你愉快编程

目录结构:

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

1.build_common.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
  <!-
      Copyright 2004 camry.wu@gmail.com
  
      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
      Copyright 2004 camry.wu@gmail.com
    </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 开头的测试类
      注意, 在 ant 的 lib 目录下要包含 junit.jar.
    ->
    <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>

2.   common.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
  <!-
      Copyright 2004 camry.wu@gmail.com
      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>

3.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                -> 默认的构建过程(自行修改)



http://blog.csdn.net/camry_camry/archive/2004/10/21/145752.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值