一個build.xml實例

<?xml version="1.0"?>css

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

      <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"/>mysql

      <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}"/>web

          <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>spring

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

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

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

          <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>app

      <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>dom

      <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就能夠。

相關文章
相關標籤/搜索