利用ant进行远程tomcat部署

系统 2253 0

在javaEE项目中,需要将工程部署到远程服务器上,如果部署的频率比较高,手动部署的方式就比较麻烦,可以利用Ant工具实现快捷的部署。这篇博文详细介绍了ant配置的步骤( http://www.cnblogs.com/GloriousOnion/archive/2012/12/18/2822817.html ),但是在tomcat7以上不适用,需要修改配置,具体如下:

1.配置tomcat的用户角色

tomcat7中的用户角色有:

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.

我们要用到的是manager-script,在tomcat-users.xml 中进行配置。加入以下代码:

<role rolename="manager-script" />

<user username="用户名" password="密码"  roles="manager-script">

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文件

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

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

利用ant进行远程tomcat部署


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论