28

How can I tell e.g. Tomcat to use a specific context path when given my WAR-File?

Example: I have a war file created by maven build and the resulting name of the file is rather long. So I do not want the tomcat manager application to use the filename of the war as the context.

Supplying a context.xml in META-INF did not produce the desired results

I also found this in the documentation for the path attribute of Context:

The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

So it does not seem to be the right way to tell the application-server what the path for my WAR should be.

Any more hints?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
er4z0r
  • 4,711
  • 8
  • 42
  • 62

4 Answers4

34

There are two important points in the the documentation of the Context Container:

  • In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The name of the file (less the .xml extension) will be used as the context path. Multi-level context paths may be defined using #, e.g. foo#bar.xml for a context path of /foo/bar. The default web application may be defined by using a file called ROOT.xml.
  • Only if a context file does not exist for the application in the $CATALINA_BASE/conf/[enginename]/[hostname]/, in an individual file at /META-INF/context.xml inside the application files. If the web application is packaged as a WAR then /META-INF/context.xml will be copied to $CATALINA_BASE/conf/[enginename]/[hostname]/ and renamed to match the application's context path. Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase.

So, when you bundle a META-INF/context.xml, the file gets renamed to the name of the WAR and this name becomes the context path, regardless of any path defined in the Context element.

I see thus two options here:

  1. Either set the name of the generated war to a shorter name (I suggest using <finalName> over <warName> which is deprecated AFAIK):

    <project>
      ...
      <build>
        <finalName>mycontext</finalName>
        ...
      </build>
      ...
    </project>
    
  2. Or use the maven-tomcat-plugin for the deployment and set the context path in the plugin configuration:

    <project>
      ...
      <build>
        ...
        <plugins>
          ...
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <configuration>
              <path>/mycontext</path>
            </configuration>
          </plugin>
          ...
        </plugins>
        ...
      </build>
      ...
    </project>
    
Christian
  • 3,708
  • 3
  • 39
  • 60
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 1
    Thanks for the snippet. The sentence below is critical for my stuff. Comment/reference added to the context.xml file itself as warning for next developers. "Once this file exists, it will not be replaced if a new WAR with a newer /META-INF/context.xml is placed in the host's appBase." – argatxa Aug 18 '10 at 12:20
6

I found an easy solution to keep war file name and choose the context-path.

You just have to deploy your war outside of the Host's appBase and to create a link inside the appBase directory.

Ex. :

ln -sf ${CATALINA_HOME}/wars/myapp-0.0.8-SNAPSHOT.war ${CATALINA_HOME}/webapps/myapp.war

Ektor

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Ektor
  • 163
  • 1
  • 7
4

You can set the path attribute of the <Context> element of your META-INF/context.xml.

Alternatively, you can configure maven to create the war artifact with a custom name:

<build>
    <plugins>
         <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <warName>yourCustomWarName</warName>
            </configuration>
        </plugin>
        ........
    </plugins>
</build>
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 4
    Sorry to be a broken record here, but setting the Path doesn't actually work in Tomcat (tested on 6.0.28), as described by Pascal above. – John Mar 24 '11 at 10:23
  • 1
    I'd downvote for the 1st sentence as it's simply not true, context path comes from the WAR file name, but the rest of the answer is good – chrisbunney Sep 29 '11 at 11:49
  • @chrisbunney the context path comes from the war file name by default, but that can be overridden – Bozho Sep 29 '11 at 11:52
  • Perhaps you could elaborate? All my investigation leads me to believe that the path attribute is ignored except when the Context element is defined in server.xml, and I've not found any way to change that behaviour – chrisbunney Sep 29 '11 at 13:29
  • I can't test right now, but I am almost sure this worked at some point. – Bozho Sep 29 '11 at 13:37
  • 2
    you CAN NOT override context path being defined by war name if you are in autoDeploy mode – Chris DaMour Jun 02 '14 at 22:46
3

In your project there is a folder META-INF, in that folder there is a context.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<Context  path="/myproject" />
Mark
  • 16,772
  • 9
  • 42
  • 55
  • I would like to use this approche, but when tomcat unpack my war, it create a folder with the same name as the war, plus it create a .xml file in /opt/apache-tomcat-6.0.29/conf/Catalina/localhost/, wich is a copy of the context.xml defined as you mentioned. Do you understand why? – benzen Nov 11 '10 at 15:24
  • I agree - this approach certainly doesn't work in Tomcat 6.0.28 for the reasons described by Pascal – John Mar 24 '11 at 10:20
  • I've never been able to get this approach to work, and the documentation I've seen does suggest that the path attribute is only used when setting the context in server.xml (otherwise it's inferred from the file name) – chrisbunney Sep 29 '11 at 11:51
  • you CAN NOT override context path being defined by war name if you are in autoDeploy mode – Chris DaMour Jun 02 '14 at 22:46