<?xml version="1.0"?>

<project name="ssh" basedir="." default="usage">
      <property name="name" value="ssh"/>

<property name="war.dir" value="war"/>
      <property name="src.dir" value="src"/>
      <property name="client.dir" value="client"/>
      <property name="build.dir" value=".classes"/>
      <property name="webcontent.dir" value="WebContent"/>
      <property name="prjlib.dir" value="lib"/>    
      <property name="webcontentlib.dir" value="${webcontent.dir}/WEB-INF/lib"/>
      <property name="weblib.dir" value="${war.dir}/WEB-INF/lib"/>
      <property name="dist.dir" value="dist"/>

<property environment="env"/>
      <property name="tomcat.home" value="${env.CATALINA_HOME}"/>
      <property name="webapp.dist" value="${dist.dir}/webapps"/>   
      <path id="master-classpath">
          <fileset dir="${webcontentlib.dir}">
              <include name="hibernate3.jar"/>
              <include name="spring.jar"/>            
              <include name="struts.jar"/>            
              <include name="struts-el.jar"/>            
              <include name="struts-menu-2.4.2.jar"/>            
              <include name="acegi-security-1.0.2.jar"/>            
              <include name="activation.jar"/>            
              <include name="antlr.jar"/>            
              <include name="antlr-2.7.6.jar"/>            
              <include name="asm.jar"/>            
              <include name="aspectjweaver-1.5.2.jar"/>            
              <include name="cglib-2.1.3.jar"/>            
              <include name="commons-beanutils.jar"/>            
              <include name="commons-codec-1.3.jar"/>            
              <include name="commons-collections.jar"/>            
              <include name="commons-dbcp.jar"/>            
              <include name="commons-digester.jar"/>            
              <include name="commons-fileupload.jar"/>            
              <include name="commons-io.jar"/>    
              <include name="commons-lang.jar"/>            
              <include name="commons-logging-1.1.jar"/>            
              <include name="commons-pool.jar"/>            
              <include name="commons-validator.jar"/>            
              <include name="displaytag-1.1.jar"/>            
              <include name="dom4j-1.6.1.jar"/>            
              <include name="dwr.jar"/>            
              <include name="ehcache-1.2.3.jar"/>            
              <include name="itext-1.4.jar"/>            
              <include name="jakarta-oro.jar"/>            
              <include name="jstl.jar"/>            
              <include name="jta.jar"/>            
              <include name="log4j-1.2.11.jar"/>            
              <include name="mail.jar"/>            
              <include name="oscache-2.3.2.jar"/>            
              <include name="mysql-connector-java-5.0.3-bin.jar"/>            
              <include name="sitemesh-2.2.1.jar"/>            
              <include name="standard.jar"/>            
              <include name="urlrewrite-3.0-beta.jar"/>            
              <include name="velocity-1.4.jar"/>            
              <include name="velocity-tools-view-1.1.jar"/>            
          </fileset>
          <fileset dir="${prjlib.dir}/servletapi-2.3">
              <include name="servletapi-2.3.jar"/>
          </fileset>        
      </path>   
      <target name="usage">
          <echo message=""/>
          <echo message="Spring JPetStore build file"/>
          <echo message="------------------------------------------------------"/>
          <echo message=""/>
          <echo message="Available targets are:"/>
          <echo message=""/>
          <echo message="clean      --> Clean output dirs"/>
          <echo message="build      --> Compile main Java sources and copy libraries"/>
          <echo message="warfile --> Build the web application archive"/>
          <echo message="all        --> Clean, build, warfile"/>
          <echo message=""/>
      </target>   
      <target name="clean" description="Clean output dirs (build, weblib, dist)">
          <delete dir="${build.dir}"/>
          <delete dir="${weblib.dir}"/>
          <delete dir="${war.dir}"/>
          <delete dir="${dist.dir}"/>
          <delete file="client/${name}.jar"/>
      </target>
      <target name="build" description="Compile main source tree java files into class files, generate jar files">
          <mkdir dir="${build.dir}"/>
          <mkdir dir="${war.dir}"/>

