Author: olamy
Date: Fri Sep  5 13:53:01 2008
New Revision: 692545

URL: http://svn.apache.org/viewvc?rev=692545&view=rev
Log:
[MRESOURCES-56] use (and release) the maven-filtering component

svn merge -r 632504:692535 
https://svn.apache.org/repos/asf/maven/plugins/branches/MRESOURCES-56

Modified:
    maven/plugins/trunk/maven-resources-plugin/   (props changed)
    maven/plugins/trunk/maven-resources-plugin/pom.xml
    
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/ReflectionProperties.java
    
maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
    
maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java
    
maven/plugins/trunk/maven-resources-plugin/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java

Propchange: maven/plugins/trunk/maven-resources-plugin/
------------------------------------------------------------------------------
    svn:mergeinfo = /maven/plugins/branches/MRESOURCES-56:632505-692535

Modified: maven/plugins/trunk/maven-resources-plugin/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/pom.xml?rev=692545&r1=692544&r2=692545&view=diff
==============================================================================
--- maven/plugins/trunk/maven-resources-plugin/pom.xml (original)
+++ maven/plugins/trunk/maven-resources-plugin/pom.xml Fri Sep  5 13:53:01 2008
@@ -70,7 +70,13 @@
     <dependency>
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
-      <version>1.5.1</version>
+      <version>1.5.5</version>
+    </dependency>
+    
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-filtering</artifactId>
+      <version>1.0-beta-1</version>
     </dependency>
 
     <dependency>

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=692545&r1=692544&r2=692545&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
 Fri Sep  5 13:53:01 2008
@@ -30,6 +30,8 @@
 
 
 /**
+ * @deprecated use classes in the component maven-filtering
+ * TODO remove the class ?
  * @author <a href="mailto:[EMAIL PROTECTED]">Kenney Westerhof</a>
  * @author William Ferguson
  * @version $Id$

Modified: 
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-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java?rev=692545&r1=692544&r2=692545&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java
 (original)
+++ 
maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java
 Fri Sep  5 13:53:01 2008
@@ -27,6 +27,8 @@
 
 
 /**
+ * @deprecated use classes in the component maven-filtering
+ * TODO remove the class ?
  * @author Andreas Hoheneder (ahoh_at_inode.at)
  * @version $Id$
  */

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=692545&r1=692544&r2=692545&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
 Fri Sep  5 13:53:01 2008
@@ -19,25 +19,23 @@
  * under the License.
  */
 
+import java.io.File;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.Resource;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.project.MavenProject;
-import org.codehaus.plexus.util.DirectoryScanner;
-import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.InterpolationFilterReader;
+import org.apache.maven.shared.filtering.MavenFilteringException;
+import org.apache.maven.shared.filtering.MavenResourcesExecution;
+import org.apache.maven.shared.filtering.MavenResourcesFiltering;
 import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.StringUtils;
 
-import java.io.File;
-import java.io.IOException;
-import java.io.Reader;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-
 /**
  * Copy resources for the main source code to the main output directory.
  *
@@ -58,7 +56,7 @@
      *
      * @parameter expression="${encoding}" 
default-value="${project.build.sourceEncoding}"
      */
-    private String encoding;
+    protected String encoding;
 
     /**
      * The output directory into which to copy the resources.
@@ -81,13 +79,7 @@
      * @required
      * @readonly
      */
-    private MavenProject project;
-
-    private Properties filterProperties;
-
-    private static final String[] EMPTY_STRING_ARRAY = {};
-
-    private static final String[] DEFAULT_INCLUDES = {"**/**"};
+    protected MavenProject project;
 
     /**
      * The list of additional key-value pairs aside from that of the System,
@@ -95,112 +87,48 @@
      *
      * @parameter expression="${project.build.filters}"
      */
