How is the context path set in a Java web application?

Adding answer to provide complete details.

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


The context path is the name of the war file, despite if the project is built via ant, maven, gradle or whatever. If you want to change the context path of your app, then the simplest way would be to change the name of the generated war. In maven, this can be done via plugin, here's an example:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <warName>kasnet-webapp</warName>
    </configuration>
</plugin>

Another way you can do it is using a specific configuration for the application server you're using as depicted here.

Tags:

Java

Maven