You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gb...@apache.org on 2018/02/25 18:13:27 UTC

maven-archetype git commit: [ARCHETYPE-492] Underscore in filenames problematic due to greedy regex [Forced Update!]

Repository: maven-archetype
Updated Branches:
  refs/heads/ARCHETYPE-492 b515040da -> bb723acb4 (forced update)


[ARCHETYPE-492] Underscore in filenames problematic due to greedy regex

The pattern looking for tokens to replace in filenames should exactly
match __<something>__, where <something> has at least 1 character, and
neither starts nor ends with an underscore (but it can contain single
underscores in the middle).


Project: http://git-wip-us.apache.org/repos/asf/maven-archetype/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-archetype/commit/bb723acb
Tree: http://git-wip-us.apache.org/repos/asf/maven-archetype/tree/bb723acb
Diff: http://git-wip-us.apache.org/repos/asf/maven-archetype/diff/bb723acb

Branch: refs/heads/ARCHETYPE-492
Commit: bb723acb42efcffd335575499ac2569f98dbbaa9
Parents: 6116cd9
Author: Guillaume Boué <gb...@apache.org>
Authored: Sun Feb 25 18:13:37 2018 +0100
Committer: Guillaume Boué <gb...@apache.org>
Committed: Sun Feb 25 19:11:37 2018 +0100

----------------------------------------------------------------------
 .../DefaultFilesetArchetypeGenerator.java       | 65 +++++---------------
 .../META-INF/maven/archetype-metadata.xml       |  2 +
 ...erscored-2____property-with-default-1__.java | 14 +++++
 .../DefaultArchetypeGeneratorTest.java          |  6 ++
 .../archetype/test/ArchetypeGenerationTest.java |  2 +
 .../generate-12/archetype.properties.sample     |  2 +
 6 files changed, 43 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-archetype/blob/bb723acb/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java
----------------------------------------------------------------------
diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java b/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java
index b21e1a0..2a5851c 100644
--- a/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java
@@ -65,6 +65,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
+import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
@@ -87,15 +88,10 @@ public class DefaultFilesetArchetypeGenerator
     private VelocityComponent velocity;
 
     /**
-     * Token delimiter.
-     */
-    private static final String DELIMITER = "__";
-
-    /**
      * Pattern used to detect tokens in a string. Tokens are any text surrounded
-     * by the delimiter.
+     * by the delimiter <code>__</code>.
      */
-    private static final Pattern TOKEN_PATTERN = Pattern.compile( ".*" + DELIMITER + ".*" + DELIMITER + ".*" );
+    private static final Pattern TOKEN_PATTERN = Pattern.compile( "__((?:[^_]+_)*[^_]+)__" );
 
     public void generateArchetype( ArchetypeGenerationRequest request, File archetypeFile )
         throws UnknownArchetype, ArchetypeNotConfigured, ProjectDirectoryExists, PomFileExists, OutputFileExists,
@@ -365,16 +361,13 @@ public class DefaultFilesetArchetypeGenerator
             directory + "/" + ( packaged ? getPackageAsDirectory( packageName ) : "" ) + "/" + templateName.substring(
                 moduleOffset.length() );
 
