问题
we have a maven generated webapp that runs in an embedded jetty server all stuffed into a fat jar. This seems to work fine but our UI developer is unhappy because when doing his UI stuff he wants to make changes to his code and not have to restart the webapp to have it show up. It was working like that before I did all the jar stuff. we have a resources section in the pom that looks like this:
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>webapp</targetPath>
</resource>
</resources>
We've determined what seems to be happening is that eclipse is copying the webapp over to its location in target and then being executed from there. What he's wanting, I think, is to have eclipse use the webapp source in the src folder and only copy the webapp over when packaging for deployment.
What would be the best way to set this up to work in the expected way?
回答1:
You have a split up resource scenario.
Traditionally, a WebApp has 1 place to go, the webapp resource base (WebAppContext.setBaseResource() in Jetty). This is the where all static content, WEB-INF
metadata (xml), WEB-INF/classes
, and WEB-INF/lib/*.jar
files are loaded from.
In your scenario you are loading it all from your target/mywebapp-1.0-SNAPSHOT
(actual path defined by maven-war-plugin's ${webappDirectory})
This is typical and normal behavior for a production capacity WebAppContext
, in that it is configured to use a simple War path or Base Resource. However you are editing files in your src/main/webapp/
directory (actual path defined by maven-war-plugin's ${warSourceDirectory}), which isn't part of the Base Resource directory.
What you need to do is disregard the static content in ${webappDirectory}
and use the static content in ${warSourceDirectory}
for the purposes of the WebAppContext.setBaseResource()
, and use the classes and jars (maven dependencies) from outside of the ${baseResource}/WEB-INF/classes
and ${baseResource}/WEB-INF/lib
General Technique:
WebAppContext context = new WebAppContext();
context.setContextPath("/");
switch (getOperationalMode())
{
case PROD:
// Configure as WAR
context.setWar(basePath.toString());
break;
case DEV:
// Configuring from Development Base
context.setBaseResource(new PathResource(basePath.resolve("src/main/webapp")));
// Add webapp compiled classes & resources (copied into place from src/main/resources)
Path classesPath = basePath.resolve("target/thewebapp/WEB-INF/classes");
context.setExtraClasspath(classesPath.toAbsolutePath().toString());
break;
default:
throw new FileNotFoundException("Unable to configure WebAppContext base resource undefined");
}
For a live version of this behavior, see the embedded-jetty-live-war example project maintained by the Eclipse Jetty project team.
回答2:
I'm not sure this is a Maven POM issue. Isn't that just for packaging?
What are you using for your resource base? WebAppContext's setResourceBase needs to be set to the root of the web app.
来源:https://stackoverflow.com/questions/33136088/updating-embedded-jetty-webapp-in-eclipse-during-run