You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sn...@apache.org on 2009/07/06 06:08:27 UTC

svn commit: r791368 - in /maven/plugins/trunk/maven-war-plugin/src: main/java/org/apache/maven/plugin/war/packaging/ main/java/org/apache/maven/plugin/war/util/ test/java/org/apache/maven/plugin/war/util/

Author: snicoll
Date: Mon Jul  6 04:08:27 2009
New Revision: 791368

URL: http://svn.apache.org/viewvc?rev=791368&view=rev
Log:
MWAR-158: Fixed usage of web.xml in case of overlay.

Modified:
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/PathSet.java
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/WebappStructure.java
    maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/util/WebappStructureTest.java

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java?rev=791368&r1=791367&r2=791368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/packaging/WarProjectPackagingTask.java Mon Jul  6 04:08:27 2009
@@ -19,10 +19,6 @@
  * under the License.
  */
 
-import java.io.File;
-import java.io.IOException;
-import java.util.Iterator;
-
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -33,6 +29,10 @@
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.XmlStreamReader;
 
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+
 /**
  * Handles the project own resources, that is:
  * <ul
@@ -151,13 +151,13 @@
         {
             context.getLog().debug( "webapp sources directory does not exist - skipping." );
         }
-        else
-        if ( !context.getWebappSourceDirectory().getAbsolutePath().equals( context.getWebappDirectory().getPath() ) )
+        else if ( !context.getWebappSourceDirectory().getAbsolutePath().equals(
+            context.getWebappDirectory().getPath() ) )
         {
             context.getLog().info( "Copying webapp resources[" + context.getWebappSourceDirectory() + "]" );
-            final PathSet sources = getFilesToIncludes( context.getWebappSourceDirectory(),
-                                                        context.getWebappSourceIncludes(),
-                                                        context.getWebappSourceExcludes() );
+            final PathSet sources =
+                getFilesToIncludes( context.getWebappSourceDirectory(), context.getWebappSourceIncludes(),
+                                    context.getWebappSourceExcludes() );
 
             try
             {
@@ -219,6 +219,10 @@
                 {
                     throw new MojoFailureException( "The specified web.xml file '" + webXml + "' does not exist" );
                 }
+
+                // Making sure that it won't get overlayed
+                context.getWebappStructure().registerFileForced( id, WEB_INF_PATH + "/web.xml" );
+
                 if ( context.isFilteringDeploymentDescriptors() )
                 {
                     context.getMavenFileFilter().copyFile( webXml, new File( webinfDir, "web.xml" ), true,
@@ -228,8 +232,6 @@
                 {
                     copyFile( context, webXml, new File( webinfDir, "web.xml" ), "WEB-INF/web.xml", true );
                 }
-
-                context.getWebappStructure().getFullStructure().add( WEB_INF_PATH + "/web.xml" );
             }
             else
             {
@@ -238,15 +240,18 @@
                 // if exists we can filter it
                 if ( defaultWebXml.exists() && context.isFilteringDeploymentDescriptors() )
                 {
+                    context.getWebappStructure().registerFile( id, WEB_INF_PATH + "/web.xml" );
                     context.getMavenFileFilter().copyFile( defaultWebXml, new File( webinfDir, "web.xml" ), true,
                                                            context.getFilterWrappers(), getEncoding( defaultWebXml ) );
-                    context.getWebappStructure().getFullStructure().add( WEB_INF_PATH + "/web.xml" );
                 }
             }
 
             if ( containerConfigXML != null && StringUtils.isNotEmpty( containerConfigXML.getName() ) )
             {
                 String xmlFileName = containerConfigXML.getName();
+
+                context.getWebappStructure().registerFileForced( id, META_INF_PATH + "/" + xmlFileName );
+
                 if ( context.isFilteringDeploymentDescriptors() )
                 {
                     context.getMavenFileFilter().copyFile( containerConfigXML, new File( metainfDir, xmlFileName ),
@@ -258,7 +263,6 @@
                     copyFile( context, containerConfigXML, new File( metainfDir, xmlFileName ),
                               "META-INF/" + xmlFileName, true );
                 }
-                context.getWebappStructure().getFullStructure().add( META_INF_PATH + "/" + xmlFileName );
             }
         }
         catch ( IOException e )
@@ -299,14 +303,12 @@
         if ( !context.getWebappDirectory().exists() )
         {
             context.getLog().warn(
-                                   "Not copying webapp webResources[" + resource.getDirectory()
-                                       + "]: webapp directory[" + context.getWebappDirectory().getAbsolutePath()
-                                       + "] does not exist!" );
+                "Not copying webapp webResources[" + resource.getDirectory() + "]: webapp directory[" +
+                    context.getWebappDirectory().getAbsolutePath() + "] does not exist!" );
         }
 
-        context.getLog().info(
-                               "Copying webapp webResources[" + resource.getDirectory() + "] to["
-                                   + context.getWebappDirectory().getAbsolutePath() + "]" );
+        context.getLog().info( "Copying webapp webResources[" + resource.getDirectory() + "] to[" +
+            context.getWebappDirectory().getAbsolutePath() + "]" );
         String[] fileNames = getFilesToCopy( resource );
         for ( int i = 0; i < fileNames.length; i++ )
         {
@@ -317,8 +319,8 @@
                 // MWAR-129 if targetPath is only a dot <targetPath>.</targetPath> or ./
                 // and the Resource is in a part of the warSourceDirectory the file from sources will override this
                 // that's we don't have to add the targetPath yep not nice but works
-                if ( !StringUtils.equals( ".", resource.getTargetPath() )
-                    && !StringUtils.equals( "./", resource.getTargetPath() ) )
+                if ( !StringUtils.equals( ".", resource.getTargetPath() ) &&
+                    !StringUtils.equals( "./", resource.getTargetPath() ) )
                 {
                     targetFileName = resource.getTargetPath() + File.separator + targetFileName;
                 }

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/PathSet.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/PathSet.java?rev=791368&r1=791367&r2=791368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/PathSet.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/PathSet.java Mon Jul  6 04:08:27 2009
@@ -197,6 +197,19 @@
     }
 
     /**
+     * Removes the specified path if it exists.
+     *
+     * @param path the path to remove
+     * @return true if the path was removed, false if it did not existed
+     */
+    boolean remove( String path )
+    {
+        final String normalizedPath = normalizeFilePath( path );
+        return pathsSet.remove( normalizedPath );
+
+    }
+
+    /**
      * Returns iterator of normalized paths (strings)
      *
      * @return iterator of normalized paths (strings)

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/WebappStructure.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/WebappStructure.java?rev=791368&r1=791367&r2=791368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/WebappStructure.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/util/WebappStructure.java Mon Jul  6 04:08:27 2009
@@ -71,7 +71,7 @@
      * Creates a new instance with the specified cache.
      *
      * @param dependencies the dependencies of the project
-     * @param cache the cache
+     * @param cache        the cache
      */
     public WebappStructure( List dependencies, WebappStructure cache )
     {
@@ -154,6 +154,37 @@
     }
 
     /**
+     * Forces the registration of the specified path for the specified owner. If
+     * the file is not registered yet, a simple registration is performed. If the
+     * file already exists, the owner changes to the specified one.
+     * <p/>
+     * Beware that the semantic of the return boolean is different thant the one
+     * from {@link #registerFile(String, String)}; returns <tt>true</tt> if an
+     * owner replacement was made and <tt>false</tt> if the file was simply registered
+     * for the first time.
+     *
+     * @param id   the owner of the path
+     * @param path the relative path from the webapp root directory
+     * @return false if the file did not exist, true if the owner was replaced
+     */
+    public boolean registerFileForced( String id, String path )
+    {
+        if ( !isRegistered( path ) )
+        {
+            doRegister( id, path );
+            return false;
+        }
+        else
+        {
+            // Force the switch to the new owner
+            getStructure( getOwner( path ) ).remove( path );
+            getStructure( id ).add( path );
+            return true;
+        }
+
+    }
+
+    /**
      * Registers the specified path for the specified owner. Invokes
      * the <tt>callback</tt> with the result of the registration.
      *
@@ -350,7 +381,7 @@
     /**
      * Registers the target file name for the specified artifact.
      *
-     * @param artifact the artifact
+     * @param artifact       the artifact
      * @param targetFileName the target file name
      */
     public void registerTargetFileName( Artifact artifact, String targetFileName )
