鍍金池/ 教程/ 物聯(lián)網(wǎng)/ 封裝應(yīng)用
生成 WAR 文件
JUnit 集成
環(huán)境搭建
Eclipse 集成
部署應(yīng)用
屬性任務(wù)
擴(kuò)展 Ant
?# 執(zhí)行 Java 代碼
構(gòu)建項(xiàng)目
構(gòu)建文件
數(shù)據(jù)類型
生成 JAR 文件
生成文檔
屬性文件
封裝應(yīng)用
介紹

封裝應(yīng)用

我們通過 Hello World Fax Web 應(yīng)用,已經(jīng)瑣碎地學(xué)習(xí)了 Ant 的不同方面的知識(shí)了。

現(xiàn)在是時(shí)候把我們所學(xué)的知識(shí)都運(yùn)用起來創(chuàng)建一個(gè)全面和完整的 build.xml 文件了。考慮下面給出的 build.propertiesbuild.xml 文件:

build.properties

eploy.path = c:\tomcat6\webapps

build.xml

<?xml version = "1.0"?>

<project name = "fax" basedir = "." default = "usage">

   <property file = "build.properties"/>
   <property name = "src.dir" value = "src"/>
   <property name = "web.dir" value = "war"/>
   <property name = "javadoc.dir" value = "doc"/>
   <property name = "build.dir" value = "${web.dir}/WEB-INF/classes"/>
   <property name = "name" value = "fax"/>

   <path id = "master-classpath">
      <fileset dir = "${web.dir}/WEB-INF/lib">
         <include name = "*.jar"/>
      </fileset>
      <pathelement path = "${build.dir}"/>
   </path>

   <target name = "javadoc">
      <javadoc packagenames = "faxapp.*" sourcepath = "${src.dir}" 
         destdir = "doc" version = "true" windowtitle = "Fax Application">

         <doctitle><![CDATA[<h1> =  Fax Application  = </h1>]]>
         </doctitle>

         <bottom><![CDATA[Copyright ? 2011. All Rights Reserved.]]>
         </bottom>

         <group title = "util packages" packages = "faxapp.util.*"/>
         <group title = "web packages" packages = "faxapp.web.*"/> 
         <group title = "data packages" packages = "faxapp.entity.*:faxapp.dao.*"/>
      </javadoc>
   </target>

   <target name = "usage">
      <echo message = ""/>
      <echo message = "${name} build file"/>
      <echo message = "-----------------------------------"/>
      <echo message = ""/>
      <echo message = "Available targets are:"/>
      <echo message = ""/>
      <echo message = "deploy    --> Deploy application as directory"/>
      <echo message = "deploywar --> Deploy application as a WAR file"/>
      <echo message = ""/>
   </target>

   <target name = "build" description = "Compile main source tree java files">
      <mkdir dir = "${build.dir}"/>

      <javac destdir = "${build.dir}" source = "1.5" target = "1.5" debug = "true"
         deprecation = "false" optimize = "false" failonerror = "true">

         <src path = "${src.dir}"/>
         <classpath refid = "master-classpath"/>

      </javac>
   </target>

   <target name = "deploy" depends = "build" description = "Deploy application">
      <copy todir = "${deploy.path}/${name}" preservelastmodified = "true">

         <fileset dir = "${web.dir}">
            <include name = "**/*.*"/>
         </fileset>

      </copy>
   </target>

   <target name = "deploywar" depends = "build" description = "Deploy application as a WAR file">

      <war destfile = "${name}.war" webxml = "${web.dir}/WEB-INF/web.xml">
         <fileset dir = "${web.dir}">
            <include name = "**/*.*"/>
         </fileset>
      </war>

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

   </target>

   <target name = "clean" description = "Clean output directories">
      <delete>
         <fileset dir = "${build.dir}">
            <include name = "**/*.class"/>
         </fileset>
      </delete>
   </target>

</project>

在上面給出的例子中:

  • 我們首先在 build.properties 文件中聲明了存放 Tomcat 的 webapp 文件夾的路徑,并用變量 deploy.path 來保存。
  • 我們聲明一個(gè)源文件夾來存放 java 文件,并用變量 src.dir 來保存。
  • 接下來,我們聲明另一個(gè)源文夾來存放 web 文件,并用變量 web.dir 來保存。變量 javadoc.dir 用來存儲(chǔ) java 文檔,變量 build.dir 是用來存儲(chǔ)配置輸出文件的路徑。
  • 然后,我們給這個(gè) web 應(yīng)用命名,也就是 fax 傳真。
  • 我們還定義了包含 JAR 文件的基本類路徑,在上面給出的項(xiàng)目中也就是: WEB-INF/lib 文件夾。
  • 我們還將 build.dir 中的類文件存放在基本類路徑下。
  • 這個(gè) Javadoc 目標(biāo)產(chǎn)生項(xiàng)目所需的文檔,以及說明目標(biāo)使用的 javadoc 文檔。

上述的例子向我們展示了兩個(gè)部署目標(biāo): deploydeploywar

這個(gè) deploy 目標(biāo)將文件從 web 目錄復(fù)制到部署目錄,并保存最后修改日期時(shí)間戳。這樣很有用,特別是當(dāng)我們將項(xiàng)目部署到服務(wù)器上,并且該服務(wù)器支持熱部署。(釋義:所謂熱部署,就是在應(yīng)用正在運(yùn)行的時(shí)候升級(jí)軟件,卻不需要重新啟動(dòng)應(yīng)用。)

這個(gè) clean 目標(biāo)清楚所有之前的構(gòu)建文件。

這個(gè) deploywar 目標(biāo)構(gòu)建 war 文件,然后將 war 文件復(fù)制到應(yīng)用程序服務(wù)器的部署目錄。

上一篇:環(huán)境搭建下一篇:Eclipse 集成