2

I am building a webapplication with maven. I want to change the name of the generated warfile to get a different context path.

To clarify misunderstandings: It is not about changing the name during development, it should be possible without touching any code (e.g. for customers). Also it should be possible to deploy those war on different servers (like WildFly, Tomcat etc.).

Example:

Hello.war = Hello.war -> URL: localhost:8080/Hello

stupid.war = stupid.war -> URL: localhost:8080/stupid

How can I achieve this? Is that even possible?

alexander
  • 1,191
  • 2
  • 20
  • 40
  • 1
    Take a look at this link is helpful [rename.war](http://stackoverflow.com/questions/2437465/java-how-to-change-context-root-of-a-dynamic-web-project-in-eclipse) and [this](http://stackoverflow.com/questions/7276989/howto-set-the-context-path-of-a-web-application-in-tomcat-7-0) – Abdelhak Nov 27 '15 at 13:41
  • You can configure the context path within the war file by using an appropriate configuration. Only need to know which kind of servlet engine you are using? – khmarbaise Nov 27 '15 at 13:42
  • See my edit: It should be possible to modify the warname / context path without touching any source code. So the conclusion is, the customer should be able to modifiy the war file and have a different context path – alexander Nov 27 '15 at 13:47
  • If you don't touch the source than you have to stuck with the name (based on the naming conventions of Maven)...If the customer will change that the customer can simply rename the file so it does not matter which name you gave it to... – khmarbaise Nov 27 '15 at 14:08
  • That's the point @khmarbaise. The customer wants to rename the war without touching the source AND the context path should be the new name of the war file. I guess, that isn't possible. What do you think? – alexander Nov 27 '15 at 14:11
  • May be i misunderstand a thing here. But you say the customer can rename the file ? So i don't see the problem..Let the customer rename it and that's it..? – khmarbaise Nov 27 '15 at 15:41
  • The problem is, the root context won't change, won't it? So maybe, `/a` becomes '/b'? – alexander Nov 27 '15 at 15:47

3 Answers3

4

For popular servlet containers (JBoss, Tomcat, Jetty), WAR naming convention can drive context paths. Name of the war becomes the context path if no explicit context path is defined anywhere.

a.war > localhost:8080/a
b.war > localhost:8080/b

The problem then is just to rename the war into different names as per your clients.

https://docs.jboss.org/jbossas/guides/webguide/r2/en/html/ch06.html https://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Naming http://www.eclipse.org/jetty/documentation/current/configuring-contexts.html

Mohit
  • 1,740
  • 1
  • 15
  • 19
  • Can you please explain `The problem then is just to rename the war into different names as per your clients.` ? – alexander Nov 30 '15 at 08:03
  • Suppose your war is generated with name application.war. In continuation with above answer, you need to create copies of application .war into a.war and b.war. Each renamed war will have its name as context root as defined in links above. – Mohit Nov 30 '15 at 08:18
  • Yeah, that sounds great! Thanks for your answer. – alexander Nov 30 '15 at 08:21
  • What about Oracle Weblogic v12c ?? It is compatible with that feature?? – NBPalomino Jun 05 '18 at 17:16
1

Overriding the default finalName element within the build element to the desired filename (without extension) may archive what you wish. You will then of course need to take care with versions. eg.

<build>
    <finalName>YourName</finalName>
</build>
Caleryn
  • 1,084
  • 3
  • 18
  • 23
  • 1
    This will change the name only for your target folder but not the name which is used if you deploy your artifacts into a repository..(publish it)... – khmarbaise Nov 27 '15 at 15:42
0

the easy way to do this is

  1. to rename the file extension.war to .zip and expand it (double-click it)
  2. drag out the web.xml to desktop
  3. make changes to web.xml accordingly
  4. drag the modified web.xml to its original location and replace the original
  5. rename the file extension back to .war
William Ku
  • 798
  • 5
  • 17