Sunday, March 28, 2004
使用Ant部署網站應用程式
本文教你如何用Ant把整個網站應用程式(webapp)包成一個war檔
並且可以在不需要重新啟動Tomcat的情況下把網站應用程式部署上去
程式的預設target是deploy-application:
build.bat
並且可以在不需要重新啟動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
並在裡面造幾個子目錄:
- src:放置Java原始碼(像是servlet、JavaBeans),要按照package的結構置放
- web:放置JSP、HTML、圖片、目錄等等靜態資料
- meta:放置web.xml檔
- build:這裡會讓Ant放置編譯完的class檔案
- lib:放置需要的類別庫
- dist:這裡會讓Ant放置包好的war檔
- 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裡面
如果你灌的目錄跟我不一樣
那就需要自行更改到正確的目錄
- tomcat.webapps:Tomcat的webapps目錄,也就是放置網站應用程式的地方
- tomcat.dir:Tomcat的安裝目錄
- url:電腦上Tomcat manager的URL
- username:Tomcat的使用者帳號
- password:Tomcat的使用者密碼
程式的預設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:清空build、dist目錄
<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>
<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
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