Author: olamy Date: Tue Aug 16 01:04:58 2016 New Revision: 1756450 URL: http://svn.apache.org/viewvc?rev=1756450&view=rev Log: get classloader resources from reactor modules as well
Modified: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculatorResult.java tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java tomcat/maven-plugin/trunk/pom.xml tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/AbstractRunMojo.java tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/RunMojo.java Modified: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculatorResult.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculatorResult.java?rev=1756450&r1=1756449&r2=1756450&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculatorResult.java (original) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculatorResult.java Tue Aug 16 01:04:58 2016 @@ -37,10 +37,23 @@ public class ClassLoaderEntriesCalculato */ private List<File> tmpDirectories; - public ClassLoaderEntriesCalculatorResult( List<String> classPathEntries, List<File> tmpDirectories ) + + /** + * directory part of webapp classpath (project.build.directory and reactor projects) + */ + private List<String> buildDirectories; + + /** + * @param classPathEntries + * @param tmpDirectories + * @param buildDirectories + */ + public ClassLoaderEntriesCalculatorResult( List<String> classPathEntries, List<File> tmpDirectories, + List<String> buildDirectories ) { this.classPathEntries = classPathEntries; this.tmpDirectories = tmpDirectories; + this.buildDirectories = buildDirectories; } public List<String> getClassPathEntries() @@ -62,4 +75,9 @@ public class ClassLoaderEntriesCalculato { this.tmpDirectories = tmpDirectories; } + + public List<String> getBuildDirectories() + { + return buildDirectories; + } } Modified: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java?rev=1756450&r1=1756449&r2=1756450&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java (original) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java Tue Aug 16 01:04:58 2016 @@ -45,7 +45,7 @@ import java.util.Set; * @author Olivier Lamy * @since 2.0 */ -@Component (role = ClassLoaderEntriesCalculator.class) +@Component( role = ClassLoaderEntriesCalculator.class ) public class DefaultClassLoaderEntriesCalculator implements ClassLoaderEntriesCalculator { @@ -63,10 +63,12 @@ public class DefaultClassLoaderEntriesCa List<File> tmpDirectories = new ArrayList<File>(); + List<String> buildDirectories = new ArrayList<String>(); + // add classes directories to loader try { - @SuppressWarnings ("unchecked") List<String> classPathElements = request.isUseTestClassPath() + @SuppressWarnings( "unchecked" ) List<String> classPathElements = request.isUseTestClassPath() ? request.getMavenProject().getTestClasspathElements() : request.getMavenProject().getRuntimeClasspathElements(); if ( classPathElements != null ) @@ -79,6 +81,7 @@ public class DefaultClassLoaderEntriesCa request.getLog().debug( "adding classPathElementFile " + classPathElementFile.toURI().toString() ); classLoaderEntries.add( classPathElementFile.toURI().toString() ); + buildDirectories.add( classPathElement ); } } } @@ -101,15 +104,15 @@ public class DefaultClassLoaderEntriesCa String scope = artifact.getScope(); // skip provided and test scoped artifacts - if ( !Artifact.SCOPE_PROVIDED.equals( scope ) && ( !Artifact.SCOPE_TEST.equals( scope ) - || request.isUseTestClassPath() ) ) + if ( !Artifact.SCOPE_PROVIDED.equals( scope ) // + && ( !Artifact.SCOPE_TEST.equals( scope ) || request.isUseTestClassPath() ) ) { request.getLog().debug( "add dependency to webapploader " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getVersion() + ":" + artifact.getScope() ); // we add artifact dependencies and projects from reactor if file (ie jar) as users can go to install/package phase // so artifact.getFile is a file not a directory and not added when iterate on project.classPathElements - if ( !isInProjectReferences( artifact, request.getMavenProject() ) || artifact.getFile().isFile() ) + if ( !isInProjectReferences( artifact, request.getMavenProject() ) || artifact.getFile().isFile() ) { String fileName = artifact.getGroupId() + "-" + artifact.getFile().getName(); if ( !fileInClassLoaderEntries.contains( fileName ) ) @@ -134,16 +137,16 @@ public class DefaultClassLoaderEntriesCa boolean existed = !tmpDir.mkdirs(); // does a directory for this artifact already exist? - if (existed) + if ( existed ) { // check timestamp to see if artifact is newer than extracted directory long dirLastMod = tmpDir.lastModified(); long warLastMod = artifact.getFile().lastModified(); - if (warLastMod == 0L || warLastMod > dirLastMod) + if ( warLastMod == 0L || warLastMod > dirLastMod ) { request.getLog().debug( - "re-exploding artifact " + artifact.getArtifactId() + " due to newer WAR"); + "re-exploding artifact " + artifact.getArtifactId() + " due to newer WAR" ); deleteDirectory( tmpDir, request.getLog() ); tmpDir = new File( tmpExtractDatas, artifact.getArtifactId() ); @@ -153,7 +156,7 @@ public class DefaultClassLoaderEntriesCa else { request.getLog().debug( - "using existing exploded war for artifact " + artifact.getArtifactId()); + "using existing exploded war for artifact " + artifact.getArtifactId() ); } } @@ -162,7 +165,7 @@ public class DefaultClassLoaderEntriesCa try { // explode the archive if it is not already exploded - if (!existed) + if ( !existed ) { File warFile = artifact.getFile(); UnArchiver unArchiver = archiverManager.getUnArchiver( "jar" ); @@ -216,7 +219,9 @@ public class DefaultClassLoaderEntriesCa } } - return new ClassLoaderEntriesCalculatorResult( new ArrayList<String>( classLoaderEntries ), tmpDirectories ); + return new ClassLoaderEntriesCalculatorResult( new ArrayList<String>( classLoaderEntries ), // + tmpDirectories, // + buildDirectories ); } @@ -240,7 +245,7 @@ public class DefaultClassLoaderEntriesCa { return false; } - @SuppressWarnings ("unchecked") Collection<MavenProject> mavenProjects = + @SuppressWarnings( "unchecked" ) Collection<MavenProject> mavenProjects = project.getProjectReferences().values(); for ( MavenProject mavenProject : mavenProjects ) { Modified: tomcat/maven-plugin/trunk/pom.xml URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/pom.xml?rev=1756450&r1=1756449&r2=1756450&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/pom.xml (original) +++ tomcat/maven-plugin/trunk/pom.xml Tue Aug 16 01:04:58 2016 @@ -74,7 +74,7 @@ <!-- server port for it tests --> <its.server.port>2008</its.server.port> <tomcat7Version>7.0.59</tomcat7Version> - <tomcat8Version>8.0.11</tomcat8Version> + <tomcat8Version>8.0.12</tomcat8Version> <!-- to prevent isssues with last apache parent pom --> <arguments /> Modified: tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/AbstractRunMojo.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/AbstractRunMojo.java?rev=1756450&r1=1756449&r2=1756450&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/AbstractRunMojo.java (original) +++ tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/AbstractRunMojo.java Tue Aug 16 01:04:58 2016 @@ -49,6 +49,7 @@ import org.apache.maven.artifact.version import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.project.MavenProject; @@ -693,7 +694,9 @@ public abstract class AbstractRunMojo Context context = container.addWebapp( contextPath, baseDir ); context.setResources( - new MyDirContext( new File( project.getBuild().getOutputDirectory() ).getAbsolutePath(), getPath() ) ); + new MyDirContext( new File( project.getBuild().getOutputDirectory() ).getAbsolutePath(), // + getPath(), // + getLog() ) ); if ( useSeparateTomcatClassLoader ) { @@ -793,16 +796,23 @@ public abstract class AbstractRunMojo WebResourceSet webResourceSet; - MyDirContext( String buildOutputDirectory, String webAppPath ) + Log log; + + MyDirContext( String buildOutputDirectory, String webAppPath, Log log ) { + this.buildOutputDirectory = buildOutputDirectory; this.webAppPath = webAppPath; + this.log = log; } + + @Override public WebResource getResource( String path ) { + log.debug( "MyDirContext#getResource: " + path ); if ( "/WEB-INF/classes".equals( path ) ) { return new FileResource( this, this.webAppPath, new File( this.buildOutputDirectory ), true ); @@ -821,7 +831,8 @@ public abstract class AbstractRunMojo @Override public WebResource getClassLoaderResource( String path ) { - // here get resources from various pathsss + log.debug( "MyDirContext#getClassLoaderResource: " + path ); + // here get resources from various paths return super.getClassLoaderResource( path ); } @@ -829,8 +840,30 @@ public abstract class AbstractRunMojo @Override public WebResource[] listResources( String path ) { + log.debug( "MyDirContext#listResources: " + path ); return super.listResources( path ); } + + @Override + public WebResource[] getClassLoaderResources( String path ) + { + log.debug( "MyDirContext#getClassLoaderResources: " + path ); + return super.getClassLoaderResources( path ); + } + + @Override + public WebResource[] getResources( String path ) + { + log.debug( "MyDirContext#getResources: " + path ); + return super.getResources( path ); + } + + @Override + protected WebResource[] getResourcesInternal( String path, boolean useClassLoaderResources ) + { + log.debug( "MyDirContext#getResourcesInternal: " + path ); + return super.getResourcesInternal( path, useClassLoaderResources ); + } } /** Modified: tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/RunMojo.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/RunMojo.java?rev=1756450&r1=1756449&r2=1756450&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/RunMojo.java (original) +++ tomcat/maven-plugin/trunk/tomcat8-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat8/run/RunMojo.java Tue Aug 16 01:04:58 2016 @@ -63,6 +63,7 @@ import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Collections; +import java.util.Enumeration; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -315,7 +316,7 @@ public class RunMojo .setMavenProject( project ) // .setAddWarDependenciesInClassloader( addWarDependenciesInClassloader ) // .setUseTestClassPath( useTestClasspath ); - ClassLoaderEntriesCalculatorResult classLoaderEntriesCalculatorResult = + final ClassLoaderEntriesCalculatorResult classLoaderEntriesCalculatorResult = classLoaderEntriesCalculator.calculateClassPathEntries( request ); final List<String> classLoaderEntries = classLoaderEntriesCalculatorResult.getClassPathEntries(); final List<File> tmpDirectories = classLoaderEntriesCalculatorResult.getTmpDirectories(); @@ -336,16 +337,21 @@ public class RunMojo } } + getLog().debug( "classLoaderEntriesCalculator urls: " + urls ); + final URLClassLoader urlClassLoader = new URLClassLoader( urls.toArray( new URL[urls.size()] ) ); final ClassRealm pluginRealm = getTomcatClassLoader(); context.setResources( - new MyDirContext( new File( project.getBuild().getOutputDirectory() ).getAbsolutePath(), getPath() ) + new MyDirContext( new File( project.getBuild().getOutputDirectory() ).getAbsolutePath(), // + getPath(), // + getLog() ) { @Override public WebResource getClassLoaderResource( String path ) { + log.debug( "RunMojo#getClassLoaderResource: " + path ); URL url = urlClassLoader.getResource( StringUtils.removeStart( path, "/" ) ); // search in parent (plugin) classloader if ( url == null ) @@ -358,6 +364,89 @@ public class RunMojo return new EmptyResource( this, getPath() ); } + return urlToWebResource( url, path ); + } + + @Override + public WebResource getResource( String path ) + { + log.debug( "RunMojo#getResource: " + path ); + return super.getResource( path ); + } + + @Override + public WebResource[] getResources( String path ) + { + log.debug( "RunMojo#getResources: " + path ); + return super.getResources( path ); + } + + @Override + protected WebResource[] getResourcesInternal( String path, boolean useClassLoaderResources ) + { + log.debug( "RunMojo#getResourcesInternal: " + path ); + return super.getResourcesInternal( path, useClassLoaderResources ); + } + + @Override + public WebResource[] getClassLoaderResources( String path ) + { + try + { + Enumeration<URL> enumeration = + urlClassLoader.findResources( StringUtils.removeStart( path, "/" ) ); + List<URL> urlsFound = new ArrayList<URL>(); + List<WebResource> webResources = new ArrayList<WebResource>(); + while ( enumeration.hasMoreElements() ) + { + URL url = enumeration.nextElement(); + urlsFound.add( url ); + webResources.add( urlToWebResource( url, path ) ); + } + log.debug( + "RunMojo#getClassLoaderResources: " + path + " found : " + urlsFound.toString() ); + + webResources.addAll( findResourcesInDirectories( path, + classLoaderEntriesCalculatorResult.getBuildDirectories() ) ); + + return webResources.toArray( new WebResource[webResources.size()] ); + + } + catch ( IOException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + } + + + private List<WebResource> findResourcesInDirectories( String path, List<String> directories ) + { + try + { + List<WebResource> webResources = new ArrayList<WebResource>(); + + for ( String directory : directories ) + { + + File file = new File( directory, path ); + if ( file.exists() ) + { + webResources.add( urlToWebResource( file.toURI().toURL(), path ) ); + } + + } + + return webResources; + } + catch ( MalformedURLException e ) + { + throw new RuntimeException( e.getMessage(), e ); + } + } + + + private WebResource urlToWebResource( URL url, String path ) + { JarFile jarFile = null; try @@ -367,19 +456,27 @@ public class RunMojo int idx = url.getFile().indexOf( '!' ); - String filePath = StringUtils.removeStart( url.getFile().substring( 0, idx ), "file:" ); - - jarFile = new JarFile( filePath ); + if ( idx >= 0 ) + { + String filePath = StringUtils.removeStart( url.getFile().substring( 0, idx ), "file:" ); + + jarFile = new JarFile( filePath ); + + JarEntry jarEntry = jarFile.getJarEntry( StringUtils.removeStart( path, "/" ) ); + + return new JarResource( this, // + getPath(), // + filePath, // + url.getPath().substring( 0, idx ), // + jarEntry, // + "", // + null ); + } + else + { + return new FileResource( this, webAppPath, new File( url.getFile() ), true ); + } - JarEntry jarEntry = jarFile.getJarEntry( StringUtils.removeStart( path, "/" ) ); - - return new JarResource( this, // - getPath(), // - filePath, // - url.getPath().substring( 0, idx ), // - jarEntry, // - "", // - null ); } catch ( IOException e ) { @@ -389,9 +486,9 @@ public class RunMojo { IOUtils.closeQuietly( jarFile ); } + } - } } ); Runtime.getRuntime().addShutdownHook( new Thread() --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org