maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rfscho...@apache.org
Subject svn commit: r1568368 - in /maven/shared/trunk/maven-filtering/src: main/java/org/apache/maven/shared/filtering/ test/java/org/apache/maven/shared/filtering/ test/units-files/maven-filename-filtering/
Date Fri, 14 Feb 2014 17:28:06 GMT
Author: rfscholte
Date: Fri Feb 14 17:28:06 2014
New Revision: 1568368

URL: http://svn.apache.org/r1568368
Log:
[MSHARED-220] Apply filtering to filenames
Patch contributed by Ton Swieb, verified and adjusted by Robert Scholte

Added:
    maven/shared/trunk/maven-filtering/src/test/units-files/maven-filename-filtering/
    maven/shared/trunk/maven-filtering/src/test/units-files/maven-filename-filtering/${pom.version}.txt
Modified:
    maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
    maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
    maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java

Modified: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java?rev=1568368&r1=1568367&r2=1568368&view=diff
==============================================================================
--- maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
(original)
+++ maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
Fri Feb 14 17:28:06 2014
@@ -19,25 +19,30 @@ package org.apache.maven.shared.filterin
  * under the License.
  */
 
+import java.io.File;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.Resource;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.utils.PathTool;
+import org.apache.maven.shared.utils.ReaderFactory;
+import org.apache.maven.shared.utils.StringUtils;
+import org.apache.maven.shared.utils.io.FileUtils;
+import org.apache.maven.shared.utils.io.FileUtils.FilterWrapper;
+import org.apache.maven.shared.utils.io.IOUtil;
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
-import org.apache.maven.shared.utils.io.FileUtils;
-import org.apache.maven.shared.utils.PathTool;
-import org.apache.maven.shared.utils.ReaderFactory;
 import org.codehaus.plexus.util.Scanner;
-import org.apache.maven.shared.utils.StringUtils;
 import org.sonatype.plexus.build.incremental.BuildContext;
 
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
 /**
  * @author Olivier Lamy
  * @plexus.component role="org.apache.maven.shared.filtering.MavenResourcesFiltering"
@@ -251,9 +256,8 @@ public class DefaultMavenResourcesFilter
 
                 File source = new File( resourceDirectory, name );
 
-                //File destinationFile = new File( outputDirectory, destination );
-
-                File destinationFile = getDestinationFile( outputDirectory, targetPath, name
);
+                File destinationFile =
+                    getDestinationFile( outputDirectory, targetPath, name, mavenResourcesExecution
);
 
                 boolean filteredExt =
                     filteredFileExtension( source.getName(), mavenResourcesExecution.getNonFilteredFileExtensions()
);
@@ -276,7 +280,8 @@ public class DefaultMavenResourcesFilter
 
             for ( String name : deletedFiles )
             {
-                File destinationFile = getDestinationFile( outputDirectory, targetPath, name
);
+                File destinationFile =
+                    getDestinationFile( outputDirectory, targetPath, name, mavenResourcesExecution
);
 
                 destinationFile.delete();
 
@@ -287,13 +292,19 @@ public class DefaultMavenResourcesFilter
 
     }
 
-    private File getDestinationFile( File outputDirectory, String targetPath, String name
)
+    private File getDestinationFile( File outputDirectory, String targetPath, String name,
MavenResourcesExecution mavenResourcesExecution )
+        throws MavenFilteringException
     {
         String destination = name;
+        
+        if ( mavenResourcesExecution.isFilterFilenames() && mavenResourcesExecution.getFilterWrappers().size()
> 0 )
+        {
+            destination = filterFileName( destination, mavenResourcesExecution.getFilterWrappers()
);
+        }
 
         if ( targetPath != null )
         {
-            destination = targetPath + "/" + name;
+            destination = targetPath + "/" + destination;
         }
 
         File destinationFile = new File( destination );
@@ -392,5 +403,38 @@ public class DefaultMavenResourcesFilter
 
         return relOutDir;
     }
+    
+    /*
+     * Filter the name of a file using the same mechanism for filtering the content of the
file.
+     */
+    private String filterFileName( String name, List<FilterWrapper> wrappers )
+        throws MavenFilteringException
+    {
+
+        Reader reader = new StringReader( name );
+        for ( FilterWrapper wrapper : wrappers )
+        {
+            reader = wrapper.getReader( reader );
+        }
+
+        StringWriter writer = new StringWriter();
+
+        try
+        {
+            IOUtil.copy( reader, writer );
+        }
+        catch ( IOException e )
+        {
+            throw new MavenFilteringException( "Failed filtering filename" + name, e );
+        }
+
+        String filteredFilename = writer.toString();
+
+        if ( getLogger().isDebugEnabled() )
+        {
+            getLogger().debug( "renaming filename " + name + " to " + filteredFilename );
+        }
+        return filteredFilename;
+    }
 
 }

