You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by br...@apache.org on 2006/06/27 09:30:32 UTC

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

Author: brett
Date: Tue Jun 27 00:30:32 2006
New Revision: 417362

URL: http://svn.apache.org/viewvc?rev=417362&view=rev
Log:
[MWAR-41] align filtering to assembly/resources (filter project properties)

Added:
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/CompositeMap.java   (with props)
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/ReflectionProperties.java
      - copied, changed from r417334, maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java
Modified:
    maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java
    maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarExplodedMojoTest.java

Modified: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java?rev=417362&r1=417361&r2=417362&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/AbstractWarMojo.java Tue Jun 27 00:30:32 2006
@@ -50,6 +50,7 @@
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 
@@ -346,7 +347,7 @@
             List webResources = this.webResources != null ? Arrays.asList( this.webResources ) : null;
             if ( webResources != null && webResources.size() > 0 )
             {
-                Properties filterProperties = getBuildFilterProperties();
+                Map filterProperties = getBuildFilterProperties();
                 for ( Iterator it = webResources.iterator(); it.hasNext(); )
                 {
                     Resource resource = (Resource) it.next();
@@ -377,11 +378,11 @@
         }
     }
 
-    private Properties getBuildFilterProperties()
+    private Map getBuildFilterProperties()
         throws MojoExecutionException
     {
         // System properties
-        Properties filterProperties = new Properties( System.getProperties() );
+        Map filterProperties = new Properties( System.getProperties() );
 
         // Project properties
         filterProperties.putAll( project.getProperties() );
@@ -401,7 +402,9 @@
                 throw new MojoExecutionException( "Error loading property file '" + filtersfile + "'", e );
             }
         }
-        return filterProperties;
+
+        // can't putAll, as ReflectionProperties doesn't enumerate - so we make a composite map with the project variables as dominant
+        return new CompositeMap( new ReflectionProperties( project ), filterProperties );
     }
 
     /**
@@ -417,7 +420,7 @@
      * @param filterProperties
      * @throws java.io.IOException if an error occured while copying webResources
      */
