Tomcat – Deployment with tomcat7 manager, change directory for uploaded war

tomcat

I am fighting with .war deployments using tomcat manager, it is starting webapp twice because I have defined hosts under server.xml. So it deployes one app for example.com and other one for /example.

As far as I know only way how to deal with that is to place my .war outside webapps folder, but is there any possibility to specify directory from which to run my war when deploying?

        <Host name="myapp.com" appBase="/var/lib/tomcat7/webapps/myapp" unpackWARs="true" autoDeploy="true">
            <Context path="" docBase="."/>
        </Host>

        <Host name="deploy.myapp.com" appBase="/var/lib/tomcat7/webapps/manager" unpackWARs="true" autoDeploy="true">
            <Context path="" docBase="."/>
        </Host>
    </Engine>

Best Answer

According to the docs...

When using automatic deployment, the docBase defined by an XML Context file should be outside of the appBase directory.

You're setting docBase to ".", which is not outside of the appBase directory. This is not good and can cause double deployments. There's a handful of ways you can fix this.

1.) Restructure the folders under "/var/lib/tomcat7/webapps" so that there are "myapp.com" and "deploy.myapp.com" sub directories. Then change the appBase for each Host tag so that it points to the appropriate sub directory. With that in place remove the Context tag, which is not recommended in server.xml anyway, and simply rename the "myapp" subdirectory to "ROOT" (exactly like that, case is important). Do the same for "manager", rename it "ROOT".

That'll give you a structure like this:

/var/lib/tomcat7/webapps/myapp.com/ROOT /var/lib/tomcat7/webapps/deploy.myapp.com/ROOT

The net result is that you'll have two hosts, each host with one app deployed as the ROOT application (i.e. /).

2.) Continue with things as they are but set autoDeploy to false. You're defining the contexts directly in server.xml, so you don't need autoDeploy. In fact, disabling it should get rid of the double deployments.

A slight alteration to this approach would be to lead autoDeploy as true but use the deployIgnore attribute so that Tomcat's auto deployment skips the apps you've manually configured with contexts in server.xml.

I'd recommend option #1 as it's the cleanest approach.