<javac destdir="${build.dir}" source="1.3" target="1.3" debug="true"
                  deprecation="false" optimize="false" failonerror="true">
              <src path="${src.dir}"/>
              <classpath refid="master-classpath"/>
          </javac>

<mkdir dir="${weblib.dir}"/>
          <mkdir dir="${war.dir}/WEB-INF/classes"/>

<jar jarfile="${weblib.dir}/${name}.jar" compress="true" basedir="${build.dir}"/>

<copy todir="${war.dir}" preservelastmodified="true">
              <fileset dir="${webcontent.dir}">
                  <include name="**/**"/>
                  <include name="**.**"/>
              </fileset>
          </copy>

<copy todir="${war.dir}/WEB-INF/classes" preservelastmodified="true">
              <fileset dir="${src.dir}">
                  <include name="*.xml"/>
                  <include name="**/*.properties"/>
                  <include name="**/*.vm"/>
                  <exclude name="**/.*"/>
              </fileset>
          </copy>        
       
          <copy file="${weblib.dir}/${name}.jar" tofile="${client.dir}/${name}.jar"/>
      </target>

<target name="dist" depends="warfile">
          <!--
              Delegate to warfile target by depending on it. dist is just to offer
              a generic target name across all Spring sample apps that may be used
              for autobuilds testing.
          -->
      </target>

<target name="warfile" depends="build" description="Build the web application archive">
          <mkdir dir="${dist.dir}"/>
          <war warfile="${dist.dir}/${name}.war" basedir="${war.dir}" webxml="${webcontent.dir}/WEB-INF/web.xml">
              <include name="*"/>
              <include name="images/**"/>
              <include name="common/**"/>
              <include name="decorators/**"/>
              <include name="scripts/**"/>
              <include name="styles/**"/>
              <include name="WEB-INF/*.*"/>
              <include name="WEB-INF/lib/**"/>
              <include name="WEB-INF/pages/**"/>
              <include name="WEB-INF/classes/**"/>
              <exclude name="WEB-INF/web.xml"/>
          </war>
      </target>

<target name="all" depends="clean,build,warfile" description="Clean,build,warfile"/>

</project>

在Eclipse中使用Ant

Ant是Java平台下很棒的批处理命令运行程序,能很方便地自己主动完毕编译,測试,打包,部署等等一系列任务,大大提高开发效率。假设你如今还没有開始使用Ant,那就要赶快開始学习使用,使自己的开发水平上一个新台阶。

Eclipse中已经集成了Ant,我们能够直接在Eclipse中运行Ant。

曾经面建立的Helloproject为例,创建下面文件夹结构:

新建一个build.xml,放在project根文件夹下。build.xml定义了Ant要运行的批处理命令。尽管Ant也能够使用其他文件名称,可是遵循标准能更使开发更规范,同一时候易于与别人交流。

通常,src存放Java源文件,classes存放编译后的class文件,lib存放编译和执行用到的全部jar文件,web存放JSP等web文件,dist存放打包后的jar文件,doc存放API文档。

然后在根文件夹下创建build.xml文件,输入下面内容:

<?xml version="1.0"?>
<project name="Hello world" default="doc">

<!-- properies -->
     <property name="src.dir" value="src" />
     <property name="report.dir" value="report" />
     <property name="classes.dir" value="classes" />
     <property name="lib.dir" value="lib" />
     <property name="dist.dir" value="dist" />
<property name="doc.dir" value="doc"/>

<!-- 定义classpath -->
     <path id="master-classpath">
         <fileset file="${lib.dir}/*.jar" />
         <pathelement path="${classes.dir}"/>
     </path>

<!-- 初始化任务 -->
     <target name="init">
     </target>

<!-- 编译 -->
     <target name="compile" depends="init" description="compile the source files">
         <mkdir dir="${classes.dir}"/>
         <javac srcdir="${src.dir}" destdir="${classes.dir}" target="1.4">
             <classpath refid="master-classpath"/>
         </javac>
     </target>

