Ant - 部署應用程式



在上一章中,我們學習瞭如何打包應用程式並將其部署到資料夾中。

在本章中,我們將把 Web 應用程式直接部署到應用程式伺服器的部署資料夾,然後新增一些 Ant 目標來啟動和停止服務。

讓我們繼續使用 **Hello World** 傳真 Web 應用程式。這是上一章的延續;新元件以 **粗體** 高亮顯示。

build.properties

build.properties 檔案如下所示:

# 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=https://tutorialspoint.tw:8080/manager
tomcat.manager.username=tutorialspoint
tomcat.manager.password=secret

build.xml

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>
   <!-- ============================================================ -->
   <!-- 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>

在本例中,我們使用 Tomcat 作為我們的應用程式伺服器。

首先,在 build 屬性檔案中,我們定義了一些其他屬性,如下所述:

  • **appserver.home** 指向 Tomcat 應用程式伺服器的安裝路徑。

  • **appserver.lib** 指向 Tomcat 安裝資料夾中的庫檔案。

  • **deploy.path** 變數現在指向 Tomcat 中的 webapp 資料夾。

Tomcat 中的應用程式可以使用 Tomcat 管理器應用程式停止和啟動。管理器應用程式的 URL、使用者名稱和密碼也指定在 build.properties 檔案中。

接下來,我們宣告一個包含 **catalina-ant.jar** 的新 CLASSPATH。此 jar 檔案需要透過 Apache Ant 執行 Tomcat 任務。

任務

catalina-ant.jar 提供以下任務:

序號 屬性和描述
1

InstallTask

安裝 Web 應用程式。類名:org.apache.catalina.ant.InstallTask

2

ReloadTask

重新載入 Web 應用程式。類名:org.apache.catalina.ant.ReloadTask

3

ListTask

列出所有 Web 應用程式。類名:org.apache.catalina.ant.ListTask

4

StartTask1

啟動 Web 應用程式。類名:org.apache.catalina.ant.StartTask

5

StopTask

停止 Web 應用程式。類名:org.apache.catalina.ant.StopTask

6

ReloadTask

在不停止的情況下重新載入 Web 應用程式。類名:org.apache.catalina.ant.ReloadTask

reload 任務需要以下附加引數:

  • 管理器應用程式的 URL。

  • 重新啟動 Web 應用程式的使用者名稱。

  • 重新啟動 Web 應用程式的密碼。

  • 要重新啟動的 Web 應用程式的名稱。

讓我們發出 **deploy-war** 命令將 webapp 複製到 Tomcat webapps 資料夾,然後讓我們重新載入傳真 Web 應用程式。以下結果是執行 Ant 檔案的結果:

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

執行上述任務後,Web 應用程式將被部署並重新載入。

廣告

© . All rights reserved.