使用 ant 让你愉快编程(2)

2  通用配置

摘要: 介绍 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                -> 默认的构建过程(自行修改)
    

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值