You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jd...@apache.org on 2009/08/19 23:53:37 UTC

svn commit: r805992 - in /maven/plugins/trunk/maven-resources-plugin/src: main/java/org/apache/maven/plugin/resources/ResourcesMojo.java test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java

Author: jdcasey
Date: Wed Aug 19 21:53:36 2009
New Revision: 805992

URL: http://svn.apache.org/viewvc?rev=805992&view=rev
Log:
[MRESOURCES-79] Adding parameter 'extraFilters' to allow users to configure resources mojo separately from testResources mojo using default-resources and default-testResources executionIds (starting in Maven 2.2.0). The build/filters section of the POM will be used as 'global' filters, and the extraFilters parameter is added in FRONT of the build/filters entries (extraFilters have the opportunity to override filters from the build section of the POM).

Modified:
    maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
    maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java

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=805992&r1=805991&r2=805992&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 Wed Aug 19 21:53:36 2009
@@ -20,6 +20,7 @@
  */
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
@@ -84,14 +85,33 @@
     protected MavenProject project;
 
     /**
-     * The list of additional key-value pairs aside from that of the System,
-     * and that of the project, which would be used for the filtering.
+     * The list of additional filter properties files to be used along with System and project
+     * properties, which would be used for the filtering.
+     * <br/>
+     * See also: {@link ResourcesMojo#extraFilters}.
      *
      * @parameter expression="${project.build.filters}"
      */
     protected List filters;
     
     /**
+     * The list of extra filter properties files to be used along with System properties,
+     * project properties, and filter properties files specified in the POM build/filters section,
+     * which should be used for the filtering during the current mojo execution.
+     * <br/>
+     * Normally, these will be configured from a plugin's execution section, to provide a different
+     * set of filters for a particular execution. For instance, starting in Maven 2.2.0, you have the
+     * option of configuring executions with the id's <code>default-resources</code> and 
+     * <code>default-testResources</code> to supply different configurations for the two 
+     * different types of resources. By supplying <code>extraFilters</code> configurations, you
+     * can separate which filters are used for which type of resource.
+     *
+     * @parameter
+     * @since 2.4
+     */
+    protected List extraFilters;
+    
+    /**
      * 
      * @component role="org.apache.maven.shared.filtering.MavenResourcesFiltering" role-hint="default"
      * @required
@@ -136,7 +156,7 @@
     
     /**
      * Whether to escape backslashes and colons in windows-style paths.
-     * @parameter expression="${maven.resources.escapeWindowsPaths} default-value="false"
+     * @parameter expression="${maven.resources.escapeWindowsPaths} default-value="true"
      * @since 2.4
      */
     protected boolean escapeWindowsPaths;
@@ -159,6 +179,7 @@
                                    + ", i.e. build is platform dependent!" );
             }
             
+            List filters = getFilters();
 
             MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( getResources(), 
                                                                                            getOutputDirectory(),
@@ -187,6 +208,25 @@
         }
     }
     
