maven war application setting up contextroot

24,636

Solution 1

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

Solution 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.

Solution 3

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).

Share:
24,636
krisdigitx
Author by

krisdigitx

Updated on May 28, 2020

Comments

  • krisdigitx
    krisdigitx about 4 years

    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>
    
  • sofs1
    sofs1 over 6 years
    Why the server tomcat is called as a container? Is it similar to the containerization (docker etc.) that people refer to? #NewbieHere
  • Jan B.
    Jan B. about 6 years
    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.
  • trebor
    trebor almost 5 years
    maven-eclipse-plugin is retired...is there an alternate?