eclipse使用Ant组件自动部署项目到tomcat6.0

来源:互联网 发布:黑龙江财经学院数据库 编辑:程序博客网 时间:2024/05/16 18:10

手动部署javaweb项目到tomcat服务器实属麻烦,贴来贴去。但有了Ant工具,我想你会瞬间爱上它,稍微配置一下Ant就帮你自动部署项目到tomcat上了,下面描述下使用ant部署项目的步骤:

准备工具:

1.tomcat6.0,当然可以使用其他版本的服务器,但我实验成功的是tomcat6.0。

2.eclipse或myeclipse,他们内置了ant组件,但是。。。。。后面会讲到。

3.到官网下载Ant组件,然后像配置jdk一样在自己的电脑上配置环境变量,不会的找度娘。

准备就绪后,下面可以配置ant了,跟着来哦!

1).在eclipse中配置环境,把tomcat6.0下的lib文件夹下的catalina-ant.jar包加载到eclipse中的Ant组件classpath下,很重要哦,这步。tomcat6.0能够实验成功,其他的tomcat版本貌似不行,其实是我不会弄,原因后面会讲到。贴图:

            

2).在eclipse中新建一个web程序测试:注意图中的标注

                                                            

3).新建一个build.xml配置文件:

<project name="AntTest2" default="redeploy" basedir="."> <!-- Configure the directory into which the web application is built --><property name="build" value="${basedir}/WebContent/WEB-INF/classes" /><property name="dist.dir" value="${basedir}/dist" /> <!-- Configure the context path for this application --><property name="path" value="/AntTest2" /> <!-- Configure properties to access the Manager application --><property name="url" value="http://119.29.84.20:6666/manager" /><property name="username" value="admin" /><property name="password" value="admin" /> <!-- Configure the custom Ant tasks for the Manager application -->    <target name="_def_tomcat_tasks">                   <!-- tasks: deploy,undeploy,reload,stop,start,list,roles,resources -->                  <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="resources" classname="org.apache.catalina.ant.ResourcesTask" />          <taskdef name="roles"     classname="org.apache.catalina.ant.RolesTask" />                   <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" />   </target>  <!-- Executable Targets --><target name="compile" description="WebSip manage">    <mkdir dir="${dist.dir}"/>     <war destfile="${dist.dir}/AntTest2.war" webxml="WebContent/WEB-INF/web.xml">        <classes dir="WebContent/WEB-INF/classes" />         <fileset dir="WebContent" excludes="WEB-INF/**" />         <lib dir="WebContent/WEB-INF/lib" />    </war></target> <target name="redeploy" description="Remove and Install web application" depends="_def_tomcat_tasks">       <antcall target="undeploy"/>             <antcall target="deploy"/>   </target>         <target name="deploy" description="Install web application" depends="_def_tomcat_tasks,compile">    <deploy url="${url}" username="${username}" password="${password}" path="${path}" war="${dist.dir}/AntTest2.war" /><delete dir="${dist.dir}"/><!-- 上传成功后,删除本地文件目录 --></target> <target name="undeploy" description="Remove web application" depends="_def_tomcat_tasks">    <undeploy url="${url}" username="${username}" password="${password}" path="${path}" /></target>    <target name="reload" description="Reload web application" depends="_def_tomcat_tasks,compile">         <reload  url="${url}" username="${username}" password="${password}" path="${path}"/>  </target> </project>
注意:tomcat7.0+没有org.apache.catalina.ant.RolesTask类,但tomcat6.0有该类。所以我使用tomcat6.0能够正常部署。因此对于新手来说还是使用tomcat6.0部署相对简单。

4).代码敲好后,接下来就是配置tomcat了,因为自动部署需要访问tomcat中的manager,所以首先需要解决访问权限的问题。未配置时,可以http://localhost:8080/manager访问一下,他要你输入账号密码,但是我怎么知道是什么。这是管理员权限,所以要到tomcat下的conf下的tomcat_user.xml修改管理员账号密码。

<tomcat-users><!--  NOTE:  By default, no user is included in the "manager-gui" role required  to operate the "/manager/html" web application.  If you wish to use this app,  you must define such a user - the username and password are arbitrary.--><!--  NOTE:  The sample user and role entries below are wrapped in a comment  and thus are ignored when reading this file. Do not forget to remove  <!.. ..> that surrounds them.-->  <role rolename="tomcat"/>  <role rolename="role1"/>  <role rolename="manager-gui"/>  <role rolename="manager-script"/>  <user username="admin" password="admin" roles="manager-gui,manager-script"/>  <user username="tomcat" password="tomcat" roles="tomcat"/>  <user username="both" password="tomcat" roles="tomcat,role1"/>  <user username="role1" password="tomcat" roles="role1"/></tomcat-users>
这时你访问http://localhost:8080/manager就可以输入admin进入服务器管理系统管理你自己的项目了。切记:

<role rolename="manager-script"/>

<user username="admin" password="admin" roles="manager-gui,manager-script"/>这两句很重要,manager-script角色如果没有设置那你不能使用ant自动部署项目,他会报IO异常提示403错误,该错误是因为权限问题。详细请到tomcat官网查询 官方文档。遇到问题最好先自行查询官方文档,提高解决问题的能力。

0 0
原创粉丝点击