一个build.xml实例

news/2024/7/7 19:11:00

<?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" failοnerrοr="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就可以。


http://lihuaxi.xjx100.cn/news/247474.html

相关文章

判断人工智能是否可信的“四把尺子”

1. 前世今生 今天介绍一下可信人工智能的前世今生。人工智能并不是一个新的概念&#xff0c;从1950年的图灵之问开始&#xff0c;到今天产业的蓬勃发展。起起伏伏&#xff0c;人工智能已经走过了三次浪潮&#xff0c;甚至有学者认为&#xff0c;我们今天正处在第四次浪潮&…

ARP协议学习

Dolphin&#xff0c;ChinaBJ&#xff0c;20131122本文档是检验自己学习TCP/IP协议卷1&#xff1a;协议中关于ARP协议的学习情况。本文档包含以下四部分&#xff1a;1、简述ARP协议&#xff1b;2、ARP的分组格式&#xff1b;3、ARP Spoofing 实验 4、应用及防御方法。1.简述ARP协…

No.6 PHP的基本配置与优化

PHP下载http://www.php.net/downloads.php安装yum -y install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libxslt-devel gcc gcc-copenssl-develtar xf php-7.2.3.tar.bz2 -C /tmp./configure --prefix/usr/local/php --with-curl --with-freety…

Doxygen生成代码关系调用图

1. 简介 Doxygen能将程序中的特定批注转换成为说明文件。它可以依据程序本身的结构&#xff0c;将程序中按规范注释的批注经过处理生成一个纯粹的参考手册&#xff0c;通过提取代码结构或借助自动生成的包含依赖图&#xff08;include dependency graphs&#xff09;、继承图&…

计算机视觉图像处理机器学习压缩感知等论文代码大全

点击链接进入相关博文 1.来自西弗吉利亚大学li xin整理的CV代码合集 主要包括&#xff1a; 1.图像去噪&#xff0c;编码&#xff0c;去马赛克&#xff0c;超分辨&#xff0c;分割&#xff0c;去模糊&#xff0c;纹理合成&#xff0c;修复&#xff0c;质量评估等 2.视频编码和目…

一分钟了解 TCP/IP 模型

原文讲的不是特别细&#xff0c;为了便于理解&#xff0c;我颠倒了顺序。写在开始 我们需要知道协议到底是什么。 在网络上&#xff0c;一个协议对应于管理系统之间如何相互通信的规则。然后我们需要知道什么是协议族。 一个协议族是一系列协同工作的协议的集合。什么是 TCP/IP…

ORACLE11g 前期安装环境配置

Linux系统可以拿来直接用的脚本哦#!/bin/bashservice iptables stop &> /dev/nulliptables -F service iptables save &> /dev/nullsed -i s/enforcing/disabled/ /etc/selinux/configsetenforce 0sed /tmpfs/d /etc/fstab &> /dev/nullecho tmpfs …

L1-016 查验身份证(2016年天梯赛模拟赛第8题)

7-8 查验身份证&#xff08;15 分&#xff09;一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下&#xff1a;首先对前17位数字加权求和&#xff0c;权重分配为&#xff1a;{7&#xff0c;9&#xff0c;10&#xff0c;5&#xff0c;8&…