Ant - 部署应用程序

  • 简述

    在上一章中,我们学习了如何打包应用程序并将其部署到文件夹中。
    在本章中,我们将把 Web 应用程序直接部署到应用程序服务器部署文件夹,然后我们将添加一些 Ant 目标来启动和停止服务。
    让我们继续Hello World传真网络应用程序。这是上一章的延续;新组件突出显示在bold.

    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=http://www.jc2182.com:8080/manager
    
    tomcat.manager.username=jc2182
    
    tomcat.manager.password=secret
    
    

    构建.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 作为我们的应用服务器。
    首先,在构建属性文件中,我们定义了一些额外的属性,如下所述 -
    • appserver.home指向Tomcat应用服务器的安装路径。
    • appserver.lib指向Tomcat安装文件夹中的库文件。
    • deploy.path变量现在指向 Tomcat 中的 webapp 文件夹。
    可以使用 Tomcat 管理器应用程序停止和启动 Tomcat 中的应用程序。管理器应用程序的 URL、用户名和密码也在 build.properties 文件中指定。
    接下来,我们声明一个新的 CLASSPATH,其中包含catalina-ant.jar. 通过 Apache Ant 执行 Tomcat 任务需要此 jar 文件。
  • 任务

    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
    重新加载任务需要额外的参数如下 -
    • 管理器应用程序的 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 应用程序并重新加载 Web 应用程序。