Context path different of WAR name with Maven - java

I have a web application (WAR) that I want to deploy in Tomcat 5. I'm using Maven 3 and the problem is that when I deploy the WAR, Tomcat automatically copies the context.xml file located in META-INF directory. The file is then renamed with the WAR name and the context path of the application will be the WAR name. By default, it is artifactId-version.war.
The problem is that some client code uses the context path to connect to the web application and I don't want to change the code each time a new version of the web application is deployed. Is there a way to set the context path to another fixed value?
I can't use the tomcat plugin for now. Setting the Context path in the context.xml doesn't help. Also, tomcat documentation precises that it's not recommended to set the Context path in the server.xml. Also, I don't want to change the WAR name, it's important for me to always keep track of the artifact version.
Thanks

You should use the ${project.build.finalName} parameter in your war plugin config so that you can ensure that the war never changes names.

Related

Changing the context path of deployed (Spring Boot) war on a container tomcat server. So it is not demo-0.01-SNAPSHOT

I have tried to deploy my site using many configurations to make it change from the war name being deployed. Is there a way to do this easily. The deployment will to a tomcat sitting somewhere else. e.g. bitnami instance
I have tried various combinations of settings in the application.properties but none make any difference:
server.servlet.context-path=/a
server.servlet.path=/b
spring.webservices.path=/c
server.contextPath=/m
The war is called demo-0.0.01-SNAPSHOT.war and when dropped in the webapps directory it creates the same named directory and the site then has a /demo-0.0.01-SNAPSHOT path. I thought with the days of Spring Boot this would be easy now but a fool like me hasn't worked it out yet. Or do people just deploy embedded versions with tomcats and run them nowadays?
(BTW I have tried the root.xml as well, no luck ... unless i did it wrong on my windows box, testing on my dev box first, linux for deployment)
Any help would be appreciated.
Thanks
The Spring Boot property to specify the context path of the application is : server.contextPath.
But the fact that you are deploying your WAR into a standalone Tomcat instance doesn't allow to use it.
In this configuration, you cannot use server.contextPath and other properties specific to the container (as for example server.port).
These are designed to work with the embedded Tomcat instance.
The standalone Tomcat instance keeps indeed the control on these facilities provided by Spring Boot.
So you have to configure it from the configuration file of the standalone Tomcat (server.xml or ROOT.xml way generally).
Adding finalName setting to pom.xml for maven will make the packaged war filename to it. For example.
<build>
...
<finalName>myapp</finalName>
</build>
The packaged filename will be myapp.war . When you deploy to tomcat it will set the context to the filename.

Configuring the context path of a web app outside the WAR's WEB-INF directory

I am trying to deploy my web app as a WAR file to WildFly 9.
I can specify the context path of the web app by renaming the WAR file in the deployments directory or by specifying the context in WEB-INF/jboss-web.xml inside the WAR file.
Is there a way to specify the context path outside the WEB-INF directory of the WAR file? This way it would be possible to change the context path after packaging the WAR.
You can specify the context path at deployment time by using the CLI to perform the deployment.
[steve#mylaptop wildfly-9.0.0.Final]$ bin/jboss-cli.sh
You are disconnected at the moment. Type 'connect' to connect to the server or 'help' for the list of supported commands.
[disconnected /] connect
[standalone#localhost:9990 /] deploy /path/to/my-app-1.0-SNAPSHOT.war --runtime-name=foo.war
This will result in your application being available at /foo
Use
deploy --help
for more information.
You can also do this via WildFly Maven Plugin as part of your CI job or similar.
We are using following command to deploy the war file to another context root:
org.wildfly.plugins:wildfly-maven-plugin:deploy-only
-Dwildfly.deployment.filename=app.war
-Dwildfly.deployment.runtime.name=appcontext.war
The app will be deployed under /appcontext.
Note, you should remove the context-root from your jboss-web.xml otherwise this value will win always.
There is the possibility to change context root of war application in runtime using CLI console Command Line Interface in Wildfly. It's possible with command deployment-overlay, in this way:
deployment-overlay add --name=myAppOverlay \
--content=/WEB-INF/jboss-web.xml=/myFiles/new-jboss-web.xml \
--deployments=myApp.war --redeploy-affected
This command forces Wildfly to load the content of file new-jboss-web.xml into the internal Wildfly cache and use it (during every deployment of myApp.war) as the replacement of /WEB-INF/jboss-web.xml file of your war web-archive. As you already know, context of web application could be therefore defined in new-jboss-web.xml in following way:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>myNewPathToApp</context-root>
</jboss-web>
Besides add action, deployment-overlay CLI command has also other actions in order to manipulate with deployment overlays (e.g. remove overlay, etc.).
The context root for a web application is determined in three different ways:
If it's deployed as part of an EAR, the context root is specified in the application.xml file of the EAR.
If it's deployed as a standalone WAR, the context root can be specified in the WEB-INF/jboss-web.xml.
If no context root is specified, the context root will be the name of the WAR file.
I don't know a fourth option.
See also the official documentation: https://docs.jboss.org/jbossas/guides/webguide/r2/en/html/ch06.html

How to get the path of an .ear file in application server?

I use JBoss 7.0 and WebSphere 8.5 application servers to deploy my .ear file which has .wars and EJB .jars.
How to get my .ear file path using Java eg. jboss7.0/standalone/deployments/sample.ear? If needed, I can use a separate Java servlet class.
If you are in a war file you can call ServetContext.getRealPath("/") which will give you the file system path to the war file. The war should be in the ear. This of course assumes that these are expanded as directories and not .war/.ear files (getRealPath only returns a non-null path in the former case).

Change context-root inside WAR file

I am really new to WebLogic deployments. I have the below situation:
I have a war file which is already deployed on WebLogic 8.1, I want to pick the same war and deploy it on the same server without bringing the original site down. This would definitely give me a exception saying the context path already exists. I have no way of recreating the war file, hence I will somehow have to modify the war file to change the context root. Is this possible?
And if it is possible could you also confirm that both using the same data-source(JNDI) would not cause any issues to the existing site.
Thanks,
Sahana
Yes you can deploy the same war file multiple times and yes you can change the context root. You will want to do something like the following if you cannot rebuild the war file yourself.
Unzip the war file (jar xvf myfile.war)
This isn't entirely necessary but it will help you understand the structure of the war file. Zip tools can modify a file in place. Try 7zip or use the Windows zip utility via right-click Open
Edit the weblogic.xml file with the new root <context-root>/new-root</context-root>
Rezip the war file (jar cvf mywar.war folder_it_is_in)
Here are other examples that may help as well:
How to deploy EAR application twice on WebLogic server?
How to deploy the same web application twice on WebLogic 11g?
How can I use Weblogic (12 C) without the application context in the URL?

One application is being run twice in tomcat7

I have deployed one war to tomcat7 webapps directory.
But when tomcat is starting it runs application twice.
What could cause this problem?
I have already met this case, but it was when I was mapping domain to webapp, but know all the configuration is standart, and there are two instances running.
This can happen if you have a symbolic link somewhere in your webapp directory, that points to a war file or exploded war directory. Tomcat simply follows those links and starts all application it finds.
Also check the configuration in conf/server.xml and make sure you haven't set up any other directories as appBase.
It might be possible that in your webapps directory you might have another war or another webapp already present.

Categories

Resources