Sunday, March 28, 2004

 

使用Ant部署網站應用程式

本文教你如何用Ant把整個網站應用程式(webapp)包成一個war檔
並且可以在不需要重新啟動Tomcat的情況下把網站應用程式部署上去

Author: swanky(swanky@giga.net.tw)
First Release: 2004-03-28
Last Updated: 2004-03-28
Version: 1.00

本文發表於 JavaWorld@taiwan,如欲轉載或轉錄內容,請註明來源及作者

強力建議使用 Mozilla 系列瀏覽
若有任何錯誤,請 mail 通知我

首先要建立一個放置網站應用程式的目錄myapp
並在裡面造幾個子目錄:

接著再寫兩個檔案:

build.properties
tomcat.webapps=C:/Tomcat/webapps
tomcat.dir=C:/Tomcat
url=http://localhost:8080/manager
username=admin
password=
build.xml
<project name="Deploy Project" default="deploy-application">
   <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />

   <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />
   <property file="build.properties" />

   <path id="classpath">

      <fileset dir="${tomcat.dir}/common/lib">
         <include name="*.jar" />
      </fileset>
      <fileset dir="${tomcat.dir}/common/endorsed">

         <include name="*.jar" />
      </fileset>
   </path>

   <target name="init"

      description="Initializes some properties.">
      <echo message="Initializing properties." />
      <property name="build" value="build" />

      <property name="src" value="src" />
      <property name="dist" value="dist" />

      <property name="lib" value="lib" />
      <property name="web" value="web" />

      <property name="meta" value="meta" />
      <property name="context-path" value="myapp" />

   </target>

   <target name="prepare" depends="init">
      <echo message="Cleaning up the build and dist directories" />

      <delete dir="${build}" />
      <mkdir dir="${build}" />
      <delete dir="${dist}" />

      <mkdir dir="${dist}" />
   </target>

   <target name="deploy"
      description="Deploys a Web application">

      <deploy url="${url}" username="${username}" password="${password}"
         path="/${context-path}" war="file:${dist}/${context-path}.war" />

   </target>

   <target name="undeploy"
      description="Undeploys a Web application" if="already.deployed">
      <undeploy url="${url}" username="${username}" password="${password}"

         path="/${context-path}" />
   </target>

   <target name="create-war" description="Creates a web application archive file">

      <war destfile="${dist}/${context-path}.war" webxml="${meta}/web.xml">
         <classes dir="${build}" />
         <lib dir="${lib}" />

         <fileset dir="${web}" />
      </war>
   </target>

   <target name="deploy-application" depends="prepare"

      description="Compile the web application...">
      <echo message="Undeploying the application only if it's deployed..." />
      <available file="${tomcat.webapps}/${context-path}.war" property="already.deployed" />

      <antcall target="undeploy" />
      <echo message="Compiling the application files..." />
      <javac srcdir="${src}" destdir="${build}">

         <include name="*.java" />
         <classpath refid="classpath" />
      </javac>
      <echo message="creation the WAR file..." />

      <antcall target="create-war" />
      <antcall target="deploy" />
   </target>
</project>

build.properties中可以看出來
我的Tomcat是灌在C:/Tomcat裡面
如果你灌的目錄跟我不一樣
那就需要自行更改到正確的目錄

以下解釋build.properties每個property的用途 以下解釋build.xml的執行順序
程式的預設target是deploy-application
   <target name="deploy-application" depends="prepare"
      description="Compile the web application...">

      <echo message="Undeploying the application only if it's deployed..." />
      <available file="${tomcat.webapps}/${context-path}.war" property="already.deployed" />
      <antcall target="undeploy" />

      <echo message="Compiling the application files..." />
      <javac srcdir="${src}" destdir="${build}">
         <include name="*.java" />

         <classpath refid="classpath" />
      </javac>
      <echo message="creation the WAR file..." />
      <antcall target="create-war" />

      <antcall target="deploy" />
   </target>
在執行deploy-application前要先執行:
init:設定每個目錄的property
   <target name="init"
      description="Initializes some properties.">
      <echo message="Initializing properties." />
      <property name="build" value="build" />

      <property name="src" value="src" />
      <property name="dist" value="dist" />

      <property name="lib" value="lib" />
      <property name="web" value="web" />

      <property name="meta" value="meta" />
      <property name="context-path" value="myapp" />

   </target>
prepare:清空builddist目錄
   <target name="prepare" depends="init">

      <echo message="Cleaning up the build and dist directories" />
      <delete dir="${build}" />
      <mkdir dir="${build}" />

      <delete dir="${dist}" />
      <mkdir dir="${dist}" />
   </target>
再來就是判斷該webapp是否已經部署過了:
      <available file="${tomcat.webapps}/${context-path}.war" property="already.deployed" />
      <antcall target="undeploy" />

如果部署過的話呼叫undeploy把之前的webapp移除
   <target name="undeploy"
      description="Undeploys a Web application" if="already.deployed">
      <undeploy url="${url}" username="${username}" password="${password}"

         path="/${context-path}" />
   </target>
webapp沒有部署過;或是已經移除後,會先編譯 Java原始碼
      <javac srcdir="${src}" destdir="${build}">

         <include name="*.java" />
         <classpath refid="classpath" />
      </javac>
再呼叫
create-war:製作成war檔
   <target name="create-war" description="Creates a web application archive file">
      <war destfile="${dist}/${context-path}.war" webxml="${meta}/web.xml">

         <classes dir="${build}" />
         <lib dir="${lib}" />
         <fileset dir="${web}" />

      </war>
   </target>
deploy:將webapp部署至Tomcat
   <target name="deploy"

      description="Deploys a Web application">
      <deploy url="${url}" username="${username}" password="${password}"
         path="/${context-path}" war="file:${dist}/${context-path}.war" />

   </target>
接下來我們做一個build.bat檔,讓我們點兩下就可以完成所有的事
build.bat
ant -lib C:\Tomcat\server\lib
其中的-lib C:\Tomcat\server\lib參數可以讓
   <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" />
   <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" />

這兩個指令能夠找得到正確的類別庫

這樣寫網站應用程式就方便許多啦!

由 swanky 發表於 March 28, 2004 04:11 PM

Comments: Post a Comment



<< Home