Index: src/main/java/org/mortbay/jetty/plugin/JettyWebAppContext.java =================================================================== --- src/main/java/org/mortbay/jetty/plugin/JettyWebAppContext.java (revision 5898) +++ src/main/java/org/mortbay/jetty/plugin/JettyWebAppContext.java (revision ) @@ -16,10 +16,16 @@ package org.mortbay.jetty.plugin; import java.io.File; -import java.util.List; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.*; import org.eclipse.jetty.annotations.AnnotationConfiguration; import org.eclipse.jetty.plus.webapp.EnvConfiguration; +import org.eclipse.jetty.util.URIUtil; +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.resource.FileResource; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.webapp.Configuration; import org.eclipse.jetty.webapp.FragmentConfiguration; @@ -40,6 +46,9 @@ */ public class JettyWebAppContext extends WebAppContext { + private static final String WEB_INF_CLASSES_PREFIX = "/WEB-INF/classes"; + private static final String WEB_INF_LIB_PREFIX = "/WEB-INF/lib"; + private List classpathFiles; private String jettyEnvXml; private MavenWebInfConfiguration webInfConfig = new MavenWebInfConfiguration(); @@ -54,8 +63,9 @@ private Configuration[] configs; private List overlays; private boolean unpackOverlays; + private File classesDir; + private Map classpathJarFiles = new TreeMap(); - + - public JettyWebAppContext () throws Exception { @@ -103,11 +113,31 @@ return this.jettyEnvXml; } + public File getClassesDir() + { + return classesDir; + } + public void setClassesDir(File classesDir) + { + this.classesDir = classesDir; + } + + public void doStart () throws Exception { setConfigurations(configs); - + + // Initialize map containing all jars in /WEB-INF/lib + classpathJarFiles.clear(); + for (File file : classpathFiles) + { + // Return all jar files from class path + String fileName = file.getName(); + if (fileName.endsWith(".jar")) + classpathJarFiles.put(fileName, file); + } + if (this.jettyEnvXml != null) envConfig.setJettyEnvXml(new File(this.jettyEnvXml).toURL()); setShutdown(false); @@ -121,4 +151,78 @@ Thread.currentThread().sleep(500L); super.doStop(); } + + @Override + public Resource getResource(String uriInContext) throws MalformedURLException + { + Resource resource = null; + // Try to get regular resource + resource = super.getResource(uriInContext); + + // If no regular resource exists check for access to /WEB-INF/lib or /WEB-INF/classes + if ((resource == null || !resource.exists()) && uriInContext != null && classesDir != null) + { + String uri = URIUtil.canonicalPath(uriInContext); + + try { + // Replace /WEB-INF/classes with real classes directory + if (uri.startsWith(WEB_INF_CLASSES_PREFIX)) + { + String classesPath = classesDir.getPath(); + String newPath = uri.replace(WEB_INF_CLASSES_PREFIX, classesPath); + return Resource.newResource(newPath); -} + } + // Return the real jar file for all accesses to /WEB-INF/lib/*.jar + else if (uri.startsWith(WEB_INF_LIB_PREFIX)) + { + String jarName = uri.replace(WEB_INF_LIB_PREFIX, ""); + if (jarName.startsWith("/") || jarName.startsWith("\\")) + jarName = jarName.substring(1); + if (jarName.isEmpty()) + return null; + File jarFile = classpathJarFiles.get(jarName); + return Resource.newResource(jarFile.getPath()); + } + } + catch (MalformedURLException e) + { + throw e; + } + catch (IOException e) + { + Log.ignore(e); + } + } + return resource; + } + + @Override + public Set getResourcePaths(String path) + { + // Try to get regular resource paths + Set paths = super.getResourcePaths(path); + + // If no paths are returned check for virtual paths /WEB-INF/classes and /WEB-INF/lib + if (paths.isEmpty() && path != null) + { + path = URIUtil.canonicalPath(path); + if (path.startsWith(WEB_INF_LIB_PREFIX)) + { + paths = new TreeSet(); + for (String fileName : classpathJarFiles.keySet()) + { + // Return all jar files from class path + paths.add(WEB_INF_LIB_PREFIX + "/" + fileName); + } + } + else if (path.startsWith(WEB_INF_CLASSES_PREFIX)) + { + String classesPath = classesDir.getPath(); + String newPath = path.replace(WEB_INF_CLASSES_PREFIX, classesPath); + super.getResourcePaths(newPath); + } + } + return paths; + } + +} Index: src/main/java/org/mortbay/jetty/plugin/JettyRunMojo.java =================================================================== --- src/main/java/org/mortbay/jetty/plugin/JettyRunMojo.java (revision 6011) +++ src/main/java/org/mortbay/jetty/plugin/JettyRunMojo.java (revision ) @@ -529,7 +529,10 @@ classPathFiles.add(testClassesDirectory); if (getClassesDirectory() != null) + { + webAppConfig.setClassesDir(getClassesDirectory()); classPathFiles.add(getClassesDirectory()); + } //now add all of the dependencies classPathFiles.addAll(getDependencyFiles());