Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Undeploy Task in Tomcat 6

Tags:

java

tomcat

ant

I am using Tomcat 6, Apache Ant 1.7.1 and JDK 1.6 on OS X Snow Leopard...

Created the following build script and supporting property files:

(1) build.xml

<?xml version="1.0"?>
<project name="${project.name}" default="deploy" basedir=".">
    <property file="build.properties"/>
    <property file="admin.properties"/>

    <taskdef file="tomcatTasks.properties">
      <classpath>
        <pathelement path="${tomcat.home}/lib/catalina-ant.jar"/>
      </classpath>
    </taskdef>

    <target name="prepare">
       <mkdir dir="${webapp.build.dir}" />
       <mkdir dir="${webapp.build.dir}/WEB-INF" />
       <mkdir dir="${webapp.build.dir}/WEB-INF/lib" />
       <mkdir dir="${webapp.build.dir}/WEB-INF/classes" />
    </target>

    <target name="static" depends="prepare">
       <!-- Copy web files -->
       <copy todir="${webapp.build.dir}/">
           <fileset dir="web" />
       </copy>
       <!-- Copy webapp configuration files -->
       <copy todir="${webapp.build.dir}/WEB-INF/">
          <fileset dir="etc" />
       </copy>
       <!-- Copy properties files -->
       <copy todir="${webapp.build.dir}/WEB-INF/classes">
         <fileset dir="props" />
       </copy>
       <!-- Copy jar files -->
       <copy todir="${webapp.build.dir}/WEB-INF/lib/">
          <fileset dir="lib" />
       </copy>
    </target>

    <target name="compile" depends="static">
       <javac srcdir="src"
           destdir="${webapp.build.dir}/WEB-INF/classes/"
           deprecation="off" debug="on" optimize="off">
           <classpath>
               <!-- Include all JAR files in my local library -->
               <fileset dir="lib">
                  <include name="*.jar"/>
               </fileset>
               <!-- Include all common libraries in Tomcat -->
               <fileset dir="${tomcat.home}/lib">
                   <include name="*.jar"/>
               </fileset>
           </classpath>
       </javac>
    </target>

    <target name="deploy" depends="compile">
       <jar jarfile="${build.dir}/${webapp.name}.war" basedir="${webapp.build.dir}" />
       <!-- Copy the newly built WAR file into the deployment directory -->
       <copy file="${build.dir}/${webapp.name}.war" todir="${tomcat.deployment.dir}" />
    </target>

    <target name="clean" description="Clears all generated files, including build directories, distributables, and documentation.">
       <delete dir="${build.dir}"/>
    </target>

    <target name="package">
       <jar jarfile="${build.dir}/${webapp.name}.war" basedir="${webapp.build.dir}" />
    </target>

    <target name="install" description="Install application in Tomcat" depends="package">
        <deploy url="${tomcat.manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${webapp.name}"
            war="file:${tomcat.deployment.dir}/${webapp.name}.war"/>
    </target>

    <target name="undeploy" depends="clean" description="Remove application in Tomcat">
        <undeploy url="${tomcat.manager.url}"
              username="${tomcat.username}"
              password="${tomcat.password}"
              path="/${webapp.name}"/>
    </target>
</project>

(2) admin.properties:

tomcat.server=localhost
tomcat.manager.url=http://${tomcat.server}:8080/manager
tomcat.username=admin
tomcat.password=admin

(3) build.properties:

project.name=myproject
build.dir=./build

# Web app properties for the project
webapp.name=myproject
webapp.build.dir=${build.dir}/${webapp.name}
webapp.virtual.host=localhost
webapp.meta.dir=${webapp.build.dir}/META-INF

# Tomcat properties
tomcat.home=/Users/myuser/DevTools/Java/tomcat/apache-tomcat-6.0.20
tomcat.deployment.dir=${tomcat.home}/webapps

(4) tomcatTasks.properties:

# Tomcat Task Properties
deploy=org.apache.catalina.ant.DeployTask
install=org.apache.catalina.ant.InstallTask
list=org.apache.catalina.ant.ListTask
reload=org.apache.catalina.ant.ReloadTask
remove=org.apache.catalina.ant.RemoveTask
resources=org.apache.catalina.ant.ResourcesTask
roles=org.apache.catalina.ant.RolesTask
start=org.apache.catalina.ant.StartTask
stop=org.apache.catalina.ant.StopTask
undeploy=org.apache.catalina.ant.UndeployTask

(5) tomcat-users.xml:

When I run the deploy target, everything works (it places myproject.war) underneath $CATALINA_HOME/webapps...

However, when I run the undeploy target, I get the following error message:

Problem:

Buildfile: /Users/myuser/work/myproject/build.xml
Trying to override old definition of datatype resources
clean:
    [delete] Deleting directory /Users/myuser/work/myproject/build
undeploy:

BUILD FAILED
/Users/myuser/work/myproject/build.xml:83: java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/undeploy?path=/myproject

Total time: 170 milliseconds

Would really appreciate it if someone could tell me what I am doing wrong...

Thank you for taking the time to read this...

Happy programming.

like image 213
mw_javaguy Avatar asked Feb 28 '23 22:02

mw_javaguy


2 Answers

It's failing because the Tomcat manager application is returning an HTTP status code 403 which means Forbidden. So access is being denied.

When I googled your question I found your post on JavaRanch which includes your tomcat-users.xml file (not included here). I see that there is no user with the manager role which is required to run the undeploy task. Try adding the manager role to your admin user.

<role rolename="manager"/>
<user username="admin" password="admin" roles="admin,manager"/> 

From the Tomcat docs:

To enable access to the Manager web application, you must either create a new username/password combination and associate the role name manager with it, or add the manager role to some existing username/password combination.

If you're wondering why your deploy task seems to be working when your undeploy task does not, it's because the deploy task doesn't appear to be using the manager application for its deployment. Instead it's simply copying the war to the target directory.

like image 175
Asaph Avatar answered Mar 03 '23 12:03

Asaph


Please try to add the role manager-script for the tomcat user you are using.

e.g.

<role rolename="manager-script"/>
<user username="admin" password="admin" roles="manager-script"/>
like image 27
ssong Avatar answered Mar 03 '23 11:03

ssong