Return-Path: Delivered-To: apmail-maven-commits-archive@www.apache.org Received: (qmail 42826 invoked from network); 4 Jul 2007 21:00:15 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Jul 2007 21:00:15 -0000 Received: (qmail 65614 invoked by uid 500); 4 Jul 2007 21:00:16 -0000 Delivered-To: apmail-maven-commits-archive@maven.apache.org Received: (qmail 65545 invoked by uid 500); 4 Jul 2007 21:00:16 -0000 Mailing-List: contact commits-help@maven.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@maven.apache.org Delivered-To: mailing list commits@maven.apache.org Received: (qmail 65531 invoked by uid 99); 4 Jul 2007 21:00:16 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 04 Jul 2007 14:00:16 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 04 Jul 2007 14:00:11 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 664C31A981A; Wed, 4 Jul 2007 13:59:51 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r553329 - in /maven/plugins/branches/MWAR-97-2: ./ src/main/java/org/apache/maven/plugin/war/ src/main/java/org/apache/maven/plugin/war/packaging/ src/main/java/org/apache/maven/plugin/war/util/ src/test/java/org/apache/maven/plugin/war/ sr... Date: Wed, 04 Jul 2007 20:59:51 -0000 To: commits@maven.apache.org From: snicoll@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070704205951.664C31A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: snicoll Date: Wed Jul 4 13:59:49 2007 New Revision: 553329 URL: http://svn.apache.org/viewvc?view=rev&rev=553329 Log: War overlays refactoring. Introduced packaging tasks with an implementation for artifacts, overlay and main project. Added a protectedFiles shared list that specify if a file has already been written during packaging. If so, further requests for that file are denied Added: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/MappingUtils.java - copied, changed from r543832, maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/MappingUtils.java maven/plugins/branches/MWAR-97-2/src/test/resources/unit/waroverlays/ maven/plugins/branches/MWAR-97-2/src/test/resources/unit/waroverlays/default.xml Removed: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/MappingUtils.java Modified: maven/plugins/branches/MWAR-97-2/pom.xml maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/PropertyUtils.java maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/OverlayPackagingTask.java maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/MappingUtilsTest.java Modified: maven/plugins/branches/MWAR-97-2/pom.xml URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/pom.xml?view=diff&rev=553329&r1=553328&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/pom.xml (original) +++ maven/plugins/branches/MWAR-97-2/pom.xml Wed Jul 4 13:59:49 2007 @@ -17,7 +17,9 @@ under the License. --> - + maven-plugins org.apache.maven.plugins @@ -27,7 +29,7 @@ maven-war-plugin maven-plugin Maven War Plugin - 2.1-SNAPSHOT + 2.1-MWAR-97-SNAPSHOT 2.0.1 Modified: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java?view=diff&rev=553329&r1=553328&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java (original) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java Wed Jul 4 13:59:49 2007 @@ -27,6 +27,7 @@ import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.war.util.MappingUtils; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.UnArchiver; @@ -171,10 +172,10 @@ private static final String[] DEFAULT_INCLUDES = {"**/**"}; - private static final String DEFAULT_FILE_NAME_MAPPING_CLASSIFIER = + public static final String DEFAULT_FILE_NAME_MAPPING_CLASSIFIER = "${artifactId}-${version}-${classifier}.${extension}"; - private static final String DEFAULT_FILE_NAME_MAPPING = "${artifactId}-${version}.${extension}"; + public static final String DEFAULT_FILE_NAME_MAPPING = "${artifactId}-${version}.${extension}"; /** * The comma separated list of tokens to include in the WAR. @@ -306,8 +307,9 @@ this.overlays = overlays; } - public void addOverlay(Overlay overlay) { - overlays.add(overlay); + public void addOverlay( Overlay overlay ) + { + overlays.add( overlay ); } /** @@ -896,13 +898,13 @@ * @throws java.io.IOException if source does not exist, the file in * destinationDirectory cannot be written to, or an IO error occurs during copying. *

