Author: olamy Date: Sat Oct 8 17:44:42 2011 New Revision: 1180425 URL: http://svn.apache.org/viewvc?rev=1180425&view=rev Log: [MTOMCAT-62] tomcat7 support: move webapp classLoader entries calculation to an external component.
Added: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculator.java (with props) tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java (with props) tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/TomcatRunException.java (with props) Modified: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java Modified: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml?rev=1180425&r1=1180424&r2=1180425&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml (original) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/pom.xml Sat Oct 8 17:44:42 2011 @@ -16,6 +16,10 @@ <artifactId>maven-plugin-api</artifactId> </dependency> <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + </dependency> + <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-component-annotations</artifactId> <version>1.5.5</version> Added: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculator.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculator.java?rev=1180425&view=auto ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculator.java (added) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculator.java Sat Oct 8 17:44:42 2011 @@ -0,0 +1,36 @@ +package org.apache.tomcat.maven.common.run; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; + +import java.util.List; +import java.util.Set; + +/** + * @author Olivier Lamy + * @since 2.0 + */ +public interface ClassLoaderEntriesCalculator +{ + List<String> calculateClassPathEntries( MavenProject mavenProject, Set<Artifact> dependencies, Log log ) + throws TomcatRunException; +} Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculator.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/ClassLoaderEntriesCalculator.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: 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=1180425&view=auto ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java (added) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java Sat Oct 8 17:44:42 2011 @@ -0,0 +1,113 @@ +package org.apache.tomcat.maven.common.run; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.util.StringUtils; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +/** + * @author Olivier Lamy + * @since 2.0 + */ +@Component( role = ClassLoaderEntriesCalculator.class ) +public class DefaultClassLoaderEntriesCalculator + implements ClassLoaderEntriesCalculator +{ + public List<String> calculateClassPathEntries(MavenProject project, Set<Artifact> dependencies, Log log) + throws TomcatRunException + { + List<String> classLoaderEntries = new ArrayList<String>(); + // add classes directories to loader + + try + { + @SuppressWarnings( "unchecked" ) List<String> classPathElements = project.getCompileClasspathElements(); + if ( classPathElements != null ) + { + for ( String classPathElement : classPathElements ) + { + File classPathElementFile = new File(classPathElement); + if ( classPathElementFile.exists() && classPathElementFile.isDirectory() ) + { + log.debug("adding classPathElementFile " + classPathElementFile.toURI().toString()); + classLoaderEntries.add(classPathElementFile.toURI().toString()); + } + } + } + } + catch ( DependencyResolutionRequiredException e ) + { + throw new TomcatRunException(e.getMessage(), e); + } + + // add artifacts to loader + if ( dependencies != null ) + { + for ( Artifact artifact : dependencies ) + { + String scope = artifact.getScope(); + + // skip provided and test scoped artifacts + if ( !Artifact.SCOPE_PROVIDED.equals(scope) && !Artifact.SCOPE_TEST.equals(scope) ) + { + log.debug( + "add dependency to webapploader " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + + artifact.getVersion() + ":" + artifact.getScope()); + if ( !isInProjectReferences(artifact, project) ) + { + classLoaderEntries.add(artifact.getFile().toURI().toString()); + } + else + { + log.debug("skip adding artifact " + artifact.getArtifactId() + " as it's in reactors"); + } + } + } + } + return classLoaderEntries; + } + + protected boolean isInProjectReferences(Artifact artifact, MavenProject project) + { + if ( project.getProjectReferences() == null || project.getProjectReferences().isEmpty() ) + { + return false; + } + @SuppressWarnings( "unchecked" ) Collection<MavenProject> mavenProjects = + project.getProjectReferences().values(); + for ( MavenProject mavenProject : mavenProjects ) + { + if ( StringUtils.equals(mavenProject.getId(), artifact.getId()) ) + { + return true; + } + } + return false; + } +} Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/DefaultClassLoaderEntriesCalculator.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Added: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/TomcatRunException.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/TomcatRunException.java?rev=1180425&view=auto ============================================================================== --- tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/TomcatRunException.java (added) +++ tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/TomcatRunException.java Sat Oct 8 17:44:42 2011 @@ -0,0 +1,32 @@ +package org.apache.tomcat.maven.common.run; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * @author Olivier Lamy + * @since 2.0 + */ +public class TomcatRunException + extends Exception +{ + public TomcatRunException( String s, Throwable throwable ) + { + super(s, throwable); + } +} Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/TomcatRunException.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: tomcat/maven-plugin/trunk/common-tomcat-maven-plugin/src/main/java/org/apache/tomcat/maven/common/run/TomcatRunException.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java URL: http://svn.apache.org/viewvc/tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java?rev=1180425&r1=1180424&r2=1180425&view=diff ============================================================================== --- tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java (original) +++ tomcat/maven-plugin/trunk/tomcat6-maven-plugin/src/main/java/org/apache/tomcat/maven/plugin/tomcat6/RunMojo.java Sat Oct 8 17:44:42 2011 @@ -23,11 +23,10 @@ import org.apache.catalina.Context; import org.apache.catalina.loader.WebappLoader; import org.apache.catalina.startup.Embedded; import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; +import org.apache.tomcat.maven.common.run.ClassLoaderEntriesCalculator; +import org.apache.tomcat.maven.common.run.TomcatRunException; import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3DomBuilder; import org.codehaus.plexus.util.xml.Xpp3DomWriter; @@ -38,7 +37,6 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; -import java.util.Collection; import java.util.List; import java.util.Set; @@ -100,6 +98,13 @@ public class RunMojo */ protected int backgroundProcessorDelay = -1; + /** + * @readonly + * @component + * @since 2.0 + */ + private ClassLoaderEntriesCalculator classLoaderEntriesCalculator; + private File temporaryContextFile = null; // ---------------------------------------------------------------------- @@ -112,12 +117,12 @@ public class RunMojo * @throws MojoExecutionException */ @Override - protected Context createContext( Embedded container ) + protected Context createContext(Embedded container) throws IOException, MojoExecutionException { - Context context = super.createContext( container ); + Context context = super.createContext(container); - context.setReloadable( isContextReloadable( ) ); + context.setReloadable(isContextReloadable()); return context; } @@ -128,92 +133,43 @@ public class RunMojo * @throws MojoExecutionException */ @Override - protected WebappLoader createWebappLoader( ) + protected WebappLoader createWebappLoader() throws IOException, MojoExecutionException { - WebappLoader loader = super.createWebappLoader( ); - //super.project. + WebappLoader loader = super.createWebappLoader(); if ( useSeparateTomcatClassLoader ) { - loader.setDelegate( delegate ); + loader.setDelegate(delegate); } - // add classes directories to loader - if ( classesDir != null ) + try { - try - { - @SuppressWarnings( "unchecked" ) List<String> classPathElements = - project.getCompileClasspathElements( ); - for ( String classPathElement : classPathElements ) - { - File classPathElementFile = new File( classPathElement ); - if ( classPathElementFile.exists( ) && classPathElementFile.isDirectory( ) ) - { - getLog( ).debug( "adding classPathElementFile " + classPathElementFile.toURI( ).toString( ) ); - loader.addRepository( classPathElementFile.toURI( ).toString( ) ); - } - } - } - catch ( DependencyResolutionRequiredException e ) - { - throw new MojoExecutionException( e.getMessage( ), e ); - } - //loader.addRepository( classesDir.toURI().toString() ); - } + List<String> classLoaderEntries = + classLoaderEntriesCalculator.calculateClassPathEntries(project, dependencies, getLog()); - // add artifacts to loader - if ( dependencies != null ) - { - for ( Artifact artifact : dependencies ) + if ( classLoaderEntries != null ) { - String scope = artifact.getScope( ); - - // skip provided and test scoped artifacts - if ( !Artifact.SCOPE_PROVIDED.equals( scope ) && !Artifact.SCOPE_TEST.equals( scope ) ) + for ( String classLoaderEntry : classLoaderEntries ) { - getLog( ).debug( - "add dependency to webapploader " + artifact.getGroupId( ) + ":" + artifact.getArtifactId( ) - + ":" + artifact.getVersion( ) + ":" + artifact.getScope( ) ); - if ( !isInProjectReferences( artifact ) ) - { - loader.addRepository( artifact.getFile( ).toURI( ).toString( ) ); - } - else - { - getLog( ).debug( "skip adding artifact " + artifact.getArtifactId( ) + " as it's in reactors" ); - } + loader.addRepository(classLoaderEntry); } } } + catch ( TomcatRunException e ) + { + throw new MojoExecutionException(e.getMessage(), e); + } return loader; } - protected boolean isInProjectReferences( Artifact artifact ) - { - if ( project.getProjectReferences( ) == null || project.getProjectReferences( ).isEmpty( ) ) - { - return false; - } - @SuppressWarnings( "unchecked" ) Collection<MavenProject> mavenProjects = - project.getProjectReferences( ).values( ); - for ( MavenProject mavenProject : mavenProjects ) - { - if ( StringUtils.equals( mavenProject.getId( ), artifact.getId( ) ) ) - { - return true; - } - } - return false; - } /** * {@inheritDoc} */ @Override - protected File getDocBase( ) + protected File getDocBase() { return warSourceDirectory; } @@ -222,7 +178,7 @@ public class RunMojo * {@inheritDoc} */ @Override - protected File getContextFile( ) + protected File getContextFile() throws MojoExecutionException { if ( temporaryContextFile != null ) @@ -236,34 +192,34 @@ public class RunMojo //---------------------------------------------------------------------------- FileReader fr = null; FileWriter fw = null; - StringWriter sw = new StringWriter( ); + StringWriter sw = new StringWriter(); try { - temporaryContextFile = File.createTempFile( "tomcat-maven-plugin", "temp-ctx-file" ); - fw = new FileWriter( temporaryContextFile ); + temporaryContextFile = File.createTempFile("tomcat-maven-plugin", "temp-ctx-file"); + fw = new FileWriter(temporaryContextFile); // format to modify/create <Context backgroundProcessorDelay="5" reloadable="false"> - if ( contextFile != null && contextFile.exists( ) ) + if ( contextFile != null && contextFile.exists() ) { - fr = new FileReader( contextFile ); - Xpp3Dom xpp3Dom = Xpp3DomBuilder.build( fr ); - xpp3Dom.setAttribute( "backgroundProcessorDelay", Integer.toString( backgroundProcessorDelay ) ); - xpp3Dom.setAttribute( "reloadable", Boolean.toString( isContextReloadable( ) ) ); - Xpp3DomWriter.write( fw, xpp3Dom ); - Xpp3DomWriter.write( sw, xpp3Dom ); - getLog( ).debug( " generated context file " + sw.toString( ) ); + fr = new FileReader(contextFile); + Xpp3Dom xpp3Dom = Xpp3DomBuilder.build(fr); + xpp3Dom.setAttribute("backgroundProcessorDelay", Integer.toString(backgroundProcessorDelay)); + xpp3Dom.setAttribute("reloadable", Boolean.toString(isContextReloadable())); + Xpp3DomWriter.write(fw, xpp3Dom); + Xpp3DomWriter.write(sw, xpp3Dom); + getLog().debug(" generated context file " + sw.toString()); } else { if ( contextReloadable ) { // don't care about using a complicated xml api to create one xml line :-) - StringBuilder sb = new StringBuilder( "<Context " ).append( "backgroundProcessorDelay=\"" ).append( - Integer.toString( backgroundProcessorDelay ) ).append( "\"" ).append( - " reloadable=\"" + Boolean.toString( isContextReloadable( ) ) + "\"/>" ); + StringBuilder sb = new StringBuilder("<Context ").append("backgroundProcessorDelay=\"").append( + Integer.toString(backgroundProcessorDelay)).append("\"").append( + " reloadable=\"" + Boolean.toString(isContextReloadable()) + "\"/>"); - getLog( ).debug( " generated context file " + sb.toString( ) ); + getLog().debug(" generated context file " + sb.toString()); - fw.write( sb.toString( ) ); + fw.write(sb.toString()); } else { @@ -274,19 +230,19 @@ public class RunMojo } catch ( IOException e ) { - getLog( ).error( "error creating fake context.xml : " + e.getMessage( ), e ); - throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage( ), e ); + getLog().error("error creating fake context.xml : " + e.getMessage(), e); + throw new MojoExecutionException("error creating fake context.xml : " + e.getMessage(), e); } catch ( XmlPullParserException e ) { - getLog( ).error( "error creating fake context.xml : " + e.getMessage( ), e ); - throw new MojoExecutionException( "error creating fake context.xml : " + e.getMessage( ), e ); + getLog().error("error creating fake context.xml : " + e.getMessage(), e); + throw new MojoExecutionException("error creating fake context.xml : " + e.getMessage(), e); } finally { - IOUtil.close( fw ); - IOUtil.close( fr ); - IOUtil.close( sw ); + IOUtil.close(fw); + IOUtil.close(fr); + IOUtil.close(sw); } return temporaryContextFile; --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org