利用ant進行遠程tomcat部署

在javaEE項目中,須要將工程部署到遠程服務器上,若是部署的頻率比較高,手動部署的方式就比較麻煩,能夠利用Ant工具實現快捷的部署。這篇博文詳細介紹了ant配置的步驟(http://www.cnblogs.com/GloriousOnion/archive/2012/12/18/2822817.html),可是在tomcat7以上不適用,須要修改配置,具體以下:html

1.配置tomcat的用戶角色java

tomcat7中的用戶角色有:web

manager-gui — Access to the HTML interface.
manager-status — Access to the "Server Status" page only.
manager-script — Access to the tools-friendly plain text interface that is described in this document, and to the "Server Status" page.
manager-jmx — Access to JMX proxy interface and to the "Server Status" page.apache

咱們要用到的是manager-script,在tomcat-users.xml 中進行配置。加入如下代碼:tomcat

<role rolename="manager-script" />服務器

<user username="用戶名" password="密碼"  roles="manager-script">app

2.配置Ant環境工具

之前在 6.0 的時候, 咱們會在 classpath中加入catalina-ant.jar 包,具體操做爲 :window-->preferences,左邊:ant-->runtime,在右邊的 classpath標籤中的global entries 下加入 external jars,路徑指向 tomcat_home/lib/catalina-ant.jar, 只需這一個便可,可是如今 7.0得再加幾個才行:
lib/catalina-ant.jar,lib/tomcat-coyote.jar,lib/tomcat-util.jar,bin/tomcat-juli.jar

3.編寫build.xml文件ui

<project name="工程名" default="redeploy" basedir=".">this

  <!-- Configure the directory into which the web application is built -->

  <property name="build" value="${basedir}/build"/>

 

  <!-- Configure the context path for this application -->

  <property name="path" value="/應用的名稱"/>

 

  <!-- Configure properties to access the Manager application -->

  <property name="url"      value="http://你的域名/manager/text"/>

  <property name="username" value="步驟1中配置的用戶名"/>

  <property name="password" value="步驟1中配置的密碼"/>

 

  <!-- Configure the custom Ant tasks for the Manager application -->

  <taskdef name="deploy"    classname="org.apache.catalina.ant.DeployTask"/>

  <taskdef name="list"      classname="org.apache.catalina.ant.ListTask"/>

  <taskdef name="reload"    classname="org.apache.catalina.ant.ReloadTask"/>

  <taskdef name="findleaks" classname="org.apache.catalina.ant.FindLeaksTask"/>

  <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask"/>

  <taskdef name="start"     classname="org.apache.catalina.ant.StartTask"/>

  <taskdef name="stop"      classname="org.apache.catalina.ant.StopTask"/>

  <taskdef name="undeploy"  classname="org.apache.catalina.ant.UndeployTask"/>

 

  <!-- Executable Targets -->

  <target name="compile" description="Compile web application">

    <!-- ... construct web application in ${build} subdirectory, and

            generated a ${path}.war ... -->

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

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

             <war destfile="${build}/school.war" webxml="WebRoot/WEB-INF/web.xml">

              <classes dir="WebRoot/WEB-INF/classes">

                   <exclude name="**/*.xml"/> 

              </classes>

              <lib dir="WebRoot/WEB-INF/lib" />

            <fileset dir="WebRoot"> 

                <include name="**/**.*" /> 

                <exclude name="**/*.jar"/> 

                <exclude name="**/*.class"/>

            </fileset>

          </war>

  </target>

 

  <target name="deploy" description="Install web application" depends="compile">

    <deploy url="${url}" username="${username}" password="${password}" path="${path}" war="${build}/school.war"/>

  </target>

 

  <target name="reload" description="Reload web application" depends="compile">

    <reload  url="${url}" username="${username}" password="${password}" path="${path}"/>

  </target>

 

  <target name="undeploy" description="Remove web application">

    <undeploy url="${url}" username="${username}" password="${password}" path="${path}"/>

  </target>

      

       <target name="redeploy" description="Remove and Install web application">   

           <antcall target="undeploy"/>

              <antcall target="deploy"/>

       </target>

</project>

最後運行該文件,你的工程就能夠部署到遠程tomcat上了。

具體的說明可參考官方的文檔:

http://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html#Executing_Manager_Commands_With_Ant 

相關文章
相關標籤/搜索