- * TO DO: Remove this method when Maven moves to plexus-utils version 1.4 + * TO DO: Remove this method when Maven moves to plexus-util version 1.4 */ private static void copyFileToDirectoryIfModified( File source, File destinationDirectory ) throws IOException { // TO DO: Remove this method and use the method in WarFileUtils when Maven 2 changes - // to plexus-utils 1.2. + // to plexus-util 1.2. if ( destinationDirectory.exists() && !destinationDirectory.isDirectory() ) { throw new IllegalArgumentException( "Destination is not a directory" ); @@ -938,7 +940,7 @@ * @param encoding * @param wrappers * @param filterProperties - * @throws IOException TO DO: Remove this method when Maven moves to plexus-utils version 1.4 + * @throws IOException TO DO: Remove this method when Maven moves to plexus-util version 1.4 */ private static void copyFilteredFile( File from, File to, String encoding, FilterWrapper[] wrappers, Map filterProperties ) @@ -996,13 +998,13 @@ * written to, or an IO error occurs during copying. * @throws java.io.FileNotFoundException if destination is a directory *

- * TO DO: Remove this method when Maven moves to plexus-utils version 1.4 + * TO DO: Remove this method when Maven moves to plexus-util version 1.4 */ private static void copyFileIfModified( File source, File destination ) throws IOException { // TO DO: Remove this method and use the method in WarFileUtils when Maven 2 changes - // to plexus-utils 1.2. + // to plexus-util 1.2. if ( destination.lastModified() < source.lastModified() ) { FileUtils.copyFile( source.getCanonicalFile(), destination ); @@ -1022,7 +1024,7 @@ * * @param sourceDirectory * @param destinationDirectory - * @throws IOException TO DO: Remove this method when Maven moves to plexus-utils version 1.4 + * @throws IOException TO DO: Remove this method when Maven moves to plexus-util version 1.4 */ private static void copyDirectoryStructureIfModified( File sourceDirectory, File destinationDirectory ) throws IOException @@ -1070,7 +1072,7 @@ } /** - * TO DO: Remove this interface when Maven moves to plexus-utils version 1.4 + * TO DO: Remove this interface when Maven moves to plexus-util version 1.4 */ private interface FilterWrapper { Modified: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/PropertyUtils.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/PropertyUtils.java?view=diff&rev=553329&r1=553328&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/PropertyUtils.java (original) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/PropertyUtils.java Wed Jul 4 13:59:49 2007 @@ -31,7 +31,7 @@ /** * @author Kenney Westerhof * @version $Id$ - * @todo this is duplicated from the resources plugin - migrate to plexus-utils + * @todo this is duplicated from the resources plugin - migrate to plexus-util */ public final class PropertyUtils { Modified: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java?view=diff&rev=553329&r1=553328&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java (original) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/AbstractWarPackagingTask.java Wed Jul 4 13:59:49 2007 @@ -19,16 +19,28 @@ * under the License. */ +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.war.AbstractWarMojo; +import org.apache.maven.plugin.war.util.MappingUtils; import org.apache.maven.plugin.war.util.PathSet; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.archiver.UnArchiver; import org.codehaus.plexus.archiver.manager.NoSuchArchiverException; +import org.codehaus.plexus.util.DirectoryScanner; import org.codehaus.plexus.util.FileUtils; +import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.InterpolationFilterReader; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; +import java.io.Reader; +import java.io.Writer; import java.util.Iterator; +import java.util.Map; /** * @author Stephane Nicoll @@ -36,7 +48,7 @@ public abstract class AbstractWarPackagingTask implements WarPackagingTask { - + public static final String[] DEFAULT_INCLUDES = {"**/**"}; /** * Copies the files if possible. @@ -55,22 +67,101 @@ { for ( Iterator iter = sourceFilesSet.iterator(); iter.hasNext(); ) { - String fileToCopyName = (String) iter.next(); - if ( !context.getProtectedFiles().contains( fileToCopyName ) ) + final String fileToCopyName = (String) iter.next(); + final File sourceFile = new File( sourceBaseDir, fileToCopyName ); + copyFile( context, sourceFile, fileToCopyName ); + } + } + + /** + * Copy the specified file if the target location has not yet already been used. + *

+ * The targetFileName is the relative path according to the root of + * the generated web application. + * + * @param context the context to use + * @param file the file to copy + * @param targetFilename the relative path according to the root of the webapp + * @return true if the file has been copied, false otherwise + * @throws IOException if an error occured while copying + */ + protected boolean copyFile( WarPackagingContext context, File file, String targetFilename ) + throws IOException + { + if ( !context.getProtectedFiles().contains( targetFilename ) ) + { + File targetFile = new File( context.getWebAppDirectory(), targetFilename ); + copyFile( file, targetFile ); + + // Add the file to the protected list + context.getProtectedFiles().add( targetFilename ); + context.getLogger().debug( " + " + targetFilename + " has been copied." ); + return true; + } + else + { + context.getLogger().debug( + " - " + targetFilename + " wasn't copied because it has already been packaged." ); + return false; + } + } + + /** + * Copy the specified file if the target location has not yet already been + * used and filter its content with the configureed filter properties. + *

+ * The targetFileName is the relative path according to the root of + * the generated web application. + * + * @param context the context to use + * @param file the file to copy + * @param targetFilename the relative path according to the root of the webapp + * @return true if the file has been copied, false otherwise + * @throws IOException if an error occured while copying + * @throws MojoExecutionException if an error occured while retrieving the filter properties + */ + protected boolean copyFilteredFile( WarPackagingContext context, File file, String targetFilename ) + throws IOException, MojoExecutionException + { + + if ( !context.getProtectedFiles().contains( targetFilename ) ) + { + final File targetFile = new File( context.getWebAppDirectory(), targetFilename ); + // buffer so it isn't reading a byte at a time! + Reader fileReader = null; + Writer fileWriter = null; + try { - File sourceFile = new File( sourceBaseDir, fileToCopyName ); - File targetFile = new File( context.getWebAppDirectory(), fileToCopyName ); - copyFile( sourceFile, targetFile ); - - // Add the file to the protected list - context.getProtectedFiles().add( fileToCopyName ); - context.getLogger().debug( " + " + fileToCopyName + " has been copied." ); + // fix for MWAR-36, ensures that the parent dir are created first + targetFile.getParentFile().mkdirs(); + + fileReader = new BufferedReader( new FileReader( file ) ); + fileWriter = new FileWriter( targetFile ); + + Reader reader = fileReader; + for ( int i = 0; i < getFilterWrappers().length; i++ ) + { + FilterWrapper wrapper = getFilterWrappers()[i]; + reader = wrapper.getReader( reader, context.getFilterProperties() ); + } + + IOUtil.copy( reader, fileWriter ); } - else + finally { - context.getLogger().debug( - " - " + fileToCopyName + " wasn't copied because it has already been packaged." ); + IOUtil.close( fileReader ); + IOUtil.close( fileWriter ); } + // Add the file to the protected list + context.getProtectedFiles().add( targetFilename ); + context.getLogger().debug( " + " + targetFilename + " has been copied." ); + return true; + } + else + { + context.getLogger().debug( + " - " + targetFilename + " wasn't copied because it has already been packaged." ); + return false; } } @@ -118,7 +209,7 @@ * will be created if they don't already exist. destination will be * overwritten if it already exists. *

- * TODO: Remove this method when Maven moves to plexus-utils version 1.4 + * TODO: Remove this method when Maven moves to plexus-util version 1.4 * * @param source an existing non-directory File to copy bytes from * @param destination a non-directory File to write bytes to (possibly overwriting). @@ -126,12 +217,106 @@ * be written to, or an IO error occurs during copying * @throws java.io.FileNotFoundException if destination is a directory */ - protected static void copyFile( File source, File destination ) + private void copyFile( File source, File destination ) throws IOException { FileUtils.copyFile( source.getCanonicalFile(), destination ); // preserve timestamp destination.setLastModified( source.lastModified() ); + } + + /** + * Returns the file to copy. If the includes are null or empty, the + * default includes are used. + * + * @param baseDir the base directory to start from + * @param includes the includes + * @param excludes the excludes + * @return the files to copy + */ + protected PathSet getFilesToIncludes( File baseDir, String[] includes, String[] excludes ) + { + final DirectoryScanner scanner = new DirectoryScanner(); + scanner.setBasedir( baseDir ); + + if ( excludes != null ) + { + scanner.setExcludes( excludes ); + } + scanner.addDefaultExcludes(); + + if ( includes != null && includes.length > 0 ) + { + scanner.setIncludes( includes ); + } + else + { + scanner.setIncludes( DEFAULT_INCLUDES ); + } + + scanner.scan(); + + return new PathSet( scanner.getIncludedFiles() ); + + } + + /** + * Returns the final name of the specified artifact. + *

+ * If the outputFileNameMapping is set, it is used, otherwise + * the standard naming scheme is used. + * + * @param context the packaging context + * @param artifact the artifact + * @return the converted filename of the artifact + */ + protected String getArtifactFinalName( WarPackagingContext context, Artifact artifact ) + { + if ( context.getOutputFileNameMapping() != null ) + { + return MappingUtils.evaluateFileNameMapping( context.getOutputFileNameMapping(), artifact ); + } + + String classifier = artifact.getClassifier(); + if ( ( classifier != null ) && !( "".equals( classifier.trim() ) ) ) + { + return MappingUtils.evaluateFileNameMapping( AbstractWarMojo.DEFAULT_FILE_NAME_MAPPING_CLASSIFIER, + artifact ); + } + else + { + return MappingUtils.evaluateFileNameMapping( AbstractWarMojo.DEFAULT_FILE_NAME_MAPPING, artifact ); + } + + } + + private FilterWrapper[] getFilterWrappers() + { + return new FilterWrapper[]{ + // support ${token} + new FilterWrapper() + { + public Reader getReader( Reader fileReader, Map filterProperties ) + { + return new InterpolationFilterReader( fileReader, filterProperties, "${", "}" ); + } + }, + // support @token@ + new FilterWrapper() + { + public Reader getReader( Reader fileReader, Map filterProperties ) + { + return new InterpolationFilterReader( fileReader, filterProperties, "@", "@" ); + } + }}; + } + + /** + * TO DO: Remove this interface when Maven moves to plexus-util version 1.4 + */ + private interface FilterWrapper + { + Reader getReader( Reader fileReader, Map filterProperties ); } } Added: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java?view=auto&rev=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java (added) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/ArtifactsPackagingTask.java Wed Jul 4 13:59:49 2007 @@ -0,0 +1,122 @@ +package org.apache.maven.plugin.war.packaging; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; +import org.apache.maven.plugin.MojoExecutionException; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +/** + * Handles the artifacts that needs to be packaged in the web application. + * + * @author Stephane Nicoll + */ +public class ArtifactsPackagingTask + extends AbstractWarPackagingTask +{ + + public static final String LIB_PATH = "WEB-INF/lib"; + + public static final String TLD_PATH = "WEB-INF/tld"; + + public static final String SERVICES_PATH = "WEB-INF/services"; + + private final Set artifacts; + + + public ArtifactsPackagingTask( Set artifacts ) + { + this.artifacts = artifacts; + } + + + public void performPackaging( WarPackagingContext context ) + throws MojoExecutionException + { + + final ScopeArtifactFilter filter = new ScopeArtifactFilter( Artifact.SCOPE_RUNTIME ); + final List duplicates = findDuplicates( context, artifacts ); + + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + String targetFileName = getArtifactFinalName( context, artifact ); + + context.getLogger().debug( "Processing: " + targetFileName ); + + if ( duplicates.contains( targetFileName ) ) + { + context.getLogger().debug( "Duplicate found: " + targetFileName ); + targetFileName = artifact.getGroupId() + "-" + targetFileName; + context.getLogger().debug( "Renamed to: " + targetFileName ); + } + + if ( !artifact.isOptional() && filter.include( artifact ) ) + { + try + { + String type = artifact.getType(); + if ( "tld".equals( type ) ) + { + copyFile( context, artifact.getFile(), TLD_PATH + targetFileName ); + } + else if ( "aar".equals( type ) ) + { + copyFile( context, artifact.getFile(), SERVICES_PATH + targetFileName ); + } + else if ( "jar".equals( type ) || "ejb".equals( type ) || "ejb-client".equals( type ) || + "test-jar".equals( type ) ) + { + copyFile( context, artifact.getFile(), LIB_PATH + targetFileName ); + } + else if ( "par".equals( type ) ) + { + targetFileName = targetFileName.substring( 0, targetFileName.lastIndexOf( '.' ) ) + ".jar"; + copyFile( context, artifact.getFile(), LIB_PATH + targetFileName ); + } + else + { + context.getLogger().debug( + "Artifact of type[" + type + "] is not supported, ignoring[" + artifact + "]" ); + } + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Failed to copy file for artifact[" + artifact + "]", e ); + } + } + } + } + + /** + * Searches a set of artifacts for duplicate filenames and returns a list + * of duplicates. + * + * @param context the packaging context + * @param artifacts set of artifacts + * @return List of duplicated artifacts as bundling file names + */ + private List findDuplicates( WarPackagingContext context, Set artifacts ) + { + List duplicates = new ArrayList(); + List identifiers = new ArrayList(); + for ( Iterator iter = artifacts.iterator(); iter.hasNext(); ) + { + Artifact artifact = (Artifact) iter.next(); + String candidate = getArtifactFinalName( context, artifact ); + if ( identifiers.contains( candidate ) ) + { + duplicates.add( candidate ); + } + else + { + identifiers.add( candidate ); + } + } + return duplicates; + } +} Modified: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/OverlayPackagingTask.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/OverlayPackagingTask.java?view=diff&rev=553329&r1=553328&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/OverlayPackagingTask.java (original) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/OverlayPackagingTask.java Wed Jul 4 13:59:49 2007 @@ -22,7 +22,6 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.war.Overlay; import org.apache.maven.plugin.war.util.PathSet; -import org.codehaus.plexus.util.DirectoryScanner; import java.io.File; import java.io.IOException; @@ -69,7 +68,7 @@ final File tmpDir = unpackOverlay( context, overlay ); // Step2: setup - final PathSet includes = getIncludes( tmpDir ); + final PathSet includes = getFilesToIncludes( tmpDir, overlay.getIncludes(), overlay.getExcludes() ); // Copy copyFiles( context, tmpDir, includes ); @@ -130,22 +129,5 @@ result.mkdir(); } return result; - } - - /** - * Returns the file to copy based on the includes/excludes. - * - * @param overlayDir the directory containing the overlay - * @return the files to copy - */ - protected PathSet getIncludes( File overlayDir ) - { - DirectoryScanner scanner = new DirectoryScanner(); - scanner.setBasedir( overlayDir ); - scanner.setExcludes( overlay.getIncludes() ); - scanner.setIncludes( overlay.getExcludes() ); - scanner.scan(); - return new PathSet( scanner.getIncludedFiles() ); - } } Modified: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java?view=diff&rev=553329&r1=553328&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java (original) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarPackagingContext.java Wed Jul 4 13:59:49 2007 @@ -19,67 +19,79 @@ * under the License. */ +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.war.util.PathSet; +import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.manager.ArchiverManager; import org.codehaus.plexus.logging.Logger; import java.io.File; +import java.util.List; +import java.util.Map; /** * The packaging context. * * @author Stephane Nicoll */ -public class WarPackagingContext +public interface WarPackagingContext { + /** + * Returns the maven project. + * + * @return the project + */ + MavenProject getProject(); + + /** + * Returns the webapp directory. Packaging tasks should use this + * directory to generate the webapp. + * + * @return the webapp directory + */ + File getWebAppDirectory(); + + /** + * Returns the main webapp source directory. + * + * @return the webapp source directory + */ + File getWebAppSourceDirectory(); + + /** + * Returns the webapp source includes. + * + * @return the webapp source includes + */ + String[] getWebAppSourceIncludes(); - private final Logger logger; - private final File overlaysWorkDirectory; - private final ArchiverManager archiverManager; - private final PathSet protectedFiles; - private final File webAppDirectory; - - - public WarPackagingContext( Logger logger, File overlaysWorkDirectory, ArchiverManager archiverManager, PathSet protectedFiles, - File webAppDirectory ) - { - this.logger = logger; - this.overlaysWorkDirectory = overlaysWorkDirectory; - this.archiverManager = archiverManager; - this.protectedFiles = protectedFiles; - this.webAppDirectory = webAppDirectory; - } + /** + * Returns the webapp source excludes. + * + * @return the webapp source excludes + */ + String[] getWebAppSourceExcludes(); /** * Returns the logger to use to output logging event. * * @return the logger */ - public Logger getLogger() - { - return logger; - } + Logger getLogger(); /** * Returns the directory to unpack dependent WARs into if needed. * * @return the overlays work directory */ - public File getOverlaysWorkDirectory() - { - return overlaysWorkDirectory; - } + File getOverlaysWorkDirectory(); /** * Returns the archiver manager to use. * * @return the archiver manager */ - public ArchiverManager getArchiverManager() - { - return archiverManager; - } - + ArchiverManager getArchiverManager(); /** * Returns the list of files that have already been copied during the @@ -90,20 +102,34 @@ * * @return the list of files that have already been copied */ - public PathSet getProtectedFiles() - { - return protectedFiles; - } + PathSet getProtectedFiles(); /** - * Returns the webapp directory. Packaging tasks should use this - * directory to generate the webapp. + * Returns the output file name mapping to use, if any. Returns null + * if no file name mapping is set. * - * @return the web app directory + * @return the output file name mapping or null */ - public File getWebAppDirectory() - { - return webAppDirectory; - } + String getOutputFileNameMapping(); + + /** + * Returns the list of filter files to use. + * + * @return a list of filter files + */ + List getFilters(); + + /** + * Returns the filter properties to use to filter resources. + *

+ * TODO: this needs to be refactored to use the resource plugin somehow. + * + * @return a map of filter properties + * @throws MojoExecutionException if an error occured while reading a filter file + */ + Map getFilterProperties() + throws MojoExecutionException; + + } Added: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java?view=auto&rev=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java (added) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java Wed Jul 4 13:59:49 2007 @@ -0,0 +1,199 @@ +package org.apache.maven.plugin.war.packaging; + +import org.apache.maven.model.Resource; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.war.util.PathSet; +import org.codehaus.plexus.util.DirectoryScanner; + +import java.io.File; +import java.io.IOException; + +/** + * Handles the project own resources, that is: + *

    The list of web resources, if any + *
  • The content of the webapp directory if it exists
  • + *
  • The dependencies of the project
  • + *
+ * + * @author Stephane Nicoll + */ +public class WarProjectPackagingTask + extends AbstractWarPackagingTask +{ + private final Resource[] webResources; + + + public WarProjectPackagingTask( Resource[] webResources ) + { + if ( webResources != null ) + { + this.webResources = webResources; + } + else + { + this.webResources = new Resource[0]; + } + } + + public void performPackaging( WarPackagingContext context ) + throws MojoExecutionException + { + + handleWebResources( context ); + + handeWebAppSourceDirectory( context ); + + handleArtifacts( context ); + } + + + /** + * Handles the web resources. + * + * @param context the packaging context + * @throws MojoExecutionException if a resource could not be copied + */ + protected void handleWebResources( WarPackagingContext context ) + throws MojoExecutionException + { + for ( int i = 0; i < webResources.length; i++ ) + { + Resource resource = webResources[i]; + if ( !( new File( resource.getDirectory() ) ).isAbsolute() ) + { + resource.setDirectory( context.getProject().getBasedir() + File.separator + resource.getDirectory() ); + } + + // Make sure that the resource directory is not the same as the webappDirectory + if ( !resource.getDirectory().equals( context.getWebAppDirectory().getPath() ) ) + { + + try + { + copyResources( context, resource ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( "Could not copy resource[" + resource.getDirectory() + "]", e ); + } + } + } + } + + /** + * Handles the webapp sources. + * + * @param context the packaging context + * @throws MojoExecutionException if the sources could not be copied + */ + protected void handeWebAppSourceDirectory( WarPackagingContext context ) + throws MojoExecutionException + { + + // Make sure that the resource directory is not the same as the webappDirectory + if ( !context.getWebAppSourceDirectory().getAbsolutePath().equals( context.getWebAppDirectory().getPath() ) ) + { + final PathSet sources = getFilesToIncludes( context.getWebAppSourceDirectory(), + context.getWebAppSourceIncludes(), + context.getWebAppSourceExcludes() ); + + try + { + copyFiles( context, context.getWebAppSourceDirectory(), sources ); + } + catch ( IOException e ) + { + throw new MojoExecutionException( + "Could not copy webapp sources[" + context.getWebAppDirectory().getAbsolutePath() + "]", e ); + } + } + } + + /** + * Handles the webapp artifacts. + * + * @param context the packaging context + * @throws MojoExecutionException if the artifacts could not be copied + */ + protected void handleArtifacts( WarPackagingContext context ) + throws MojoExecutionException + { + ArtifactsPackagingTask task = new ArtifactsPackagingTask( context.getProject().getArtifacts() ); + task.performPackaging( context ); + } + + + /** + * Copies webapp webResources from the specified directory. + * + * @param context the war packaging context to use + * @param resource the resource to copy + * @throws IOException if an error occured while copying the resources + * @throws MojoExecutionException if an error occured while retrieving the filter properties + */ + public void copyResources( WarPackagingContext context, Resource resource ) + throws IOException, MojoExecutionException + { + if ( !context.getWebAppDirectory().exists() ) + { + context.getLogger().warn( "Not copyuing webapp webResources[" + resource.getDirectory() + + "]: webapp directory[" + context.getWebAppDirectory().getAbsolutePath() + "] does not exist!" ); + } + + context.getLogger().info( "Copy webapp webResources[" + resource.getDirectory() + "] to[" + + context.getWebAppDirectory().getAbsolutePath() + "]" ); + String[] fileNames = getFilesToCopy( resource ); + for ( int i = 0; i < fileNames.length; i++ ) + { + String targetFileName = fileNames[i]; + if ( resource.getTargetPath() != null ) + { + //TODO make sure this thing is 100% safe + targetFileName = resource.getTargetPath() + File.separator + targetFileName; + } + if ( resource.isFiltering() ) + { + copyFilteredFile( context, new File( resource.getDirectory(), fileNames[i] ), targetFileName ); + } + else + { + copyFile( context, new File( resource.getDirectory(), fileNames[i] ), targetFileName ); + } + } + } + + + /** + * Returns a list of filenames that should be copied + * over to the destination directory. + * + * @param resource the resource to be scanned + * @return the array of filenames, relative to the sourceDir + */ + private String[] getFilesToCopy( Resource resource ) + { + DirectoryScanner scanner = new DirectoryScanner(); + scanner.setBasedir( resource.getDirectory() ); + if ( resource.getIncludes() != null && !resource.getIncludes().isEmpty() ) + { + scanner.setIncludes( + (String[]) resource.getIncludes().toArray( new String[resource.getIncludes().size()] ) ); + } + else + { + scanner.setIncludes( DEFAULT_INCLUDES ); + } + if ( resource.getExcludes() != null && !resource.getExcludes().isEmpty() ) + { + scanner.setExcludes( + (String[]) resource.getExcludes().toArray( new String[resource.getExcludes().size()] ) ); + } + + scanner.addDefaultExcludes(); + + scanner.scan(); + + return scanner.getIncludedFiles(); + } +} Copied: maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/MappingUtils.java (from r543832, maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/MappingUtils.java) URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/MappingUtils.java?view=diff&rev=553329&p1=maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/MappingUtils.java&r1=543832&p2=maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/MappingUtils.java&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/MappingUtils.java (original) +++ maven/plugins/branches/MWAR-97-2/src/main/java/org/apache/maven/plugin/war/util/MappingUtils.java Wed Jul 4 13:59:49 2007 @@ -1,4 +1,4 @@ -package org.apache.maven.plugin.war; +package org.apache.maven.plugin.war.util; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -45,7 +45,7 @@ * * @author Stephane Nicoll */ -class MappingUtils +public class MappingUtils { /** @@ -99,9 +99,9 @@ /** - * This a copy of the class in plexus-utils 1.4 + * This a copy of the class in plexus-util 1.4 *

- * TODO: remove this once the plugin can depend on plexus-utils 1.4 + * TODO: remove this once the plugin can depend on plexus-util 1.4 */ static class RegexBasedInterpolator { Modified: maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/MappingUtilsTest.java URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/MappingUtilsTest.java?view=diff&rev=553329&r1=553328&r2=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/MappingUtilsTest.java (original) +++ maven/plugins/branches/MWAR-97-2/src/test/java/org/apache/maven/plugin/war/MappingUtilsTest.java Wed Jul 4 13:59:49 2007 @@ -22,6 +22,7 @@ import junit.framework.TestCase; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.war.stub.AbstractArtifactStub; +import org.apache.maven.plugin.war.util.MappingUtils; /** * Tests the mapping of file names. Added: maven/plugins/branches/MWAR-97-2/src/test/resources/unit/waroverlays/default.xml URL: http://svn.apache.org/viewvc/maven/plugins/branches/MWAR-97-2/src/test/resources/unit/waroverlays/default.xml?view=auto&rev=553329 ============================================================================== --- maven/plugins/branches/MWAR-97-2/src/test/resources/unit/waroverlays/default.xml (added) +++ maven/plugins/branches/MWAR-97-2/src/test/resources/unit/waroverlays/default.xml Wed Jul 4 13:59:49 2007 @@ -0,0 +1,32 @@ + + + + war-plugin-test + + + + maven-war-plugin + + + + + + +