在之前的章節(jié)中,我們已經(jīng)學(xué)會了如何去打包一個應(yīng)用程序以及怎樣將其部署到一個文件夾中。
在這個章節(jié)中,我們將要直接將 web 應(yīng)用程序部署到一個應(yīng)用服務(wù)器的部署文件夾中。隨后,我們將添加一些 Ant 目標(biāo)來啟動和停止服務(wù)。讓我們繼續(xù) Hello World fax
web 應(yīng)用程序。 這一章節(jié)是對之前的章節(jié)的一個延續(xù),所有新的組件會用粗體突出顯示。
# Ant properties for building the springapp
appserver.home=c:\\install\\apache-tomcat-7.0.19
# for Tomcat 5 use $appserver.home}/server/lib
# for Tomcat 6 use $appserver.home}/lib
appserver.lib=${appserver.home}/lib
deploy.path=${appserver.home}/webapps
tomcat.manager.url=http://www.tutorialspoint.com:8080/manager
tomcat.manager.username=tutorialspoint
tomcat.manager.password=secret
<?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>
<!-- ============================================================ -->
<!-- Tomcat tasks -->
<!-- ============================================================ -->
<path id="catalina-ant-classpath">
<!-- We need the Catalina jars for Tomcat -->
<!-- * for other app servers - check the docs -->
<fileset dir="${appserver.lib}">
<include name="catalina-ant.jar"/>
</fileset>
</path>
<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<target name="reload" description="Reload application in Tomcat">
<reload url="${tomcat.manager.url}"username="${tomcat.manager.username}"
password="${tomcat.manager.password}" path="/${name}"/>
</target>
</project>
在這個例子中,我們已經(jīng)使用 Tomcat 作為我們應(yīng)用的服務(wù)器。 首先,在構(gòu)建屬性文件中,我們已經(jīng)定義了一些附加屬性。
在 Tomcat 中的應(yīng)用程序能通過使用 Tomcat 管理應(yīng)用程序進(jìn)行啟動和停止。管理應(yīng)用程序的統(tǒng)一資源定位器(URL),用戶名和密碼也在 build.properties 文件夾中進(jìn)行指定。 接下來,我們聲明一個新的 CLASSPATH 來包含 catalina-ant.jar。 若要通過 Apache Ant 來運行 Tomcat, 這個 jar 文件是必須的。
catalina-ant.jar 提供了下述的任務(wù):
屬性 | 描述 |
---|---|
InstallTask | 安裝一個 web 應(yīng)用程序。 類名字為: org.apache.catalina.ant.InstallTask |
ReloadTask | 重新安裝一個 web 應(yīng)用程序。類名字為: org.apache.catalina.ant.ReloadTask |
ListTask | 列出所有的 web 應(yīng)用程序。類名字為: Class Name: org.apache.catalina.ant.ListTask |
StartTask | 啟動一個 web 應(yīng)用程序。類名字為: org.apache.catalina.ant.StartTask |
StopTask | 停止一個 web 應(yīng)用程序。類名字為: org.apache.catalina.ant.StopTask |
ReloadTask | 重新加載一個無需停止的 web 應(yīng)用程序。類名字為:org.apache.catalina.ant.ReloadTask |
重載任務(wù)需要下列附加參數(shù):
讓我們發(fā)出部署 war (deploy-war)的命令來復(fù)制 web 應(yīng)用程序到 Tomcat 的 webapps 文件夾中。同時,我們重新加載傳真 web 應(yīng)用程序。下述的輸出是運行 Ant 文件的結(jié)果。
>C:\>ant deploy-war
>Buildfile: C:\build.xml
>BUILD SUCCESSFUL
>Total time: 6.3 seconds
>C:\>ant reload
>Buildfile: C:\build.xml
>BUILD SUCCESSFUL
>Total time: 3.1 seconds
一旦上述的任務(wù)被運行,web 應(yīng)用程序就是已經(jīng)被部署好且重新加載了的。
更多建議: