Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating war application name from war file name

Currently if i deploy a war file on tomcat named say myapp.war, I can access its url by http://localhost/myapp/MyServlet.

However what I want is to deploy a war with a version number in the war file name and still have the same url. For eg, I want to deploy myapp-1.1.0.war and still have the url be http://localhost/myapp/MyServlet

Of course I need to keep updating the war and the version number will keep changing, so I cant hardcode the war filename anywhere. Is there any setting in web.xml I can use to keep the same url for the app regardless of the war filename?

like image 758
pdeva Avatar asked May 03 '11 20:05

pdeva


People also ask

What is the structure of a war file?

A war file is just a jar file (like a zip archive) with a standard structure. Basic structure: WEB-INF/ - directory for application support lib/ - supporting jar files web.

What is meant by war file?

In software engineering, a WAR file (Web Application Resource or Web application ARchive) is a file used to distribute a collection of JAR-files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web ...

Where is the war file is to be stored?

The location for WAR files is the webapps directory within your Tomcat installation directory.


3 Answers

The solution is to stop using the automatic deployment feature of Tomcat, which takes the shortcut of setting the "context name" (the /myapp part of the URL) to the portion of the WAR filename before ".war".

Instead, extract the WAR contents to the filesystem yourself and setup an XML file at TOMCAT_HOME/conf/[enginename]/[hostname]/[contextname].xml which points the desired context path (such as /myapp) to the location of the application on disk (such as /opt/webapps/myapp-1.1.0/).

The Tomcat reference docs provide a good explanation of how Tomcat deploys applications automatically, and how you can configure customized logic for the mapping of context path to application file location (there are a handful of alternate ways to set this up other than the one I suggest above).

like image 144
matt b Avatar answered Sep 22 '22 11:09

matt b


You can use YOUR_WAR/META-INF/context.xml for this. Here is a sample:

<?xml version="1.0" encoding="UTF-8"?> <Context antiJARLocking="true" path="/MyServlet"/> 
like image 42
Karthik Ramachandran Avatar answered Sep 22 '22 11:09

Karthik Ramachandran


When using Maven you can control your deployment's path by doing the followings:

Tomcat's conf/tomcat-users.xml:

<tomcat-users>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <role rolename="manager-jmx"/>
  <role rolename="manager-status"/>
  <role rolename="admin-gui"/>
  <role rolename="admin-script"/>

  <user username="root" password="root" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

</tomcat-users>

~/.m2/settings.xml :

...
<server>
  <id>tomcat</id>
  <username>root</username>
  <password>root</password>
</server>
...

pom.xml :

...
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>myapp</artifactId>
  <version>1.1.0</version>
  <packaging>war</packaging>
...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>tomcat-maven-plugin</artifactId>
        <configuration>
          <!-- neglect /html below Tomcat7: -->
          <url>http://server:8080/manager/html</url>
          <!-- Refer to the server settings in your ~/.m2/settings.xml -->
          <server>tomcat</server>
          <path>/myWebApp</path>
        </configuration>
      </plugin>
      ....
    </plugins>
  </build>
...

Start your tomcat first Then build and deploy your application..

mvn clean install tomcat:deploy

..it will be accessible under http://server:8080/myWebApp

like image 23
Lorand Bendig Avatar answered Sep 23 '22 11:09

Lorand Bendig