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

commit 6ce8a46777ac436162aa98839ae92c037cbcb0ea
Author: Arturo Bernal <[email protected]>
AuthorDate: Fri Oct 30 11:29:20 2026 +0100

    Fix module directory names that collide with a fileSet directory
    
    DefaultFilesetArchetypeGenerator.getOutputFile() stripped a fileSet's
    directory from a template path with a plain first-occurrence
    substring replace, not a path-anchored one. When a module's own
    directory name contains the fileSet directory's name as a substring
    (e.g. module dir "openapi" with a nested fileSet directory "api"),
    that earlier, unrelated occurrence gets stripped instead of the real
    one, corrupting the output path or throwing a StringIndexOutOfBounds
    once the mismatch outruns the subsequent offset-based substring.
    
    Compute the known (moduleOffset + directory) prefix instead - the
    same basedir filterFiles() already scans resources against - and
    strip exactly that, so an unrelated path segment that happens to
    contain the directory's text can no longer be mistaken for it.
---
 .../DefaultFilesetArchetypeGenerator.java          | 34 +++++++++++++---------
 .../META-INF/maven/archetype-metadata.xml          | 11 +++++++
 .../archetype-resources/openapi/api/index.ts       |  5 ++++
 .../archetype-resources/{ => openapi}/pom.xml      | 13 +++++----
 .../fileset-1.0/archetype-resources/pom.xml        |  1 +
 .../generator/DefaultArchetypeGeneratorTest.java   |  4 +++
 6 files changed, 49 insertions(+), 19 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 0914ca21..c515427d 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,19 +342,27 @@ public class DefaultFilesetArchetypeGenerator implements 
FilesetArchetypeGenerat
             String packageName,
             String moduleOffset,
             Context context) {
-        // 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());
+        // template is one of the archetypeResources matched by filterFiles() 
against a basedir built the
+        // same way, so it is always prefixed by exactly (moduleOffset + "/", 
if any) + directory. Stripping
+        // that known prefix - rather than searching for directory as a bare 
substring - avoids falsely
+        // matching an unrelated earlier path segment that happens to contain 
directory's text (e.g. a
+        // module dir named "openapi" when directory is "api").
+        String prefix = (moduleOffset == null || moduleOffset.isEmpty()) ? 
directory : (moduleOffset + "/" + directory);
+        String templateName;
+        if (template.startsWith(prefix)) {
+            templateName = template.substring(prefix.length());
+        } else if (template.equals(moduleOffset)) {
+            // processTemplates() also calls this with template == 
moduleOffset (no directory/file suffix
+            // at all), purely to pre-create the fileSet's own empty 
directory; there is no relative file
+            // name to append in that case.
+            templateName = "";
+        } else {
+            LOGGER.warn("Template " + template + " does not start with 
expected prefix " + prefix);
+            templateName = template;
+        }
+
+        String outputFileName =
+                directory + "/" + (packaged ? 
getPackageAsDirectory(packageName) : "") + "/" + templateName;
 
         outputFileName = replaceFilenameTokens(outputFileName, context);
 
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 9998ac73..f81dcf53 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
@@ -89,6 +89,17 @@
   </fileSets>
 
   <modules>
+    <module name="openapi" id="openapi" dir="openapi">
+      <fileSets>
+        <fileSet filtered="false" packaged="false">
+          <directory>api</directory>
+          <includes>
+            <include>**/*.ts</include>
+          </includes>
+        </fileSet>
+      </fileSets>
+    </module>
+
     <module name="subproject" id="subproject" dir="subproject">
       <fileSets>
         <fileSet filtered="true" packaged="true">
diff --git 
a/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/openapi/api/index.ts
 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/openapi/api/index.ts
new file mode 100644
index 00000000..6e7d4f2a
--- /dev/null
+++ 
b/archetype-common/src/test/archetypes/fileset-1.0/archetype-resources/openapi/api/index.ts
@@ -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/openapi/pom.xml
similarity index 86%
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/openapi/pom.xml
index 4531fbec..98790338 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/openapi/pom.xml
@@ -24,16 +24,17 @@
 
   <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>
+  <name>Maven archetype Test OpenAPI Module</name>
   <packaging>pom</packaging>
 
-  <modules>
-    <module>subproject</module>
-    <module>demoapi</module>
-  </modules>
-
 </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 4531fbec..f6afd348 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
@@ -32,6 +32,7 @@
   <packaging>pom</packaging>
 
   <modules>
+    <module>openapi</module>
     <module>subproject</module>
     <module>demoapi</module>
   </modules>
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 54b66173..5f8e3375 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
@@ -323,6 +323,10 @@ public class DefaultArchetypeGeneratorTest {
         assertEquals("file-value", model.getArtifactId());
         assertEquals("file-value", model.getVersion());
 
+        // ARCHETYPE-? : a module directory name ("openapi") that contains the 
fileSet directory's own
+        // name ("api") as a substring must not corrupt the output path of 
files in that fileSet.
+        assertTemplateCopiedWithFileSetArchetype("openapi/api/index.ts");
+
         assertTemplateContentGeneratedWithFileSetArchetype(
                 "subproject/src/main/java/file/value/package/App.java", 
"subproject");
 

Reply via email to