登录社区:用户名: 密码: 忘记密码 网页功能:加入收藏 设为首页 网站搜索  

文档

下载

图书

论坛

安全

源码

硬件

游戏
首页 信息 空间 VB VC Delphi Java Flash 补丁 控件 安全 黑客 电子书 笔记本 手机 MP3 杀毒 QQ群 产品库 分类信息 编程网站
  立华软件园 - 安全技术中心 - 技术文档 - JAVA 技术文章 | 相关下载 | 电子图书 | 攻防录像 | 安全网站 | 在线论坛 | QQ群组 | 搜索   
 安全技术技术文档
  · 安全配制
  · 工具介绍
  · 黑客教学
  · 防火墙
  · 漏洞分析
  · 破解专题
  · 黑客编程
  · 入侵检测
 安全技术工具下载
  · 扫描工具
  · 攻击程序
  · 后门木马
  · 拒绝服务
  · 口令破解
  · 代理程序
  · 防火墙
  · 加密解密
  · 入侵检测
  · 攻防演示
 安全技术论坛
  · 安全配制
  · 工具介绍
  · 防火墙
  · 黑客入侵
  · 漏洞检测
  · 破解方法
 其他安全技术资源
  · 攻防演示动画
  · 电子图书
  · QQ群组讨论区
  · 其他网站资源
最新招聘信息

使用 ant 让你愉快编程
发表日期:2004-10-25作者:[转贴] 出处:  

已经有无数的文章介绍过 ant 了, 我就不再介绍 ant 的安装,配置了.
每个使用 ant 的朋友都有一套自己的组织方式, 现在我把我的方式写出来
供大家参考, 也免得自己忘记.

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/               # 这个目录中放置测试代码

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

. 以下为 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 开头的测试类 -->
  <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"?>
<!--
    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>


. 以下为 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 文件内容.

<?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="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=camry                  # 用户名
# ftp.pass=*****                  # 用户密码
# remote.dir=/opt/release         # 远程发布目录位置

现在已经有了目录结构, 有了 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">

    <!

我来说两句】 【发送给朋友】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 使用 ant 让你愉快编程

 ■ [欢迎对本文发表评论]
用  户:  匿名发出:
您要为您所发的言论的后果负责,故请各位遵纪守法并注意语言文明。

最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放 / 友情链接  
Copyright ©2001-2006 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.00203