Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

maven

I like to know how the context path is set in a java web application.
More precisely in case of a maven project, does the context path get set from the pom.xml file?
Is the context path value referred anywhere in the web application or is it just the name of the WAR file?
Is there a possibility that the name of the WAR file and context path are different?

like image 224
Anonymous Avatar asked Jan 28 '16 14:01

Anonymous


3 Answers

I´m using Eclipse/Maven/Wildfly and the only solution was create a file "jboss-web.xml" at "WEB-INF" folder, inserting the content below;

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
    <context-root>MyContextName</context-root>
</jboss-web>
like image 150
Paulo A. Costa Avatar answered Nov 08 '22 15:11

Paulo A. Costa


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.

like image 8
Luiggi Mendoza Avatar answered Nov 08 '22 15:11

Luiggi Mendoza


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

like image 5
Nikhil Bhide Avatar answered Nov 08 '22 16:11

Nikhil Bhide