You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ma...@apache.org on 2007/10/13 17:20:56 UTC

svn commit: r584410 - in /maven/plugins/trunk/maven-resources-plugin/src: it/ it/filter/ it/filter/src/ it/filter/src/main/ it/filter/src/main/filters/ it/filter/src/main/resources/ main/java/org/apache/maven/plugin/resources/ test/java/org/apache/mave...

Author: mauro
Date: Sat Oct 13 08:20:45 2007
New Revision: 584410

URL: http://svn.apache.org/viewvc?rev=584410&view=rev
Log:
MRESOURCES-47:  applied patch from William Ferguson.

Added:
    maven/plugins/trunk/maven-resources-plugin/src/it/
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/pom.xml
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/filters/
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/filters/my.filter
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/resources/
    maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/resources/SomeResource.txt
    maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/EnhancedPropertyUtilsTest.java
    maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced.properties
    maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced_validation.properties
Modified:
    maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java
    maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java

Added: maven/plugins/trunk/maven-resources-plugin/src/it/filter/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/it/filter/pom.xml?rev=584410&view=auto
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/it/filter/pom.xml (added)
+++ maven/plugins/trunk/maven-resources-plugin/src/it/filter/pom.xml Sat Oct 13 08:20:45 2007
@@ -0,0 +1,42 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins</groupId>
+  <artifactId>maven-resources-plugin-it-filter</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+
+  <build>
+    <resources>
+      <resource>
+        <directory>src/main/resources</directory>
+        <filtering>true</filtering>
+      </resource>
+    </resources>
+    <filters>
+      <filter>src/main/filters/my.filter</filter>
+    </filters>
+  </build>
+
+  <!--     
+    Look at SomeResource.txt after being processed with filtering. 
+    Note the tokens for ${projectProperty} and ${profileProperty} for the "filter resolution" case should be resolved and evaluated
+    using the POM property values of the filter tokens  
+  -->
+  <properties>
+    <projectProperty>foo-projectProperty-bar</projectProperty>
+  </properties>
+
+  <profiles>
+    <profile>
+      <activation>
+        <activeByDefault>true</activeByDefault>
+      </activation>
+      <properties>
+        <profileProperty>foo-profileProperty-bar</profileProperty>
+      </properties>
+    </profile>
+  </profiles>
+</project>

Added: maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/filters/my.filter
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/filters/my.filter?rev=584410&view=auto
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/filters/my.filter (added)
+++ maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/filters/my.filter Sat Oct 13 08:20:45 2007
@@ -0,0 +1,4 @@
+
+filter.token.project.version=${project.version}
+filter.token.projectProperty=${projectProperty}
+filter.token.profileProperty=${profileProperty}
\ No newline at end of file

Added: maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/resources/SomeResource.txt
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/resources/SomeResource.txt?rev=584410&view=auto
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/resources/SomeResource.txt (added)
+++ maven/plugins/trunk/maven-resources-plugin/src/it/filter/src/main/resources/SomeResource.txt Sat Oct 13 08:20:45 2007
@@ -0,0 +1,10 @@
+
+test:direct resolution project.version=${project.version}
+test:filter resolution project.version=${filter.token.project.version}
+
+test:filter direct projectProperty=${projectProperty}
+test:filter resolution projectProperty=${filter.token.projectProperty}
+
+test:filter direct profileProperty=${profileProperty}
+test:filter resolution profileProperty=${filter.token.profileProperty}
+

Modified: maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java?rev=584410&r1=584409&r2=584410&view=diff
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java (original)
+++ maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java Sat Oct 13 08:20:45 2007
@@ -22,12 +22,13 @@
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import java.util.Enumeration;
+import java.util.Iterator;
 import java.util.Properties;
 
 
 /**
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
+ * @author William Ferguson
  * @version $Id$
  */
 public final class PropertyUtils