<!-- 測试 -->
     <target name="test" depends="compile" description="run junit test">
         <mkdir dir="${report.dir}"/>
         <junit printsummary="on"
                 haltonfailure="false"
                 failureproperty="tests.failed"
                 showoutput="true">
             <classpath refid="master-classpath" />
             <formatter type="plain"/>
             <batchtest todir="${report.dir}">
                 <fileset dir="${classes.dir}">
                     <include name="**/*Test.*"/>
                 </fileset>
             </batchtest>
         </junit>
         <fail if="tests.failed">
         ***********************************************************
         ****   One or more tests failed!   Check the output ...   ****
         ***********************************************************
         </fail>
     </target>

<!-- 打包成jar -->
     <target name="pack" depends="test" description="make .jar file">
      <mkdir dir="${dist.dir}" />
         <jar destfile="${dist.dir}/hello.jar" basedir="${classes.dir}">
             <exclude name="**/*Test.*" />
             <exclude name="**/Test*.*" />
         </jar>
     </target>

<!-- 输出api文档 -->
     <target name="doc" depends="pack" description="create api doc">
      <mkdir dir="${doc.dir}" />
      <javadoc destdir="${doc.dir}"
             author="true"
             version="true"
             use="true"
             windowtitle="Test API">
             <packageset dir="${src.dir}" defaultexcludes="yes">
                 <include name="example/**" />
             </packageset>
             <doctitle><![CDATA[<h1>Hello, test</h1>]]></doctitle>
             <bottom><![CDATA[<i>All Rights Reserved.</i>]]></bottom>
             <tag name="todo" scope="all" description="To do:" />
         </javadoc>
     </target>
</project>

以上xml依次定义了init(初始化),compile(编译),test(測试),doc(生成文档),pack(打包)任务,能够作为模板。

选中Helloproject,然后选择“Project”,“Properties”,“Builders”,“New…”,选择“Ant Build”:

填入Name:Ant_Builder;Buildfile:build.xml;Base Directory:${workspace_loc:/Hello}(按“Browse Workspace”选择project根文件夹),因为用到了junit.jar包,搜索Eclipse文件夹,找到junit.jar,把它拷贝到Hello/lib文件夹下,并加入到Ant的Classpath中:

然后在Builder面板中钩上Ant_Build,去掉Java Builder:

再次编译,就可以在控制台看到Ant的输出:

Buildfile: F:/eclipse-projects/Hello/build.xml

init:

compile:
        [mkdir] Created dir: F:/eclipse-projects/Hello/classes
        [javac] Compiling 2 source files to F:/eclipse-projects/Hello/classes

