You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ol...@apache.org on 2008/03/01 02:03:48 UTC

svn commit: r632522 - in /maven/plugins/branches/MRESOURCES-56: ./ src/main/java/org/apache/maven/plugin/resources/ src/test/java/org/apache/maven/plugin/resources/

Author: olamy
Date: Fri Feb 29 17:03:46 2008
New Revision: 632522

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


Modified:
    maven/plugins/branches/MRESOURCES-56/pom.xml
    maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java
    maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java
    maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
    maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java
    maven/plugins/branches/MRESOURCES-56/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java

Modified: maven/plugins/branches/MRESOURCES-56/pom.xml
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MRESOURCES-56/pom.xml?rev=632522&r1=632521&r2=632522&view=diff
==============================================================================
--- maven/plugins/branches/MRESOURCES-56/pom.xml (original)
+++ maven/plugins/branches/MRESOURCES-56/pom.xml Fri Feb 29 17:03:46 2008
@@ -24,7 +24,7 @@
   <parent>
     <artifactId>maven-plugins</artifactId>
     <groupId>org.apache.maven.plugins</groupId>
-    <version>8</version>
+    <version>11-SNAPSHOT</version>
   </parent>
 
   <artifactId>maven-resources-plugin</artifactId>
@@ -71,6 +71,12 @@
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
       <version>1.4.1</version>
+    </dependency>
+    
+    <dependency>
+      <groupId>org.apache.maven.shared</groupId>
+      <artifactId>maven-filtering</artifactId>
+      <version>1.0-alpha-1-SNAPSHOT</version>
     </dependency>
 
     <dependency>

Modified: maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java?rev=632522&r1=632521&r2=632522&view=diff
==============================================================================
--- maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java (original)
+++ maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/PropertyUtils.java Fri Feb 29 17:03:46 2008
@@ -30,6 +30,8 @@
 
 
 /**
+ * @deprecated use classes in the component maven-filtering
+ * TODO remove the class ?
  * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
  * @author William Ferguson
  * @version $Id$

Modified: maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java?rev=632522&r1=632521&r2=632522&view=diff
==============================================================================
--- maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java (original)
+++ maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ReflectionProperties.java Fri Feb 29 17:03:46 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/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java?rev=632522&r1=632521&r2=632522&view=diff
==============================================================================
--- maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java (original)
+++ maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/ResourcesMojo.java Fri Feb 29 17:03:46 2008
@@ -19,21 +19,17 @@
  * under the License.
  */
 
-import org.apache.maven.model.Resource;
+import java.io.File;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.maven.execution.MavenSession;
 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 java.io.File;
-import java.io.IOException;
-import java.io.Reader;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
+import org.apache.maven.shared.filtering.MavenFilteringException;
+import org.apache.maven.shared.filtering.MavenResourcesExecution;
+import org.apache.maven.shared.filtering.MavenResourcesFiltering;
 
 /**
  * Copy resources for the main source code to the main output directory.
@@ -55,7 +51,7 @@
      *
      * @parameter
      */
-    private String encoding;
+    protected String encoding;
 
     /**
      * The output directory into which to copy the resources.
@@ -78,13 +74,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,
@@ -92,179 +82,36 @@
      *
      * @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 ( encoding == null || encoding.length() < 1 )
+        try
         {
-            getLog().info( "Using default encoding to copy filtered resources." );
+            MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( resources, outputDirectory,
+                                                                                           project, encoding, filters,
+                                                                                           Collections.EMPTY_LIST,
+                                                                                           session );
+            mavenResourcesFiltering.filterResources( mavenResourcesExecution );
         }
-        else
+        catch ( MavenFilteringException e )
         {
-            getLog().info( "Using '" + encoding + "' to copy filtered resources." );
-        }
-
-        for ( Iterator i = resources.iterator(); i.hasNext(); )
-        {
-            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 " + 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 );
-                }
-            }
-        }
-    }
-
-    private void initializeFiltering()
-        throws MojoExecutionException
-    {
-        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();
-
-            try
-            {
-                Properties properties = PropertyUtils.loadPropertyFile( new File( filtersfile ), baseProps );
-
-                filterProperties.putAll( properties );
-            }
-            catch ( IOException e )
-            {
-                throw new MojoExecutionException( "Error loading property file '" + filtersfile + "'", e );
-            }
-        }
-    }
-
-    private void copyFile( File from, final File to, boolean filtering )
-        throws IOException
-    {
-        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), "${", "}");
-                        }
-                    }
-            };
+            throw new MojoExecutionException( e.getMessage(), e );
         }
-        FileUtils.copyFile(from, to, encoding, wrappers);
     }
 }

Modified: maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java?rev=632522&r1=632521&r2=632522&view=diff
==============================================================================
--- maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java (original)
+++ maven/plugins/branches/MRESOURCES-56/src/main/java/org/apache/maven/plugin/resources/TestResourcesMojo.java Fri Feb 29 17:03:46 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;
 
@@ -55,7 +58,18 @@
     public void execute()
         throws MojoExecutionException
     {
-        copyResources( resources, outputDirectory );
+        try
+        {
+            MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( resources, outputDirectory,
+                                                                                           project, encoding, filters,
+                                                                                           Collections.EMPTY_LIST,
+                                                                                           session );
+            mavenResourcesFiltering.filterResources( mavenResourcesExecution );
+        }
+        catch ( MavenFilteringException e )
+        {
+            throw new MojoExecutionException( e.getMessage(), e );
+        }
     }
 
 }

Modified: maven/plugins/branches/MRESOURCES-56/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java
URL: http://svn.apache.org/viewvc/maven/plugins/branches/MRESOURCES-56/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java?rev=632522&r1=632521&r2=632522&view=diff
==============================================================================
--- maven/plugins/branches/MRESOURCES-56/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java (original)
+++ maven/plugins/branches/MRESOURCES-56/src/test/java/org/apache/maven/plugin/resources/ResourcesMojoTest.java Fri Feb 29 17:03:46 2008
@@ -26,6 +26,7 @@
 import java.util.LinkedList;
 import java.util.List;
 
+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;
@@ -312,6 +313,8 @@
         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();