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

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


The following commit(s) were added to refs/heads/master by this push:
     new bef789da Issue #274 Only strip the fileSet directory after the 
moduleOffset prefix
bef789da is described below

commit bef789daa5e37fc918110db2e2edb5cc07ba9c5e
Author: Arturo Bernal <[email protected]>
AuthorDate: Wed Oct 28 10:54:09 2026 +0100

    Issue #274 Only strip the fileSet directory after the moduleOffset prefix
    
    getOutputFile() computed each generated file's path with
    StringUtils.replaceOnce(template, directory, ""), a plain substring
    search for the fileSet's directory inside the full template path. For a
    module whose own directory name happens to contain the fileSet
    directory as a substring -- e.g. module dir "demoapi" and fileSet
    directory "api" -- replaceOnce matches "api" inside "demoapi" instead
    of the real "api" path segment that follows it, producing a corrupted
    output path ("demo" instead of "demoapi", with "api" duplicated as a
    separate directory).
    
    The same defect also crashes outright: the "create an empty directory
    even if there's no file to process" call (ARCHETYPE-57) passes
    moduleOffset itself as the template argument. When that moduleOffset
    string also contains the fileSet directory as a substring, replaceOnce
    removes it from moduleOffset, leaving a String shorter than
    moduleOffset.length() for the subsequent .substring(moduleOffset
    .length()) call, throwing StringIndexOutOfBoundsException and failing
    the whole generation.
    
    directory is only meaningful once the moduleOffset prefix has already
    been accounted for, so the search for it must not start before that
    point. getOutputFile() now searches for directory starting at
    moduleOffset.length() instead of from the beginning of the string,
    which finds the real path segment unambiguously regardless of what
    moduleOffset itself contains, and leaves the non-module case (empty
    moduleOffset) unchanged.
    
    Added a "demoapi" module with a nested "api" fileSet to the fileset-1.0
    test archetype used by DefaultArchetypeGeneratorTest, reproducing the
    issue's module-directory/fileSet-directory substring collision.
    Confirmed it fails with the reported StringIndexOutOfBoundsException on
    unfixed code and passes with the fix. Full archetype-common test suite,
    checkstyle and spotless all still pass.
---
 .../generator/DefaultFilesetArchetypeGenerator.java         | 11 ++++++++++-
 .../fileset-1.0/META-INF/maven/archetype-metadata.xml       | 13 +++++++++++++
 .../archetype-resources/demoapi/api/Marker.properties       |  5 +++++
 .../fileset-1.0/archetype-resources/{ => demoapi}/pom.xml   | 13 +++++++------
 .../test/archetypes/fileset-1.0/archetype-resources/pom.xml |  1 +
 .../archetype/generator/DefaultArchetypeGeneratorTest.java  | 13 +++++++++++++
 6 files changed, 49 insertions(+), 7 deletions(-)

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 778c2913..0914ca21 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
@@ -342,7 +342,16 @@ public class DefaultFilesetArchetypeGenerator implements 
FilesetArchetypeGenerat
             String packageName,
             String moduleOffset,
             Context context) {
-        String templateName = StringUtils.replaceOnce(template, directory, "");
+        // directory is only meaningful once the moduleOffset prefix has been 
accounted for, so the search
+        // for it must not start before that point: a plain replaceOnce can 
otherwise match directory inside
+        // moduleOffset itself (e.g. fileSet directory "api" found inside 
module directory "foo-openapi"),
+        // corrupting the computed path or, if the match leaves fewer 
characters than moduleOffset is long,
+        // throwing a StringIndexOutOfBoundsException below.
+        int searchFrom = moduleOffset == null ? 0 : moduleOffset.length();
+        int directoryIndex = template.indexOf(directory, searchFrom);
+        String templateName = directoryIndex < 0
+                ? template
+                : template.substring(0, directoryIndex) + 
template.substring(directoryIndex + directory.length());
 
         String outputFileName = directory + "/" + (packaged ? 
getPackageAsDirectory(packageName) : "") + "/"
                 + templateName.substring(moduleOffset.length());
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 e27a2ed1..9998ac73 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
@@ -118,6 +118,19 @@
         </module>
       </modules>
     </module>
+
+    <!-- Regression test for a module directory that contains a fileSet 
directory name as a
+         substring (e.g. "demoapi" contains "api"), see GitHub issue #274. -->
+    <module name="demoapi" id="demoapi" dir="demoapi">
+      <fileSets>
+        <fileSet filtered="true" packaged="false">
+          <directory>api</directory>
+          <includes>
+            <include>**/*.properties</include>
+          </includes>
+        </fileSet>
+      </fileSets>
+    </module>
   </modules>
 
 </archetype-descriptor>
diff --git 
a/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/demoapi/api/Marker.properties
 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/demoapi/api/Marker.properties
new file mode 100644
index 00000000..6e7d4f2a
--- /dev/null
+++ 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/demoapi/api/Marker.properties
@@ -0,0 +1,5 @@
+groupId=${groupId}
+artifactId=${artifactId}
+version=${version}
+package=${package}
+packageInPathFormat=${packageInPathFormat}
diff --git 
a/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/demoapi/pom.xml
similarity index 85%
copy from 
archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml
copy to 
archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/demoapi/pom.xml
index 0cae4563..798a2f4b 100644
--- 
a/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml
+++ 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/demoapi/pom.xml
@@ -24,15 +24,16 @@
 
   <modelVersion>4.0.0</modelVersion>
 
+  <parent>
+    <groupId>${groupId}</groupId>
+    <artifactId>${parentArtifactId}</artifactId>
+    <version>${version}</version>
+  </parent>
+
   <groupId>${groupId}</groupId>
   <artifactId>${artifactId}</artifactId>
   <version>${version}</version>
 
-  <name>Maven archetype Test</name>
-  <packaging>pom</packaging>
-
-  <modules>
-    <module>subproject</module>
-  </modules>
+  <name>Maven archetype Test Module Directory Name Substring Match</name>
 
 </project>
diff --git 
a/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml
index 0cae4563..4531fbec 100644
--- 
a/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml
+++ 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/pom.xml
@@ -33,6 +33,7 @@
 
   <modules>
     <module>subproject</module>
+    <module>demoapi</module>
   </modules>
 
 </project>
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 25ff2700..54b66173 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
@@ -370,6 +370,19 @@ public class DefaultArchetypeGeneratorTest {
         assertEquals("file-value", model.getGroupId());
         assertEquals("subsubproject", model.getArtifactId());
         assertEquals("file-value", model.getVersion());
+
+        // Module "demoapi" has a fileSet directory "api" that is also a 
substring of the module's own
+        // directory name, e.g. "demoapi" contains "api" (see GitHub issue 
#274).
+        
assertTemplateContentGeneratedWithFileSetArchetype("demoapi/api/Marker.properties",
 "demoapi");
+
+        model = readPom(new File(projectDirectory, "demoapi/pom.xml"));
+        assertNotNull(model.getParent());
+        assertEquals("file-value", model.getParent().getGroupId());
+        assertEquals("file-value", model.getParent().getArtifactId());
+        assertEquals("file-value", model.getParent().getVersion());
+        assertEquals("file-value", model.getGroupId());
+        assertEquals("demoapi", model.getArtifactId());
+        assertEquals("file-value", model.getVersion());
     }
 
     @Test

Reply via email to