test:
        [mkdir] Created dir: F:/eclipse-projects/Hello/report
        [junit] Running example.HelloTest
        [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.02 sec

pack:
        [mkdir] Created dir: F:/eclipse-projects/Hello/dist
          [jar] Building jar: F:/eclipse-projects/Hello/dist/hello.jar

doc:
        [mkdir] Created dir: F:/eclipse-projects/Hello/doc
      [javadoc] Generating Javadoc
      [javadoc] Javadoc execution
      [javadoc] Loading source files for package example...
      [javadoc] Constructing Javadoc information...
      [javadoc] Standard Doclet version 1.4.2_04
      [javadoc] Building tree for all the packages and classes...
      [javadoc] Building index for all the packages and classes...
      [javadoc] Building index for all classes...
      [javadoc] Generating F:/eclipse-projects/Hello/doc/stylesheet.css...
      [javadoc] Note: Custom tags that could override future standard tags:   @todo. To avoid potential overrides, use at least one period character (.) in custom tag names.
      [javadoc] Note: Custom tags that were not seen:   @todo
BUILD SUCCESSFUL
Total time: 11 seconds

Ant依次运行初始化,编译,測试,打包,生成API文档一系列任务,极大地提高了开发效率。将来开发J2EE项目时,还可增加部署等任务。而且,即使脱离了Eclipse环境,仅仅要正确安装了Ant,配置好环境变量ANT_HOME=<Ant解压文件夹>,Path=…;%ANT_HOME%/bin,在命令行提示符下切换到Hello文件夹,简单地键入ant就可以。

一个build.xml实例的更多相关文章

  1. Java eclipse下 Ant build.xml实例详解

    在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...

  2. 转:Java eclipse下 Ant build.xml实例详解

    在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...

  3. Java eclipse下 Ant build.xml实例详解 附完整项目源码

    在有eclipse集成环境下ant其实不是很重要,但有些项目需要用到,另外通过eclipse来学习和理解ant是个很好的途径,所以写他demo总结下要点,希望能够帮到大家. 一.本人测试环境eclip ...

  4. Java MyEclipse下Ant build.xml简单实例详解

    一.下载配置ant 1.首先下载ant: http://www.apache.org/ 下载最新的版本2.解压ant 后设置ANT_HOME, PATH中添加ANT_HOME目录下的bin目录(如:A ...

  5. Ant_的最完整build.xml解释

    Ant的概念 Make命令是一个项目管理工具,而Ant所实现功能与此类似.像make,gnumake和nmake这些编译工具都有一定的缺陷,但是Ant却克服了这些工具的缺陷.最初Ant开发者在开发跨平 ...

  6. Java Ant build.xml详解

    1,什么是antant是构建工具2,什么是构建概念到处可查到,形象来说,你要把代码从某个地方拿来,编译,再拷贝到某个地方去等等操作,当然不仅与此,但是主要用来干这个3,ant的好处跨平台   --因为 ...

  7. (转)Java Ant build.xml详解

    1,什么是ant ant是构建工具2,什么是构建概念到处可查到,形象来说,你要把代码从某个地方拿来,编译,再拷贝到某个地方去等等操作,当然不仅与此,但是主要用来干这个3,ant的好处跨平台   --因 ...

  8. Ant之build.xml详解---可用

    Ant的概念 :在Eclipse中使用Ant Ant是Java平台下非常棒的批处理命令执行程序,能非常方便地自动完成编译,测试,打包,部署等等一系列任务,大大提高开发效率. Ant和make命令很像. ...

  9. ANT编译build.xml

    一,体验ant就像每个语言都有HelloWorld一样,一个最简单的应用能让人感受一下Ant1,首先你要知道你要干什么,我现在想做的事情是:编写一些程序编译它们把它打包成jar包把他们放在应该放置的地 ...

随机推荐

  1. 【转】小白级的CocoaPods安装和使用教程

    原文网址:http://www.jianshu.com/p/e2f65848dddc 百度有很多CocoaPods的安装教程.第一次看的时候,确实有点摸不透的感觉.经过思考,一步一步来实践,前后花了三 ...

  2. 2017-3-7 leetcode 66 119 121

    今天纠结了一整天============================================================== leetcode66 https://leetcode.c ...

  3. vue.js和node.js的认识

    首先vue.js 是库,不是框架,不是框架,不是框架. Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据. Vue.js 的核心是一个允许你 ...

  4. Binary Indexed Tree 总结

    特点 1. 针对 数组连续子序列累加和 问题(需要进行频繁的 update.sum 操作): 2. 并非是树型结构,只是逻辑上层次分明: 3. 可以通过 填坑法 来理解: 4. 中心思想:每一个整数都 ...

  5. POJ 2446 匈牙利算法

    题意: 思路: 二分图匹配... // by SiriusRen #include <cmath> #include <cstdio> #include <cstring ...

  6. HD-ACM算法专攻系列(22)——Max Sum

    问题描述: AC源码: 此题考察动态规划,解题思路:遍历(但有技巧),在于当前i各之和为负数时,直接选择以第i+1个为开头,在于当前i各之和为正数时,第i个可以不用作为开头(因为前i+1个之和一定大于 ...

  7. Unity 控制public/private 是否暴露给Inspector面板

    默认情况下Public是暴露给Unity,protect/private是不暴露给Unity的,但有时候想让外部引用,又不想暴露给Unity,怎么办? 对Unity隐藏,使用[HideInInspec ...

  8. Unity 导入的模型检测不到碰撞

    解决方案 添加Mesh Collider和rigidbody,并且Collider里边2个勾要勾上. 这是我碰到的问题.如果没解决你的问题,别打我,逃~

  9. Kattis - String Matching(kmp)

    String Matching Input The input consists of several test cases. Each test case consists of two lines ...

  10. leetcode525. 连续数组 python

    给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组. 示例 1: 输入: [0,1] 输出: 2 说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2: 输入: ...