Specify context-path for spring-boot WAR - eclipse

I have a spring boot web application that deploys as a WAR. Right now when I deploy it to my Tomcat 7 server, it uses the name of the war file as the context, such as /myartifactid-1.5.4.SNAPSHOT/. I want to specify the context-path, but
server.contextPath=/mywebapp
seems to only work for embedded tomcat. I've added a META-INF/context.xml with only this
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="false" path="/mywebapp" />
but that hasn't had any effect.
EDIT: This is NOT a spring-boot issue, but instead related to how NetBeans behaves differently from Eclipse. NetBeans apparently recognizes the context path parameter in context.xml, but Eclipse doesn't. In Eclipse you have to modify the Web Projects Settings to set the context path.
Changing Tomcat context path of web project in Eclipse

Since you are packaging your Spring Boot application as a war (as opposed to a jar with an embedded tomcat container) the context path will be the name of the war.
For example if you name your packaged application mywebapp.war and you place it under Tomcat's webapps, it will be available under /mywebapp

when the war does not have the same name with the project, the application will not be accessible via the context path on the tomcat,
to fix this you need to go to the POM.xml change the name of package to match the context path name
server.servlet.context-path=/virtualcard
<groupId>com.test.csc.virtualcard</groupId>
<artifactId>virtualcard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>virtualcard</name>
then the war will be: virtualcard.war

Related

Wildfly in Eclipse is adding the Maven SNAPSHOT to the web context

I'm having a new problem when we moved to a Wildfly 8.x server in eclipse.
My project is called PayloadSvc. The first part of the pom.xml looks like this:
<modelVersion>4.0.0</modelVersion>
<groupId>PayloadSvc</groupId>
<artifactId>PayloadSvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
Later in the pom I have this:
<build>
<finalName>PayloadSvc</finalName>
In eclipse I also have my context root in the Web Project Settings set to PayloadSvc
However, when it deploys into Wildfly it deploys this way:
Registered web context: /PayloadSvc-0.0.1-SNAPSHOT
I can't seem to get it to remove the "-0.0.1-SNAPSHOT" part of the web context.
Oddly enough, other projects in the same workplace work correctly even with the same pom data (with appropriate context, groupId, artifact Id, and the same version attribute of course).
I'm using the STS version of Eclipse Neon.
Thank you for any advice you may have.
Just had the same problem solved.
If you don't have it already, create a WEB-INF\jboss-web.xml file, with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>PayloadSvc</context-root>
</jboss-web>
Republish the resource and you're done.
Not sure why the file is not created automatically though.
Edit the file \[root-of-your-project]\.settings\org.eclipse.wst.common.component, changing the deploy-name attribute and context-root value.
Show the server tab by: Window > Show View > Server
Right click on your server then Add and Remove.... Remove all projects from server.
Right click on your server and clean.
Just to be sure, go to the directory where WildFly is installed and delete the content of the directory [wildfly-path]\[standalone-path]\deployments, if it exits.
Add again the projects into your server and run.
Have you tried adding the <name>PayloadSvc</name> element in the root of the pom (same spot as your group,artifact,version elements)?
If that doesn't help, what does the artifact look like when you build it with mvn install? Does it include the version number in the war filename?
For Eclipse Neon:
1 Did a Eclipse Neon 'Check for updates' and updated it.
2 I editted file org.eclipse.wst.common.component adding a blank line and saved (check if deploy-name is suffixed with SNAPSHOT).
3 After that the suffix '0.0.1-SNAPSHOT' disappear from the war name when adding it to Wildfly server.

No persistence.xml file found in project

I'm using JBoss Developer Studio 7 (alpha) and have an EAR project with two WARs and one JAR. The JAR is a JPA project. The JAR contains all JPA Entities and also persistance.xml (inside META-INF folder). So I'd like to use the persitence unit defined in the JAR from both WARs. Since the idea is that each WAR has to use the shared persistance.xml from the JAR (JPA project) y removed the persitence.xml in each WAR MATA-INF folder, but then when I want to build the WAR I get "No persistence.xml file found in project (WAR name)".
How I resolve this? If I set some dummy configuration in the WAR persistance.xml the IDE let me buid withot errors, but I don't like this, I have two persitence units that never will be used.