-    private List filters;
+    protected List filters;
+    
+    /**
+     * 
+     * @component 
role="org.apache.maven.shared.filtering.MavenResourcesFiltering" 
role-hint="default"
+     * @required
+     */    
+    protected MavenResourcesFiltering mavenResourcesFiltering;    
+    
+    /**
+     * @parameter expression="${session}"
+     * @readonly
+     * @required
+     */
+    protected MavenSession session;   
 
     public void execute()
         throws MojoExecutionException
     {
-        copyResources( resources, outputDirectory );
-    }
-
-    protected void copyResources( List resources, File outputDirectory )
-        throws MojoExecutionException
-    {
-        initializeFiltering();
-
-        if ( StringUtils.isEmpty( encoding ) && isFilteringEnabled( resources 
) )
+        try
         {
-            getLog().warn(
-                           "File encoding has not been set, using platform 
encoding " + ReaderFactory.FILE_ENCODING
-                               + ", i.e. build is platform dependent!" );
+            
+            if ( StringUtils.isEmpty( encoding ) && isFilteringEnabled( 
getResources() ) )
+            {
+                getLog().warn(
+                               "File encoding has not been set, using platform 
encoding " + ReaderFactory.FILE_ENCODING
+                                   + ", i.e. build is platform dependent!" );
+            }
+            
+            
+            MavenResourcesExecution mavenResourcesExecution = new 
MavenResourcesExecution( getResources(), getOutputDirectory(),
+                                                                               
            project, encoding, filters,
+                                                                               
            Collections.EMPTY_LIST,
+                                                                               
            session );
+            mavenResourcesFiltering.filterResources( mavenResourcesExecution );
         }
-
-        for ( Iterator i = resources.iterator(); i.hasNext(); )
+        catch ( MavenFilteringException e )
         {
-            Resource resource = (Resource) i.next();
-
-            String targetPath = resource.getTargetPath();
-
-            File resourceDirectory = new File( resource.getDirectory() );
-            if ( !resourceDirectory.isAbsolute() )
-            {
-                resourceDirectory = new File( project.getBasedir(), 
resourceDirectory.getPath() );
-            }
-
-            if ( !resourceDirectory.exists() )
-            {
-                getLog().info( "Resource directory does not exist: " + 
resourceDirectory );
-                continue;
-            }
-
-            // this part is required in case the user specified "../something" 
as destination
-            // see MNG-1345
-            if ( !outputDirectory.exists() )
-            {
-                if ( !outputDirectory.mkdirs() )
-                {
-                    throw new MojoExecutionException( "Cannot create resource 
output directory: " + outputDirectory );
-                }
-            }
-
-            DirectoryScanner scanner = new DirectoryScanner();
-
-            scanner.setBasedir( resourceDirectory );
-            if ( resource.getIncludes() != null && 
!resource.getIncludes().isEmpty() )
-            {
-                scanner.setIncludes( (String[]) 
resource.getIncludes().toArray( EMPTY_STRING_ARRAY ) );
-            }
-            else
-            {
-                scanner.setIncludes( DEFAULT_INCLUDES );
-            }
-
-            if ( resource.getExcludes() != null && 
!resource.getExcludes().isEmpty() )
-            {
-                scanner.setExcludes( (String[]) 
resource.getExcludes().toArray( EMPTY_STRING_ARRAY ) );
-            }
-
-            scanner.addDefaultExcludes();
-            scanner.scan();
-
-            List includedFiles = Arrays.asList( scanner.getIncludedFiles() );
-
-            getLog().info( "Copying " + ( resource.isFiltering() ? "and 
filtering " : "")
-                + includedFiles.size() + " resource" + ( includedFiles.size() 
!= 1 ? "s" : "" )
-                + ( targetPath == null ? "" : " to " + targetPath ) );
-
-            for ( Iterator j = includedFiles.iterator(); j.hasNext(); )
-            {
-                String name = (String) j.next();
-
-                String destination = name;
-
-                if ( targetPath != null )
-                {
-                    destination = targetPath + "/" + name;
-                }
-
-                File source = new File( resourceDirectory, name );
-
-                File destinationFile = new File( outputDirectory, destination 
);
-
-                if ( !destinationFile.getParentFile().exists() )
-                {
-                    destinationFile.getParentFile().mkdirs();
-                }
-
-                try
-                {
-                    copyFile( source, destinationFile, resource.isFiltering() 
);
-                }
-                catch ( IOException e )
-                {
-                    throw new MojoExecutionException( "Error copying resource 
" + source, e );
-                }
-            }
+            throw new MojoExecutionException( e.getMessage(), e );
         }
     }
-
+    
     /**
      * Determines whether filtering has been enabled for any resource.
      * 
@@ -223,71 +151,25 @@
         return false;
     }
 
-    private void initializeFiltering()
-        throws MojoExecutionException
+    public List getResources()
     {
-        filterProperties = new Properties();
-
-        // System properties
-        filterProperties.putAll( System.getProperties() );
-
-        // 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();
+        return resources;
+    }
 
-            try
-            {
-                Properties properties = PropertyUtils.loadPropertyFile( new 
File( filtersfile ), baseProps );
+    public void setResources( List resources )
+    {
+        this.resources = resources;
+    }
 
-                filterProperties.putAll( properties );
-            }
-            catch ( IOException e )
-            {
-                throw new MojoExecutionException( "Error loading property file 
'" + filtersfile + "'", e );
-            }
-        }
+    public File getOutputDirectory()
+    {
+        return outputDirectory;
     }
 
-    private void copyFile( File from, final File to, boolean filtering )
-        throws IOException
+    public void setOutputDirectory( File outputDirectory )
     {
-        FileUtils.FilterWrapper[] wrappers = null;
-        if (filtering) {
-            wrappers = new FileUtils.FilterWrapper[]{
-                    // support ${token}
-                    new FileUtils.FilterWrapper() {
-                        public Reader getReader(Reader reader) {
-                            return new InterpolationFilterReader(reader, 
filterProperties, "${", "}");
-                        }
-                    },
-                    // support @token@
-                    new FileUtils.FilterWrapper() {
-                        public Reader getReader(Reader reader) {
-                            return new InterpolationFilterReader(reader, 
filterProperties, "@", "@");
-                        }
-                    },
-
-                    new FileUtils.FilterWrapper() {
-                        public Reader getReader(Reader reader) {
-                            boolean isPropertiesFile = false;
-
-                            if (to.isFile() && 
to.getName().endsWith(".properties")) {
-                                isPropertiesFile = true;
-                            }
-
-                            return new InterpolationFilterReader(reader, new 
ReflectionProperties(project, isPropertiesFile), "${", "}");
-                        }
-                    }
-            };
-        }
-        FileUtils.copyFile(from, to, encoding, wrappers);
+        this.outputDirectory = outputDirectory;
     }
+    
+    
 }

Modified: 
maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java?rev=692545&r1=692544&r2=692545&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-resources-plugin/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java
 Fri Sep  5 13:53:01 2008
@@ -20,7 +20,10 @@
  */
 
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.shared.filtering.MavenFilteringException;
+import org.apache.maven.shared.filtering.MavenResourcesExecution;
 
+import java.util.Collections;
 import java.util.List;
 import java.io.File;
 
@@ -52,10 +55,24 @@
      */
     private List resources;
 
-    public void execute()
-        throws MojoExecutionException
+    public File getOutputDirectory()
     {
-        copyResources( resources, outputDirectory );
+        return outputDirectory;
+    }
+
+    public void setOutputDirectory( File outputDirectory )
+    {
+        this.outputDirectory = outputDirectory;
+    }
+
+    public List getResources()
+    {
+        return resources;
+    }
+
+    public void setResources( List resources )
+    {
+        this.resources = resources;
     }
 
 }

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=692545&r1=692544&r2=692545&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
 Fri Sep  5 13:53:01 2008
@@ -21,11 +21,14 @@
 
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Properties;
 
+import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.resources.stub.MavenProjectResourcesStub;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.codehaus.plexus.util.FileUtils;
@@ -303,7 +306,7 @@
 
         assertNotNull( mojo );
 
-        project.addFile( "file4.txt", "current working directory = 
${user.dir}" );
+        project.addFile( "file4.txt", "current-working-directory = 
${user.dir}" );
         project.setResourceFiltering( 0, true );
         project.setupBuildEnvironment();
 
@@ -312,12 +315,20 @@
         setVariableValueToObject( mojo, "resources", resources );
         setVariableValueToObject( mojo, "outputDirectory", new File( 
project.getBuild().getOutputDirectory() ) );
         setVariableValueToObject( mojo, "filters", new LinkedList() );
+        MavenSession mavenSession = new MavenSession( null, null, null, null, 
null, null, null, System.getProperties(), null );
+        setVariableValueToObject( mojo, "session", mavenSession );
         mojo.execute();
 
         String resourcesDir = project.getOutputDirectory();
-        String checkString = "current working directory = " + (String) 
System.getProperty( "user.dir" );
 
-        assertContent( resourcesDir + "/file4.txt", checkString );
+        File userDir = new File( System.getProperty( "user.dir" ) );
+        assertTrue( userDir.exists() );
+        
+        Properties props = new Properties();
+        props.load( new FileInputStream( new File( resourcesDir + "/file4.txt" 
) ) );
+        File fileFromFiltering = new File( props.getProperty( 
"current-working-directory" ) );
+        assertTrue( fileFromFiltering.exists() );
+        assertEquals( userDir.getAbsolutePath(), 
fileFromFiltering.getAbsolutePath() );
     }
 
     /**
@@ -450,6 +461,7 @@
         setVariableValueToObject(mojo, "filters", filterList);
         mojo.execute();
         final String resourcesDir = project.getOutputDirectory();
+        
         final String checkString = "current working directory=foobar";
 
         assertContent(resourcesDir + "/file4.properties", checkString);


Reply via email to