Modified: maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java?rev=1568368&r1=1568367&r2=1568368&view=diff
==============================================================================
--- maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
(original)
+++ maven/shared/trunk/maven-filtering/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
Fri Feb 14 17:28:06 2014
@@ -56,6 +56,8 @@ public class MavenResourcesExecution
 
     private boolean useDefaultFilterWrappers = false;
 
+    private boolean filterFilenames = false;
+
     /**
      * Overwrite existing files even if the destination files are newer.
      * <code>false</code> by default.
@@ -350,6 +352,26 @@ public class MavenResourcesExecution
         this.includeEmptyDirs = includeEmptyDirs;
     }
 
+    /**
+     * 
+     * @return {@code true} if filenames are filtered, otherwise {@code false}
+     * @since 1.2
+     */
+    public boolean isFilterFilenames()
+    {
+        return filterFilenames;
+    }
+
+    /**
+     * 
+     * @param filterFilenames {@code true} if filenames should be filtered, otherwise {@code
false}
+     * @since 1.2
+     */
+    public void setFilterFilenames( boolean filterFilenames )
+    {
+        this.filterFilenames = filterFilenames;
+    }
+
     public MavenResourcesExecution copyOf()
     {
         MavenResourcesExecution mre = new MavenResourcesExecution();

Modified: maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java?rev=1568368&r1=1568367&r2=1568368&view=diff
==============================================================================
--- maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
(original)
+++ maven/shared/trunk/maven-filtering/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
Fri Feb 14 17:28:06 2014
@@ -808,5 +808,40 @@ public class DefaultMavenResourcesFilter
 
         assertTrue( nonFilteredResult.equals( expectedNonFilteredResult ) );
     }
+    
+    // MSHARED-220: Apply filtering to filenames
+    public void testFilterFileName()
+        throws Exception
+    {
+
+        File baseDir = new File( "/foo/bar" );
+        StubMavenProject mavenProject = new StubMavenProject( baseDir );
+        mavenProject.setVersion( "1.0" );
+        mavenProject.setGroupId( "org.apache" );
+        mavenProject.setName( "test project" );
+
+        MavenResourcesFiltering mavenResourcesFiltering =
+            (MavenResourcesFiltering) lookup( MavenResourcesFiltering.class.getName() );
+
+        String unitFilesDir = getBasedir() + "/src/test/units-files/maven-filename-filtering";
+
+        Resource resource = new Resource();
+        resource.setDirectory( unitFilesDir );
+        resource.setFiltering( true );
+        resource.addInclude( "${pom.version}*" );
+        resource.setTargetPath( "testTargetPath" );
+
+        MavenResourcesExecution mavenResourcesExecution =
+            new MavenResourcesExecution( Collections.singletonList( resource ), outputDirectory,
mavenProject, "UTF-8", Collections.<String>emptyList(),
+                                         Collections.<String>emptyList(), new StubMavenSession()
);
+        mavenResourcesExecution.setFilterFilenames( true );
+        mavenResourcesFiltering.filterResources( mavenResourcesExecution );
+
+        File targetPathFile = new File( outputDirectory, "testTargetPath" );
+
+        File[] files = targetPathFile.listFiles();
+        assertEquals( 1, files.length );
+        assertEquals( "1.0.txt", files[0].getName() );
+    }
 
 }

Added: maven/shared/trunk/maven-filtering/src/test/units-files/maven-filename-filtering/${pom.version}.txt
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-filtering/src/test/units-files/maven-filename-filtering/%24%7Bpom.version%7D.txt?rev=1568368&view=auto
==============================================================================
--- maven/shared/trunk/maven-filtering/src/test/units-files/maven-filename-filtering/${pom.version}.txt
(added)
+++ maven/shared/trunk/maven-filtering/src/test/units-files/maven-filename-filtering/${pom.version}.txt
Fri Feb 14 17:28:06 2014
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.



Mime
View raw message