10

i am building a war application file using the below maven config, however when i start the application in tomcat the Context Root is set to "/CommerceApi-0.0.1-SNAPSHOT/"

I want this to be set to "/api",

any ideas?, below is the pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>CommerceApi</groupId>
  <artifactId>CommerceApi</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
   <dependencies>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>CommerceApiCommon</groupId>
      <artifactId>CommerceApiCommon</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>
  </dependencies>
</project>
krisdigitx
  • 7,068
  • 20
  • 61
  • 97

3 Answers3

16

There are three ways to do it:

1. If you are not using Eclipse/MyEclipse to deploy the application onto application server -

You need to make use of maven-war plugin, you can specify warName in configuration section.

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <warName>customwarname</warName>
    </configuration>
</plugin>

2. If you are using Eclipse/MyEclipse to deploy the application onto application server -

If you are using eclipse and deploying war using eclipse then you can use following maven configuration.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <wtpversion>2.0</wtpversion>
        <wtpContextName>customwarname</wtpContextName>
    </configuration>
</plugin>

Then, run following commands to update eclipse settings.

   mvn eclipse:eclipse -Dwtpversion=2.0

Restart Eclipse and then navigate to project properties, Properties->Web to view the reflected changes in root-context value or navigate to Deployment Assembly of the project to view the changes

Note that above can be achieved using m2eclipse by adding a new plugin.

3. Application server specific: You should prefer to follow server agnostic approach, but if are required to do it then you can configure root context url in server specific configuration file. You can find detailed approach here

Nikhil Bhide
  • 728
  • 8
  • 23
  • 1
    `maven-eclipse-plugin` is [retired](https://maven.apache.org/plugins/maven-eclipse-plugin/)...is there an alternate? – trebor Jul 14 '19 at 07:20
2

Your application is not in charge to define its own context path. That's task of the container, the Tomcat in your case. Tomcat offers several options of how to set the context path. You may define the context path it in a context file or specify the context path in the manager application. If you use Jenkins or other CI tools you'd be able to specify the context path there, as well.

Best you read up on the options you have regarding your particular Tomcat version.

Jan B.
  • 6,030
  • 5
  • 32
  • 53
  • Why the server tomcat is called as a container? Is it similar to the containerization (docker etc.) that people refer to? #NewbieHere – sofs1 Mar 06 '18 at 06:48
  • _Container_ is for sure a very commonly used word for different contexts. Here, it is a short version of _web container_ or _servlet container_. I don't see much similarities to Docker, actually. – Jan B. Apr 15 '18 at 21:50
1

There are several options. Some are described in Define Servlet Context in WAR-File

Using tomcat you can also define the context.xml path: http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html#containerConfigXML and maybe configure it in there: https://tomcat.apache.org/tomcat-7.0-doc/config/context.html

the fastest way os probably to change the final name (see other stackoverflow question).

Community
  • 1
  • 1
wemu
  • 7,952
  • 4
  • 30
  • 59