You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2021/01/24 21:53:55 UTC

[maven-filtering] branch MRESOURCES-250 created (now 5478362)

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a change to branch MRESOURCES-250
in repository https://gitbox.apache.org/repos/asf/maven-filtering.git.


      at 5478362  [MRESOURCES-250] - Add ability to flatten folder structure into target directory when copying resources

This branch includes the following new commits:

     new 5478362  [MRESOURCES-250] - Add ability to flatten folder structure into target directory when copying resources

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[maven-filtering] 01/01: [MRESOURCES-250] - Add ability to flatten folder structure into target directory when copying resources

Posted by sl...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch MRESOURCES-250
in repository https://gitbox.apache.org/repos/asf/maven-filtering.git

commit 5478362db48b5c15f883fe4e623a70df1ae5fc7d
Author: Michel Schudel <mi...@gmail.com>
AuthorDate: Wed Jan 1 16:32:37 2020 +0100

    [MRESOURCES-250] - Add ability to flatten folder structure into target directory when copying resources
    
    This change is the preparation to enabling the maven-resources-plugin to make use of a new version of the filtering library the allows the resource plugin to copy to copy resources to a flattened directory structure.
---
 .../filtering/DefaultMavenResourcesFiltering.java  | 27 ++++++-
 .../shared/filtering/MavenResourcesExecution.java  | 26 +++++++
 .../DefaultMavenResourcesFilteringTest.java        | 82 ++++++++++++++++++++++
 .../includedir/includefile.txt                     | 16 +++++
 4 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
index 4e06402..e5b2414 100644
--- a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
+++ b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFiltering.java
@@ -24,6 +24,8 @@ import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -247,6 +249,19 @@ public class DefaultMavenResourcesFiltering
 
                 File destinationFile = getDestinationFile( outputDirectory, targetPath, name, mavenResourcesExecution );
 
+                if ( mavenResourcesExecution.isFlatten() && destinationFile.exists() )
+                {
+                    if ( mavenResourcesExecution.isOverwrite() )
+                    {
+                        getLogger().warn( "existing file " + destinationFile.getName()
+                                + " will be overwritten by " + name );
+                    }
+                    else
+                    {
+                        throw new MavenFilteringException( "existing file " + destinationFile.getName()
+                                + " will be overwritten by " + name + " and overwrite was not set to true" );
+                    }
+                }
                 boolean filteredExt =
                     filteredFileExtension( source.getName(), mavenResourcesExecution.getNonFilteredFileExtensions() );
                 if ( resource.isFiltering() && isPropertiesFile( source ) )