-        if ( TOKEN_PATTERN.matcher( outputFileName ).matches() )
-        {
-            outputFileName = replaceFilenameTokens( outputFileName, context );
-        }
+        outputFileName = replaceFilenameTokens( outputFileName, context );
 
         return new File( outputDirectoryFile, outputFileName );
     }
 
     /**
-     * Replaces all tokens (text surrounded by the {@link #DELIMITER}) within
+     * Replaces all tokens (text matching {@link #TOKEN_PATTERN}) within
      * the given string, using properties contained within the context. If a
      * property does not exist in the context, the token is left unmodified
      * and a warning is logged.
@@ -384,62 +377,38 @@ public class DefaultFilesetArchetypeGenerator
      */
     private String replaceFilenameTokens( final String filePath, final Context context )
     {
-        String interpolatedResult = filePath;
-
-        int start = 0;
+        StringBuffer interpolatedResult = new StringBuffer();
+        Matcher matcher = TOKEN_PATTERN.matcher( filePath );
 
-        while ( true )
+        while ( matcher.find() )
         {
-            start = interpolatedResult.indexOf( DELIMITER, start );
-
-            if ( start == -1 )
-            {
-                break;
-            }
-
-            int end = interpolatedResult.indexOf( DELIMITER, start + DELIMITER.length() );
-
-            if ( end == -1 )
-            {
-                break;
-            }
-
-            String propertyToken = interpolatedResult.substring( start + DELIMITER.length(), end );
-
+            String propertyToken = matcher.group( 1 );
             String contextPropertyValue = (String) context.get( propertyToken );
-
             if ( contextPropertyValue != null && contextPropertyValue.trim().length() > 0 )
             {
-                String search = DELIMITER + propertyToken + DELIMITER;
-
                 if ( getLogger().isDebugEnabled() )
                 {
-                    getLogger().debug(
-                        "Replacing '" + search + "' in file path '" + interpolatedResult + "' with value '"
-                            + contextPropertyValue + "'." );
+                    getLogger().debug( "Replacing property '" + propertyToken + "' in file path '" + filePath
+                        + "' with value '" + contextPropertyValue + "'." );
                 }
-
-                interpolatedResult = StringUtils.replace( interpolatedResult, search, contextPropertyValue );
-
-                end = end + contextPropertyValue.length() - search.length();
+                matcher.appendReplacement( interpolatedResult, contextPropertyValue );
             }
             else
             {
                 // Need to skip the undefined property
-                getLogger().warn(
-                    "Property '" + propertyToken + "' was not specified, so the token in '" + interpolatedResult
-                        + "' is not being replaced." );
+                getLogger().warn( "Property '" + propertyToken + "' was not specified, so the token in '" + filePath
+                    + "' is not being replaced." );
             }
-
-            start = end + DELIMITER.length() + 1;
         }
 
+        matcher.appendTail( interpolatedResult );
+
         if ( getLogger().isDebugEnabled() )
         {
             getLogger().debug( "Final interpolated file path: '" + interpolatedResult + "'" );
         }
 
-        return interpolatedResult;
+        return interpolatedResult.toString();
     }
 
     private String getPackageInPathFormat( String aPackage )

http://git-wip-us.apache.org/repos/asf/maven-archetype/blob/bb723acb/archetype-common/src/test/archetypes/fileset-1.0/META-INF/maven/archetype-metadata.xml
----------------------------------------------------------------------
diff --git a/archetype-common/src/test/archetypes/fileset-1.0/META-INF/maven/archetype-metadata.xml b/archetype-common/src/test/archetypes/fileset-1.0/META-INF/maven/archetype-metadata.xml
index 6b2523a..e27a2ed 100644
--- a/archetype-common/src/test/archetypes/fileset-1.0/META-INF/maven/archetype-metadata.xml
+++ b/archetype-common/src/test/archetypes/fileset-1.0/META-INF/maven/archetype-metadata.xml
@@ -36,6 +36,8 @@
     <requiredProperty key="property-without-default-2"/>
     <requiredProperty key="property-without-default-3"/>
     <requiredProperty key="property-without-default-4"/>
+    <requiredProperty key="property_underscored_1"/>
+    <requiredProperty key="property_underscored-2"/>
   </requiredProperties>
 
   <fileSets>

http://git-wip-us.apache.org/repos/asf/maven-archetype/blob/bb723acb/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/M___artifactId_____property_underscored_1__Test__property_underscored-2____property-with-default-1__.java
----------------------------------------------------------------------
diff --git a/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/M___artifactId_____property_underscored_1__Test__property_underscored-2____property-with-default-1__.java b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/M___artifactId_____property_underscored_1__Test__property_underscored-2____property-with-default-1__.java
new file mode 100644
index 0000000..e6a5a6e
--- /dev/null
+++ b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/subproject/subsubproject/src/main/java/M___artifactId_____property_underscored_1__Test__property_underscored-2____property-with-default-1__.java
@@ -0,0 +1,14 @@
+groupId=${groupId}
+rootArtifactId=${rootArtifactId}
+artifactId=${artifactId}
+version=${version}
+package=${package}
+packageInPathFormat=${packageInPathFormat}
+property-without-default-1=${property-without-default-1}
+property-without-default-2=${property-without-default-2}
+property-without-default-3=${property-without-default-3}
+property-without-default-4=${property-without-default-4}
+property-with-default-1=${property-with-default-1}
+property-with-default-2=${property-with-default-2}
+property-with-default-3=${property-with-default-3}
+property-with-default-4=${property-with-default-4}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/maven-archetype/blob/bb723acb/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java
----------------------------------------------------------------------
diff --git a/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java b/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java
index 94ee058..191c449 100644
--- a/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java
+++ b/archetype-common/src/test/java/org/apache/maven/archetype/generator/DefaultArchetypeGeneratorTest.java
@@ -75,6 +75,8 @@ public class DefaultArchetypeGeneratorTest
         ADDITIONAL_PROPERTIES.setProperty( "property-with-default-2", "file-value" );
         ADDITIONAL_PROPERTIES.setProperty( "property-with-default-3", "file-value" );
         ADDITIONAL_PROPERTIES.setProperty( "property-with-default-4", "file-value" );