-    public void copyResources( Resource resource, File webappDirectory, Properties filterProperties )
+    public void copyResources( Resource resource, File webappDirectory, Map filterProperties )
         throws IOException
     {
         if ( !resource.getDirectory().equals( webappDirectory.getPath() ) )
@@ -496,7 +499,7 @@
         {
             archiver.getArchiver().addDirectory( classesDirectory, getIncludes(), getExcludes() );
 
-            archiver.createArchive( getProject(), archive );
+            archiver.createArchive( project, archive );
         }
         catch ( Exception e )
         {
@@ -837,7 +840,7 @@
             // support ${token}
             new FilterWrapper()
             {
-                public Reader getReader( Reader fileReader, Properties filterProperties )
+                public Reader getReader( Reader fileReader, Map filterProperties )
                 {
                     return new InterpolationFilterReader( fileReader, filterProperties, "${", "}" );
                 }
@@ -845,7 +848,7 @@
             // support @token@
             new FilterWrapper()
             {
-                public Reader getReader( Reader fileReader, Properties filterProperties )
+                public Reader getReader( Reader fileReader, Map filterProperties )
                 {
                     return new InterpolationFilterReader( fileReader, filterProperties, "@", "@" );
                 }
@@ -861,7 +864,7 @@
      * @throws IOException TO DO: Remove this method when Maven moves to plexus-utils version 1.4
      */
     private static void copyFilteredFile( File from, File to, String encoding, FilterWrapper[] wrappers,
-                                          Properties filterProperties )
+                                          Map filterProperties )
         throws IOException
     {
         // buffer so it isn't reading a byte at a time!
@@ -994,7 +997,7 @@
      */
     private interface FilterWrapper
     {
-        Reader getReader( Reader fileReader, Properties filterProperties );
+        Reader getReader( Reader fileReader, Map filterProperties );
     }
 
     /**

Added: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/CompositeMap.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/CompositeMap.java?rev=417362&view=auto
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/CompositeMap.java (added)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/CompositeMap.java Tue Jun 27 00:30:32 2006
@@ -0,0 +1,58 @@
+package org.apache.maven.plugin.war;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed 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 java.util.AbstractMap;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @version $Id$
+ * @todo merge with resources/assembly plugin
+ */
+public class CompositeMap
+    extends AbstractMap
+{
+    private Map recessive;
+
+    private Map dominant;
+
+    public CompositeMap( Map dominant, Map recessive )
+    {
+        this.dominant = Collections.unmodifiableMap( dominant );
+
+        this.recessive = Collections.unmodifiableMap( recessive );
+    }
+
+    public synchronized Object get( Object key )
+    {
+        Object value = dominant.get( key );
+
+        if ( value == null )
+        {
+            value = recessive.get( key );
+        }
+
+        return value;
+    }
+
+    public Set entrySet()
+    {
+        throw new UnsupportedOperationException( "Cannot enumerate properties in a composite map" );
+    }
+}

Propchange: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/CompositeMap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/CompositeMap.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Copied: maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/ReflectionProperties.java (from r417334, maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java)
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/ReflectionProperties.java?p2=maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/ReflectionProperties.java&p1=maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java&r1=417334&r2=417362&rev=417362&view=diff
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/main/java/org/apache/maven/plugin/war/ReflectionProperties.java Tue Jun 27 00:30:32 2006
@@ -1,7 +1,7 @@
-package org.apache.maven.plugin.resources;
+package org.apache.maven.plugin.war;
 
 /*
- * Copyright 2001-2005 The Apache Software Foundation.
+ * Copyright 2001-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,57 +17,41 @@
  */
 
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
 
-import java.util.Properties;
-
+import java.util.AbstractMap;
+import java.util.Set;
 
 /**
- * @author Andreas Hoheneder (ahoh_at_inode.at)
  * @version $Id$
+ * @todo merge with resources/assembly plugin
  */
 public class ReflectionProperties
-    extends Properties
+    extends AbstractMap
 {
-
     private MavenProject project;
 
-    boolean escapedBackslashesInFilePath;
-
-    public ReflectionProperties( MavenProject aProject, boolean escapedBackslashesInFilePath ) 
+    public ReflectionProperties( MavenProject project )
     {
-       super();
-
-       project = aProject;
-
-       this.escapedBackslashesInFilePath = escapedBackslashesInFilePath;
+        this.project = project;
     }
-    
-    public Object get( Object key )
+
+    public synchronized Object get( Object key )
     {
         Object value = null;
-        try 
+        try
         {
-            value = ReflectionValueExtractor.evaluate( "" + key , project );
-
-            if ( escapedBackslashesInFilePath && value != null &&
-                "java.lang.String".equals( value.getClass().getName() ) )
-            {
-                String val = (String) value;
-
-                // Check if it's a windows path
-                if ( val.indexOf( ":\\" ) == 1 )
-                {
-                    value = StringUtils.replace( (String)value, "\\", "\\\\" );
-                    value = StringUtils.replace( (String)value, ":", "\\:" );
-                }
-            }
+            value = ReflectionValueExtractor.evaluate( String.valueOf( key ), project );
         }
-        catch ( Exception e ) 
+        catch ( Exception e )
         {
             //TODO: remove the try-catch block when ReflectionValueExtractor.evaluate() throws no more exceptions
-        } 
+        }
         return value;
+    }
+
+    public Set entrySet()
+    {
+        throw new UnsupportedOperationException( "Cannot enumerate properties in a project" );
     }
 }

Modified: maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarExplodedMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarExplodedMojoTest.java?rev=417362&r1=417361&r2=417362&view=diff
==============================================================================
--- maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarExplodedMojoTest.java (original)
+++ maven/plugins/trunk/maven-war-plugin/src/test/java/org/apache/maven/plugin/war/WarExplodedMojoTest.java Tue Jun 27 00:30:32 2006
@@ -32,7 +32,6 @@
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileWriter;
 import java.io.StringReader;
 import java.util.LinkedList;
 import java.util.List;
@@ -545,22 +544,17 @@
         filterList.add( filterFile.getAbsolutePath() );
 
         // prepare web resources and filters
-        String filterData = "resource_key=${resource_value}\n";
-        String systemData = "system_key=${user.dir}\n";
-        String projectProp = "project_key=${is_this_simple}\n";
-        FileWriter writer = new FileWriter( sampleResourceWDir );
-        writer.write( filterData + systemData + projectProp );
-        writer.flush();
-        writer.close();
-        writer = new FileWriter( sampleResource );
-        writer.write( filterData + systemData + projectProp );
-        writer.flush();
-        writer.close();
-
-        writer = new FileWriter( filterFile );
-        writer.write( "resource_value=this_is_filtered" );
-        writer.flush();
-        writer.close();
+        String content = "resource_key=${resource_value}\n";
+        content += "system_key=${user.dir}\n";
+        content += "project_key=${is_this_simple}\n";
+        content += "project_name=${project.name}\n";
+        content += "system_property=${system.property}\n";
+        FileUtils.fileWrite( sampleResourceWDir.getAbsolutePath(), content );
+        FileUtils.fileWrite( sampleResource.getAbsolutePath(), content );
+
+        FileUtils.fileWrite( filterFile.getAbsolutePath(), "resource_value=this_is_filtered" );
+
+        System.setProperty( "system.property", "system-property-value" );
 
         // configure mojo
         project.addProperty( "is_this_simple", "i_think_so" );
@@ -583,15 +577,20 @@
                     expectedResourceWDirFile.exists() );
 
         // validate filtered file
-        String content = FileUtils.fileRead( expectedResourceWDirFile );
+        content = FileUtils.fileRead( expectedResourceWDirFile );
         BufferedReader reader = new BufferedReader( new StringReader( content ) );
 
         assertEquals( "error in filtering using filter files", "resource_key=this_is_filtered", reader.readLine() );
 
-        assertEquals( "error in filtering using System properties", reader.readLine(),
-                      "system_key=" + System.getProperty( "user.dir" ) );
+        assertEquals( "error in filtering using System properties", "system_key=" + System.getProperty( "user.dir" ),
+                      reader.readLine() );
 
         assertEquals( "error in filtering using project properties", "project_key=i_think_so", reader.readLine() );
+
+        assertEquals( "error in filtering using project properties", "project_name=Test Project ", reader.readLine() );
+
+        assertEquals( "error in filtering using System properties", "system_property=system-property-value",
+                      reader.readLine() );
     }
 
     public void testExplodedWar_WithSourceIncludeExclude()