@@ -38,6 +39,60 @@
     }
 
     /**
+     * Reads a property file, resolving all internal variables, using the supplied base properties.
+     * <p>
+     * The properties are resolved iteratively, so if the value of property A refers to property B, then after resolution
+     * the value of property B will contain the value of property B.
+     * </p>
+     * 
+     * @param propFile The property file to load.
+     * @param baseProps Properties containing the initial values to subsitute into the properties file.
+     * @return Properties object containing the properties in the file with their values fully resolved.
+     * @throws IOException if profile does not exist, or cannot be read.
+     */
+    public static Properties loadPropertyFile( File propFile, Properties baseProps )
+        throws IOException
+    {
+        if ( !propFile.exists() )
+        {
+            throw new FileNotFoundException( propFile.toString() );
+        }
+
+        final Properties fileProps = new Properties();
+        final FileInputStream inStream = new FileInputStream( propFile );
+        try
+        {
+            fileProps.load( inStream );
+        }
+        finally
+        {
+            IOUtil.close( inStream );
+        }
+
+        final Properties combinedProps = new Properties();
+        combinedProps.putAll( baseProps );
+        combinedProps.putAll( fileProps );
+
+        // The algorithm iterates only over the fileProps which is all that is required to resolve
+        // the properties defined within the file. This is slighlty different to current, however
+        // I suspect that this was the actual original intent.
+        // 
+        // The difference is that #loadPropertyFile(File, boolean, boolean) also resolves System properties
+        // whose values contain expressions. I believe this is unexpected and is not validated by the test cases,
+        // as can be verified by replacing the implementation of #loadPropertyFile(File, boolean, boolean)
+        // with the commented variant I have provided that reuses this method.
+
+        for ( Iterator iter = fileProps.keySet().iterator(); iter.hasNext(); )
+        {
+            final String k = (String) iter.next();
+            final String propValue = getPropertyValue( k, combinedProps );
+            fileProps.setProperty( k, propValue );
+        }
+
+        return fileProps;
+    }
+
+    /**
      * Reads a property file, resolving all internal variables.
      *
      * @param propfile The property file to load
@@ -48,37 +103,32 @@
     public static Properties loadPropertyFile( File propfile, boolean fail, boolean useSystemProps )
         throws IOException
     {
-        Properties props = new Properties();
+        
+        final Properties baseProps = new Properties();
 
-        if ( useSystemProps )
+        if (useSystemProps) 
         {
-            props = new Properties( System.getProperties() );
+            baseProps.putAll(System.getProperties());
         }
 
-        if ( propfile.exists() )
+        final Properties resolvedProps = new Properties();
+        try 
         {
-            FileInputStream inStream = new FileInputStream( propfile );
-            try
-            {
-                props.load( inStream );
-            }
-            finally
+            resolvedProps.putAll(loadPropertyFile(propfile, baseProps));
+        } catch (FileNotFoundException e)
+        {
+            if (fail) 
             {
-                IOUtil.close( inStream );
+                throw new FileNotFoundException(propfile.toString());
             }
         }
-        else if ( fail )
-        {
-            throw new FileNotFoundException( propfile.toString() );
-        }
 
-        for ( Enumeration n = props.propertyNames(); n.hasMoreElements(); )
+        if (useSystemProps) 
         {
-            String k = (String) n.nextElement();
-            props.setProperty( k, getPropertyValue( k, props ) );
+            resolvedProps.putAll(baseProps);
         }
 
-        return props;
+        return resolvedProps;
     }
 
 

Modified: maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java?rev=584410&r1=584409&r2=584410&view=diff
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java (original)
+++ maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java Sat Oct 13 08:20:45 2007
@@ -38,6 +38,7 @@
  * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
  * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
  * @author Andreas Hoheneder
+ * @author William Ferguson
  * @version $Id$
  * @goal resources
  * @phase process-resources
@@ -204,13 +205,18 @@
         // Project properties
         filterProperties.putAll( project.getProperties() );
         
+        // Take a copy of filterProperties to ensure that evaluated filterTokens are not propagated 
+        // to subsequent filter files. NB this replicates current behaviour and seems to make sense. 
+        final Properties baseProps = new Properties();
+        baseProps.putAll( this.filterProperties );
+        
         for ( Iterator i = filters.iterator(); i.hasNext(); )
         {
             String filtersfile = (String) i.next();
             
             try
             {
-                Properties properties = PropertyUtils.loadPropertyFile( new File( filtersfile ), true, true );
+                Properties properties = PropertyUtils.loadPropertyFile( new File( filtersfile ), baseProps );
                 
                 filterProperties.putAll( properties );
             }

Added: maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/EnhancedPropertyUtilsTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/EnhancedPropertyUtilsTest.java?rev=584410&view=auto
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/EnhancedPropertyUtilsTest.java (added)
+++ maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/EnhancedPropertyUtilsTest.java Sat Oct 13 08:20:45 2007
@@ -0,0 +1,115 @@
+package org.apache.maven.plugin.resources;
+
+/*
+ * 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.io.File;
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * Tests {@link PropertyUtils#loadPropertyFile(File, Properties)}.
+ * 
+ * @author William Ferguson
+ */
+public class EnhancedPropertyUtilsTest
+    extends AbstractPropertyUtilsTest
+{
+    private static final String validationFileName =
+        "/target/test-classes/unit/propertiesutils-test/enhanced_validation.properties";
+
+    private static final String propFileName = "/target/test-classes/unit/propertiesutils-test/enhanced.properties";
+
+    private final Properties baseProps = new Properties();
+
+    protected void setUp()
+        throws Exception
+    {
+        super.setUp();
+        this.baseProps.setProperty( "prop1", "valueOfProperty1" );
+    }
+
+    protected File getPropertyFile()
+    {
+        final File propFile = new File( getBasedir(), propFileName );
+
+        if ( !propFile.exists() )
+        {
+            return null;
+        }
+
+        return propFile;
+    }
+
+    protected File getValidationFile()
+    {
+        final File file = new File( getBasedir(), validationFileName );
+
+        if ( !file.exists() )
+        {
+            return null;
+        }
+
+        return file;
+    }
+
+    /**
+     * Load property test case can be adjusted by modifying the enhanced.properties and enhanced_validation properties.
+     * 
+     * @throws Exception
+     */
+    public void testBasicLoadProperty()
+        throws Exception
+    {
+        final Properties props = PropertyUtils.loadPropertyFile( this.propertyFile, this.baseProps );
+
+        assertNotNull( props );
+        assertTrue( validateProperties( props ) );
+    }
+
+    /**
+     * Load property test case can be adjusted by modifying the enhanced.properties and enhanced_validation properties.
+     * 
+     * @throws Exception
+     */
+    public void testNonExistentProperty()
+        throws Exception
+    {
+        final Properties props = PropertyUtils.loadPropertyFile( this.propertyFile, this.baseProps );
+
+        assertNotNull( props );
+        assertNull( props.getProperty( "does_not_exist" ) );
+    }
+
+    /**
+     * Load property test case can be adjusted by modifying the enhanced.properties and enhanced_validation properties.
+     * 
+     * @throws Exception
+     */
+    public void testException()
+        throws Exception
+    {
+        try
+        {
+            PropertyUtils.loadPropertyFile( new File( "NON_EXISTENT_FILE" ), this.baseProps );
+            fail( "Should not have been able to load properties from a non-existent file" );
+        }
+        catch ( IOException e )
+        {
+            // as expected.
+        }
+    }
+}

Added: maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced.properties?rev=584410&view=auto
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced.properties (added)
+++ maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced.properties Sat Oct 13 08:20:45 2007
@@ -0,0 +1,8 @@
+key1=111
+key2=${prop1}
+key3=333_${key2}_${key1}_333
+key4=444_${key1}_444
+key5=555_${key4}_555
+key6=666_${key7}_666
+key7=777_${key8}_777
+key8=888
\ No newline at end of file

Added: maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced_validation.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced_validation.properties?rev=584410&view=auto
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced_validation.properties (added)
+++ maven/plugins/trunk/maven-resources-plugin/src/test/resources/unit/propertiesutils-test/enhanced_validation.properties Sat Oct 13 08:20:45 2007
@@ -0,0 +1,8 @@
+key1=111
+key2=valueOfProperty1
+key3=333_valueOfProperty1_111_333
+key4=444_111_444
+key5=555_444_111_444_555
+key6=666_777_888_777_666
+key7=777_888_777
+key8=888
\ No newline at end of file