+    protected List getFilters()
+    {
+        if ( extraFilters == null || extraFilters.isEmpty() )
+        {
+            return filters;
+        }
+        else
+        {
+            List result = new ArrayList( extraFilters );
+            
+            if ( filters != null && !filters.isEmpty() )
+            {
+                result.addAll( filters );
+            }
+            
+            return result;
+        }
+    }
+
     /**
      * Determines whether filtering has been enabled for any resource.
      * 

Modified: maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java?rev=805992&r1=805991&r2=805992&view=diff
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java (original)
+++ maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java Wed Aug 19 21:53:36 2009
@@ -29,15 +29,19 @@
 import java.util.Properties;
 
 import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.Mojo;
 import org.apache.maven.plugin.resources.stub.MavenProjectResourcesStub;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.shared.filtering.MavenFileFilter;
+import org.codehaus.plexus.configuration.PlexusConfiguration;
 import org.codehaus.plexus.util.FileUtils;
 
 public class ResourcesMojoTest
     extends AbstractMojoTestCase
 {
     protected final static String defaultPomFilePath = "/target/test-classes/unit/resources-test/plugin-config.xml";
+    
+    private MavenFileFilter mff;
 
     /**
      * test mojo lookup, test harness should be working fine
@@ -52,6 +56,22 @@
 
         assertNotNull( mojo );
     }
+    
+    public void tearDown()
+        throws Exception
+    {
+        if ( mff != null )
+        {
+            try
+            {
+                release( mff );
+            }
+            catch ( Exception e )
+            {}
+        }
+        
+        super.tearDown();
+    }
 
     /**
      * @throws Exception
@@ -390,6 +410,7 @@
         setVariableValueToObject( mojo, "resources", resources );
         setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
         setVariableValueToObject( mojo, "filters", new LinkedList() );
+        setVariableValueToObject( mojo, "escapeWindowsPaths", Boolean.TRUE );
         mojo.execute();
 
         String resourcesDir = project.getOutputDirectory();
@@ -432,6 +453,80 @@
     }
 
     /**
+     * @throws Exception
+     */
+    public void testPropertyFiles_Extra()
+        throws Exception
+    {
+        File testPom = new File( getBasedir(), defaultPomFilePath );
+        ResourcesMojo mojo = (ResourcesMojo) lookupMojo( "resources", testPom );
+        MavenProjectResourcesStub project = new MavenProjectResourcesStub( "resourcePropertyFiles_Extra" );
+        List resources = project.getBuild().getResources();
+        LinkedList filterList = new LinkedList();
+
+        assertNotNull( mojo );
+
+        project.addFile( "extra.properties", "current working directory=${dir}" );
+        project.addFile( "filter.properties", "dir:testdir" );
+        project.setResourceFiltering( 0, true );
+        project.setupBuildEnvironment();
+        filterList.add( project.getResourcesDirectory() + "filter.properties" );
+
+        // setVariableValueToObject(mojo,"encoding","UTF-8");
+        setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "resources", resources );
+        setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
+        setVariableValueToObject( mojo, "extraFilters", filterList );
+        mojo.execute();
+
+        String resourcesDir = project.getOutputDirectory();
+        String checkString = "current working directory=testdir";
+
+        assertContent( resourcesDir + "/extra.properties", checkString );
+    }
+
+    /**
+     * @throws Exception
+     */
+    public void testPropertyFiles_MainAndExtra()
+        throws Exception
+    {
+        File testPom = new File( getBasedir(), defaultPomFilePath );
+        ResourcesMojo mojo = (ResourcesMojo) lookupMojo( "resources", testPom );
+        MavenProjectResourcesStub project = new MavenProjectResourcesStub( "resourcePropertyFiles_MainAndExtra" );
+        List resources = project.getBuild().getResources();
+        LinkedList filterList = new LinkedList();
+        LinkedList extraFilterList = new LinkedList();
+
+        assertNotNull( mojo );
+
+        project.addFile( "main-extra.properties", "current working directory=${dir}; old working directory=${dir2}" );
+        project.addFile( "filter.properties", "dir:testdir" );
+        project.addFile( "extra-filter.properties", "dir2:testdir2" );
+        project.setResourceFiltering( 0, true );
+        
+        project.cleanBuildEnvironment();
+        project.setupBuildEnvironment();
+        
+        filterList.add( project.getResourcesDirectory() + "filter.properties" );
+        extraFilterList.add( project.getResourcesDirectory() + "extra-filter.properties" );
+
+        // setVariableValueToObject(mojo,"encoding","UTF-8");
+        setVariableValueToObject( mojo, "project", project );
+        setVariableValueToObject( mojo, "resources", resources );
+        setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
+        setVariableValueToObject( mojo, "filters", filterList );
+        setVariableValueToObject( mojo, "extraFilters", extraFilterList );
+        mojo.execute();
+
+        String resourcesDir = project.getOutputDirectory();
+        String checkString = "current working directory=testdir; old working directory=testdir2";
+
+        File file = new File( resourcesDir, "main-extra.properties" );
+        assertContent( file.getAbsolutePath(), checkString );
+    }
+
+    /**
      * Validates that a Filter token containing a project property will be resolved before the Filter is applied to the
      * resources.
      * 
@@ -549,29 +644,9 @@
         setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
         setVariableValueToObject( mojo, "filters", new LinkedList() );
 
-        MavenFileFilter mff = null;
-        try
-        {
-            mff = (MavenFileFilter) lookup( MavenFileFilter.class.getName(), "default" );
-            setVariableValueToObject( mojo, "mavenFileFilter", mff );
-
-            setVariableValueToObject( mojo, "escapeWindowsPaths", Boolean.TRUE );
+        setVariableValueToObject( mojo, "escapeWindowsPaths", Boolean.TRUE );
 
-            mojo.execute();
-        }
-        finally
-        {
-            if ( mff != null )
-            {
-                try
-                {
-                    release( mff );
-                }
-                catch ( Exception e )
-                {
-                }
-            }
-        }
+        mojo.execute();
 
         String resourcesDir = project.getOutputDirectory();
 
@@ -595,4 +670,49 @@
 
         assertEquals( data, new BufferedReader( new FileReader( fileName ) ).readLine() );
     }
+
+    protected Mojo lookupEmptyMojo( String goal, File pom )
+        throws Exception
+    {
+        return setupComponents( super.lookupEmptyMojo( goal, pom ) );
+    }
+
+    protected Mojo lookupEmptyMojo( String goal, String pluginPom )
+        throws Exception
+    {
+        return setupComponents( super.lookupEmptyMojo( goal, pluginPom ) );
+    }
+
+    protected Mojo lookupMojo( String goal, File pom )
+        throws Exception
+    {
+        return setupComponents( super.lookupMojo( goal, pom ) );
+    }
+
+    protected Mojo lookupMojo( String groupId, String artifactId, String version, String goal,
+                               PlexusConfiguration pluginConfiguration )
+        throws Exception
+    {
+        return setupComponents( super.lookupMojo( groupId, artifactId, version, goal, pluginConfiguration ) );
+    }
+
+    protected Mojo lookupMojo( String goal, String pluginPom )
+        throws Exception
+    {
+        return setupComponents( super.lookupMojo( goal, pluginPom ) );
+    }
+    
+
+    private synchronized Mojo setupComponents( Mojo mojo )
+        throws Exception
+    {
+        if ( mff == null )
+        {
+            mff = (MavenFileFilter) lookup( MavenFileFilter.class.getName(), "default" );
+        }
+        
+        setVariableValueToObject( mojo, "mavenFileFilter", mff );
+        
+        return mojo;
+    }
 }