Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven war application setting up contextroot

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>
like image 785
krisdigitx Avatar asked Jul 29 '16 14:07

krisdigitx


People also ask

How do you set root context?

You specify the context root when you deploy a web module. A context root must start with a forward slash (/) and end with a string. In a packaged web module for deployment on the Application Server, the context root is stored in sun-web. xml .

How do you change the web context root in an application?

1.1 Right click on the project, select Properties , Web Project Settings , update the context root here. 1.2 Remove your web app from the server and add it back. The context root should be updated. 1.3 If step 2 is failing, delete the server, create a new server and add back the web app.

What is the Context_root in a JEE web application URL?

The context root for an application defines the location at which the module can be accessed. The context root is part of the URL you use to connect to the application.

How do I create a war file in Maven?

Using the mvn:war:exploded command, we can generate the exploded WAR as a directory inside the target directory. This is a normal directory, and all the files inside the WAR file are contained inside the exploded WAR directory.


2 Answers

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

like image 101
Nikhil Bhide Avatar answered Sep 17 '22 11:09

Nikhil Bhide


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

like image 22
wemu Avatar answered Sep 17 '22 11:09

wemu