@@ -387,10 +418,10 @@
         {
             DependencyInfo dependencyInfo = (DependencyInfo) it.next();
             final Dependency dependency2 = dependencyInfo.getDependency();
-            if ( StringUtils.equals( dependency.getGroupId(), dependency2.getGroupId() )
-                && StringUtils.equals( dependency.getArtifactId(), dependency2.getArtifactId() )
-                && StringUtils.equals( dependency.getType(), dependency2.getType() )
-                && StringUtils.equals( dependency.getClassifier(), dependency2.getClassifier() ) )
+            if ( StringUtils.equals( dependency.getGroupId(), dependency2.getGroupId() ) &&
+                StringUtils.equals( dependency.getArtifactId(), dependency2.getArtifactId() ) &&
+                StringUtils.equals( dependency.getType(), dependency2.getType() ) &&
+                StringUtils.equals( dependency.getClassifier(), dependency2.getClassifier() ) )
             {
 
                 return dependencyInfo.getTargetFileName();
@@ -411,7 +442,7 @@
     /**
      * Find a dependency that is similar from the specified dependency.
      *
-     * @param dependency the dependency to find
+     * @param dependency   the dependency to find
      * @param dependencies a list of dependencies
      * @return a similar dependency or <tt>null</tt> if no similar dependency is found
      */
@@ -421,11 +452,12 @@
         while ( it.hasNext() )
         {
             Dependency dep = (Dependency) it.next();
-            if ( dependency.getGroupId().equals( dep.getGroupId() )
-                && dependency.getArtifactId().equals( dep.getArtifactId() )
-                && dependency.getType().equals( dep.getType() ) && (
-                ( dependency.getClassifier() == null && dep.getClassifier() == null ) || (
-                    dependency.getClassifier() != null && dependency.getClassifier().equals( dep.getClassifier() ) ) ) )
+            if ( dependency.getGroupId().equals( dep.getGroupId() ) &&
+                dependency.getArtifactId().equals( dep.getArtifactId() ) &&
+                dependency.getType().equals( dep.getType() ) &&
+                ( ( dependency.getClassifier() == null && dep.getClassifier() == null ) ||
+                    ( dependency.getClassifier() != null &&
+                        dependency.getClassifier().equals( dep.getClassifier() ) ) ) )
             {
                 return dep;
             }
@@ -596,7 +628,7 @@
         /**
          * Called if the version of the dependency has changed since the last build.
          *
-         * @param dependency the dependency
+         * @param dependency      the dependency
          * @param previousVersion the previous version of the dependency
          */
         void updatedVersion( Dependency dependency, String previousVersion );
@@ -604,7 +636,7 @@
         /**
          * Called if the scope of the dependency has changed since the last build.
          *
-         * @param dependency the dependency
+         * @param dependency    the dependency
          * @param previousScope the previous scope
          */
         void updatedScope( Dependency dependency, String previousScope );
@@ -613,7 +645,7 @@
          * Called if the optional flag of the dependency has changed since the
          * last build.
          *
-         * @param dependency the dependency
+         * @param dependency       the dependency
          * @param previousOptional the previous optional flag
          */
         void updatedOptionalFlag( Dependency dependency, boolean previousOptional );
@@ -621,7 +653,7 @@
         /**
          * Called if the dependency has been updated for unknown reason.
          *
-         * @param dependency the dependency
+         * @param dependency  the dependency
          * @param previousDep the previous dependency
          */
         void updatedUnknown( Dependency dependency, Dependency previousDep );

Modified: maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/util/WebappStructureTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/util/WebappStructureTest.java?rev=791368&r1=791367&r2=791368&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/util/WebappStructureTest.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/util/WebappStructureTest.java Mon Jul  6 04:08:27 2009
@@ -8,8 +8,6 @@
 import java.util.List;
 
 /**
- *
- *
  * @author Stephane Nicoll
  */
 public class WebappStructureTest
@@ -205,6 +203,40 @@
 
     }
 
+    public void testUnknownFileNotAvailable()
+    {
+        final WebappStructure structure = new WebappStructure( new ArrayList() );
+        assertFalse( structure.isRegistered( "/foo/bar.txt" ) );
+    }
+
+    public void testRegisterSamePathTwice()
+    {
+        final WebappStructure structure = new WebappStructure( new ArrayList() );
+        structure.registerFile( "overlay1", "WEB-INF/web.xml" );
+        assertFalse( structure.registerFile( "currentBuild", "WEB-INF/web.xml" ) );
+
+    }
+
+    public void testRegisterForced()
+    {
+        final String path = "WEB-INF/web.xml";
+        final WebappStructure structure = new WebappStructure( new ArrayList() );
+        assertFalse("New file should return false",
+                    structure.registerFileForced( "overlay1", path ));
+        assertEquals( "overlay1", structure.getOwner( path ) );         
+    }
+
+    public void testRegisterSamePathTwiceForced()
+    {
+        final String path = "WEB-INF/web.xml";
+        final WebappStructure structure = new WebappStructure( new ArrayList() );
+        structure.registerFile( "overlay1", path );
+        assertEquals( "overlay1", structure.getOwner( path ) );
+        assertTrue("owner replacement should have returned true",
+                   structure.registerFileForced( "currentBuild", path ));
+        assertEquals("currentBuild", structure.getOwner( path ));
+    }
+
 
     protected Dependency createDependency( String groupId, String artifactId, String version, String type, String scope,
                                            String classifier )