Context pointing to external directory in Jetty 9

I want to deploy a .war file to Jetty by setting up a context that points to my external .war (outside webapps folder). I know how to do this in Tomcat, but I can't figure out how to do this for Jetty 9.
In Tomcat I placed an .xml file to configure my context in $CATALINA_HOME/conf/Catalina/localhost :
<?xml version='1.0' encoding='utf-8'?>
<!-- Context fragment for deploying ServletExample.war -->
<Context path="/CurrencyServlet" docBase="/Users/macbook/Desktop/School/Java/Temp/CurrencyServlet.war" debug="0" privileged="true"/>
Can anyone provide a simple example for Jetty 9?
Have a look at the provided $jetty_home/webapps/test.xml file. In jetty9 you can configure your webapps, etc. by placing a ContextProvider config file in $jetty_home/webapps.
Have a look at the docs: http://www.eclipse.org/jetty/documentation/current/configuring-contexts.html
This is how you configure the path to your webapp:
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/test</Set>

How to auto set crossContext in an Eclipse WTP Tomcat

How can I set the crossContext directive in the context of an web application so any WTP publish / deploy will set this correct to the generated Applications context.xml (conf/Catalina/localhost/appname.xml)
I tried to set it in the Servers context.xml as "default" but also generated a stub context.xml in the WEB-INF/context.xml of the web application, but nothing seems to help here ;/
The WEB-INF/context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true">
</Context>
Notes:
related to How to get a tomcat project path from different project in same tomcat by using java
http://olafsblog.sysbsb.de/tomcat-5-und-crosscontext-konfiguration/
http://www.eclipse.org/forums/index.php/t/51881/
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
http://www.eclipse.org/forums/index.php/t/50640/
It's simple but not perfect:
After adding the module to tomcat via wtp, open the WTP Tomcat server.xml (typically located in your workspace > Servers > Tomcat vX.X Server at localhost) and add attribute crossContext="true" to the <Context> tag of the corresponding web-module.
check "Publish module contexts to seperate XML files" in Tomcat WTP Frontend
Positive side-effects:
This will survive any "clean Tomcats working directory" or "clean project" actions
(still) Negative:
This will be overridden / removed if you change any WTP Tomcat options that affects server.xml like adding new modules, changing ports... After this action, you have to add it again.
There is still NO frontend option as like for reloadable or ContextPath ;/

deploy.last in JBoss 5.1 in web configuration?

We have ear that depends on war file.
We use web configuration.
I put war file to <jboss_home>/server/web/deploy directory.
And I put ear file to <jboss_home>/server/web/deploy/deploy.last directory.
But ear starts prior to war.
Why?
I use a similar configuration to what you describe, though I put all the apps I want to deploy first in deploy/myapps and all the ones to deploy afterwards in deploy/myapps.last. This works correctly for me on JBoss 5.1.2.
Although I can't explain why it isn't working for you, I can offer an alternative solution. You can make the EAR declare a dependency on the WAR and JBoss will then ensure the WAR is deployed first.
First, add a file called aliases.txt into the META-INF directory of your WAR. This file should just contain a single line with an arbitrary name / identifier for your WAR. For example, if you have mywebapp.war, your META-INF/aliases.txt file could contain 'mywebapp'. It just needs to be something that won't clash with any other aliases declared by other apps deployed on the same server.
Next, add a jboss-dependency.xml file to the META-INF directory of your EAR, containing the following (subsituting 'mywebapp' for the alias you created above):
<dependency xmlns="urn:jboss:dependency:1.0">
<item whenRequired="Real" dependentState="Create">mywebapp</item>
</dependency>
This should ensure the WAR is deployed before the EAR.
Also, if you try to deploy the EAR without the WAR being present, JBoss will log a clear deployment error message telling you about the missing dependency.