From ba74af34c9232c8e6e4bccb9c2b8f56f800adc38 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Thu, 19 Sep 2019 16:30:19 +1000 Subject: [PATCH] Issue #1743 WIP tests Signed-off-by: Jan Bartel --- ...sLoadingTestingServletContextListener.java | 28 ++++++++++- .../maven/plugin/AbstractWebAppMojo.java | 50 ++++++++++++++++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/jetty-maven-plugin/src/it/jetty-maven-plugin-provided-module-dep/web/src/main/java/test/ClassLoadingTestingServletContextListener.java b/jetty-maven-plugin/src/it/jetty-maven-plugin-provided-module-dep/web/src/main/java/test/ClassLoadingTestingServletContextListener.java index e96fce2432d..04db0b61bea 100755 --- a/jetty-maven-plugin/src/it/jetty-maven-plugin-provided-module-dep/web/src/main/java/test/ClassLoadingTestingServletContextListener.java +++ b/jetty-maven-plugin/src/it/jetty-maven-plugin-provided-module-dep/web/src/main/java/test/ClassLoadingTestingServletContextListener.java @@ -18,6 +18,9 @@ package test; +import java.net.URL; +import java.net.URLClassLoader; + import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @@ -33,7 +36,19 @@ public class ClassLoadingTestingServletContextListener try { Api api = new Api(); - //System.out.println("Class " + api.getClass().getName() + " is available and loaded by classloader " + api.getClass().getClassLoader().toString() + ". Expected ClassNotFoundException."); + System.err.println("Class " + api.getClass().getName() + " is available and loaded by classloader " + api.getClass().getClassLoader().toString() + ". Expected CNFE."); + ClassLoader cl = api.getClass().getClassLoader(); + while (cl != null) + { + if (cl instanceof URLClassLoader) + { + URLClassLoader ucl = (URLClassLoader)cl; + System.err.println("-----"); + printURLs(ucl); + System.err.println("-----"); + } + cl = cl.getParent(); + } } catch (java.lang.Exception exception) { @@ -45,4 +60,15 @@ public class ClassLoadingTestingServletContextListener public void contextDestroyed(ServletContextEvent sce) { } + + private void printURLs (URLClassLoader l) + { + if (l == null) + return; + + for (URL u: l.getURLs()) + { + System.err.println(u); + } + } } diff --git a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractWebAppMojo.java b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractWebAppMojo.java index 4d868a114c8..cd7f88a77f3 100644 --- a/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractWebAppMojo.java +++ b/jetty-maven-plugin/src/main/java/org/eclipse/jetty/maven/plugin/AbstractWebAppMojo.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -40,6 +41,7 @@ import java.util.Map; import java.util.Properties; import java.util.Random; import java.util.Set; +import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; @@ -58,6 +60,7 @@ import org.apache.maven.shared.artifact.DefaultArtifactCoordinate; import org.apache.maven.shared.artifact.resolve.ArtifactResolver; import org.apache.maven.shared.artifact.resolve.ArtifactResolverException; import org.codehaus.plexus.util.StringUtils; +import org.eclipse.jetty.maven.plugin.utils.MavenProjectHelper; import org.eclipse.jetty.security.LoginService; import org.eclipse.jetty.server.RequestLog; import org.eclipse.jetty.server.Server; @@ -675,7 +678,7 @@ public abstract class AbstractWebAppMojo extends AbstractMojo continue; //only add dependencies of scope=test if explicitly required dependencyFiles.add(artifact.getFile()); - getLog().debug( "Adding artifact " + artifact.getFile().getName() + " with scope "+artifact.getScope()+" for WEB-INF/lib " ); + getLog().info( "Adding artifact " + artifact.getFile().getName() + " with scope "+artifact.getScope()+" for WEB-INF/lib " ); } return dependencyFiles; @@ -1160,7 +1163,16 @@ public abstract class AbstractWebAppMojo extends AbstractMojo if (useTestScope && (testClassesDirectory != null)) webApp.setTestClasses (testClassesDirectory); - webApp.setWebInfLib(getProjectDependencyFiles()); + MavenProjectHelper mavenProjectHelper = new MavenProjectHelper(project); + List webInfLibs = getWebInfLibArtifacts().stream() + .map(a -> + { + Path p = mavenProjectHelper.getArtifactPath(a); + getLog().debug("Artifact " + a.getId() + " loaded from " + p + " added to WEB-INF/lib"); + return p.toFile(); + }).collect(Collectors.toList()); + + webApp.setWebInfLib(webInfLibs); //if we have not already set web.xml location, need to set one up if (webApp.getDescriptor() == null) @@ -1243,4 +1255,38 @@ public abstract class AbstractWebAppMojo extends AbstractMojo outputFile.createNewFile(); return outputFile; } + + /** + * Find which dependencies are suitable for addition to the virtual + * WEB-INF lib. + * + * @param mavenProject this project + */ + private Collection getWebInfLibArtifacts() + { + String type = project.getArtifact().getType(); + if (!"war".equalsIgnoreCase(type) && !"zip".equalsIgnoreCase(type)) + return Collections.emptyList(); + + return project.getArtifacts().stream() + .filter(this::isArtifactOKForWebInfLib) + .collect(Collectors.toList()); + } + + private boolean isArtifactOKForWebInfLib(Artifact artifact) + { + //The dependency cannot be of type war + if ("war".equalsIgnoreCase(artifact.getType())) + return false; + + //The dependency cannot be scope provided (those should be added to the plugin classpath) + if (Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) + return false; + + //Test dependencies not added by default + if (Artifact.SCOPE_TEST.equals(artifact.getScope()) && !useTestScope) + return false; + + return true; + } }