@@ -379,7 +394,17 @@ public class DefaultMavenResourcesFiltering
                                      MavenResourcesExecution mavenResourcesExecution )
                                          throws MavenFilteringException
     {
-        String destination = name;
+        String destination;
+        if ( !mavenResourcesExecution.isFlatten() )
+        {
+            destination = name;
+        }
+        else
+        {
+            Path path = Paths.get( name );
+            Path filePath = path.getFileName();
+            destination = filePath.toString();
+        }
 
         if ( mavenResourcesExecution.isFilterFilenames() && mavenResourcesExecution.getFilterWrappers().size() > 0 )
         {
diff --git a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
index 6d68126..449e405 100644
--- a/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
+++ b/src/main/java/org/apache/maven/shared/filtering/MavenResourcesExecution.java
@@ -113,6 +113,12 @@ public class MavenResourcesExecution
     private boolean supportMultiLineFiltering;
 
     /**
+     * Write resources to a flattened directory structure.
+     *
+     */
+    private boolean flatten = false;
+
+    /**
      * Do nothing.
      */
     public MavenResourcesExecution()
@@ -385,6 +391,26 @@ public class MavenResourcesExecution
     }
 
     /**
+     * Write to flattened directory structure.
+     *
+     * @return {@link #flatten}
+     */
+    public boolean isFlatten()
+    {
+        return flatten;
+    }
+
+    /**
+     * Write to flattened directory structure.
+     *
+     * @param flatten flatten true or false.
+     */
+    public void setFlatten( boolean flatten )
+    {
+        this.flatten = flatten;
+    }
+
+    /**
      * Copy any empty directories included in the Resources.
      *
      * @return {@link #includeEmptyDirs}
diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
index 0a6061e..90633cb 100644
--- a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
+++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
@@ -440,6 +440,88 @@ public class DefaultMavenResourcesFilteringTest
 
     }
 
+    public void testFlattenDirectoryStructure()
+            throws Exception
+    {
+        File baseDir = new File( "c:\\foo\\bar" );
+        StubMavenProject mavenProject = new StubMavenProject( baseDir );
+        mavenProject.setVersion( "1.0" );
+        mavenProject.setGroupId( "org.apache" );
+        mavenProject.setName( "test project" );
+
+        MavenResourcesFiltering mavenResourcesFiltering = lookup( MavenResourcesFiltering.class );
+
+        String unitFilesDir = getBasedir() + "/src/test/units-files/maven-resources-filtering";
+
+        Resource resource = new Resource();
+        List<Resource> resources = new ArrayList<>();
+        resources.add( resource );
+        resource.setDirectory( unitFilesDir );
+        resource.setFiltering( true );
+        resource.addInclude( "includ*" );
+        resource.addInclude( "**/includ*" );
+
+        List<String> filtersFile = new ArrayList<>();
+        filtersFile.add( getBasedir()
+                + "/src/test/units-files/maven-resources-filtering/empty-maven-resources-filtering.txt" );
+
+        MavenResourcesExecution mavenResourcesExecution =
+                new MavenResourcesExecution( resources, outputDirectory, mavenProject, "UTF-8", filtersFile,
+                        Collections.<String> emptyList(), new StubMavenSession() );
+        mavenResourcesExecution.setFlatten(true);
+        mavenResourcesExecution.setOverwrite(true);
+        mavenResourcesFiltering.filterResources( mavenResourcesExecution );
+
+        File[] files = outputDirectory.listFiles();
+        assertNotNull( files );
+        assertEquals( 2, files.length );
+        File includeFile = new File( outputDirectory, "includefile.txt" );
+        assertTrue( includeFile.exists() );
+
+        includeFile = new File( outputDirectory, "include.txt" );
+        assertTrue( includeFile.exists() );
+
+    }
+
+    public void testFlattenDirectoryStructureWithoutOverride()
+            throws Exception
+    {
+        File baseDir = new File( "c:\\foo\\bar" );
+        StubMavenProject mavenProject = new StubMavenProject( baseDir );
+        mavenProject.setVersion( "1.0" );
+        mavenProject.setGroupId( "org.apache" );
+        mavenProject.setName( "test project" );
+
+        MavenResourcesFiltering mavenResourcesFiltering = lookup( MavenResourcesFiltering.class );
+
+        String unitFilesDir = getBasedir() + "/src/test/units-files/maven-resources-filtering";
+
+        Resource resource = new Resource();
+        List<Resource> resources = new ArrayList<>();
+        resources.add( resource );
+        resource.setDirectory( unitFilesDir );
+        resource.setFiltering( true );
+        resource.addInclude( "includ*" );
+        resource.addInclude( "**/includ*" );
+
+        List<String> filtersFile = new ArrayList<>();
+        filtersFile.add( getBasedir()
+                + "/src/test/units-files/maven-resources-filtering/empty-maven-resources-filtering.txt" );
+
+        MavenResourcesExecution mavenResourcesExecution =
+                new MavenResourcesExecution( resources, outputDirectory, mavenProject, "UTF-8", filtersFile,
+                        Collections.<String> emptyList(), new StubMavenSession() );
+        mavenResourcesExecution.setFlatten(true);
+        mavenResourcesExecution.setOverwrite(false);
+        try {
+            mavenResourcesFiltering.filterResources(mavenResourcesExecution);
+        } catch (MavenFilteringException e) {
+            return;
+        }
+        fail("Copying directory structure with duplicate filename includefile.txt should have failed with overwrite");
+
+    }
+
     public void testExcludeOneFile()
         throws Exception
     {
diff --git a/src/test/units-files/maven-resources-filtering/includedir/includefile.txt b/src/test/units-files/maven-resources-filtering/includedir/includefile.txt
new file mode 100644
index 0000000..13a8339
--- /dev/null
+++ b/src/test/units-files/maven-resources-filtering/includedir/includefile.txt
@@ -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.