+        ADDITIONAL_PROPERTIES.setProperty( "property_underscored_1", "prop1" );
+        ADDITIONAL_PROPERTIES.setProperty( "property_underscored-2", "prop2" );
     }
 
     ArtifactRepository localRepository;
@@ -372,6 +374,10 @@ public class DefaultArchetypeGeneratorTest
                                                             /* + "file-value/inner/subsubproject/innest/" + */
                                                             + "ArbitraryProperty-file-value.java", "subsubproject" );
 
+        assertTemplateContentGeneratedWithFileSetArchetype( "subproject/subsubproject/src/main/java/file/value/package/"
+                                                            /* + "file-value/inner/subsubproject/innest/" + */
+                                                            + "M_subsubproject_prop1Testprop2file-value.java", "subsubproject" );
+
         // Test that undefined properties are safely ignored (and skipped)
         assertTemplateContentGeneratedWithFileSetArchetype( "subproject/subsubproject/src/main/java/file/value/package/"
                                                             /* + "file-value/inner/subsubproject/innest/" + */

http://git-wip-us.apache.org/repos/asf/maven-archetype/blob/bb723acb/archetype-common/src/test/java/org/apache/maven/archetype/test/ArchetypeGenerationTest.java
----------------------------------------------------------------------
diff --git a/archetype-common/src/test/java/org/apache/maven/archetype/test/ArchetypeGenerationTest.java b/archetype-common/src/test/java/org/apache/maven/archetype/test/ArchetypeGenerationTest.java
index f92c4da..3d7f0b1 100644
--- a/archetype-common/src/test/java/org/apache/maven/archetype/test/ArchetypeGenerationTest.java
+++ b/archetype-common/src/test/java/org/apache/maven/archetype/test/ArchetypeGenerationTest.java
@@ -100,6 +100,8 @@ public class ArchetypeGenerationTest
         archetypeRequiredProperties.setProperty( "property-without-default-2", "some-value-2" );
         archetypeRequiredProperties.setProperty( "property-without-default-3", "some-value-3" );
         archetypeRequiredProperties.setProperty( "property-without-default-4", "some-value-4" );
+        archetypeRequiredProperties.setProperty( "property_underscored_1", "prop1" );
+        archetypeRequiredProperties.setProperty( "property_underscored-2", "prop2" );
         agr.setProperties( archetypeRequiredProperties );
         agr.setProjectBuildingRequest( buildingRequest );
         

http://git-wip-us.apache.org/repos/asf/maven-archetype/blob/bb723acb/archetype-common/src/test/resources/projects/generate-12/archetype.properties.sample
----------------------------------------------------------------------
diff --git a/archetype-common/src/test/resources/projects/generate-12/archetype.properties.sample b/archetype-common/src/test/resources/projects/generate-12/archetype.properties.sample
index f9f4790..f59f773 100644
--- a/archetype-common/src/test/resources/projects/generate-12/archetype.properties.sample
+++ b/archetype-common/src/test/resources/projects/generate-12/archetype.properties.sample
@@ -32,3 +32,5 @@ property-with-default-1=file-value
 property-with-default-2=file-value
 property-with-default-3=file-value
 property-with-default-4=file-value
+property_underscored_1=prop1
+property_underscored-2=prop2