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 8da850f96b5329a170939e19d853f5840d10e31f
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Sat Oct 31 13:18:19 2026 +0000

    Keep the fileSet directory handling that master already has
    
    Master fixes the same collision since #1049, so this PR keeps its 
regression test and fixture on top of that implementation.
---
 .../DefaultFilesetArchetypeGenerator.java          | 34 +++++++++-------------
 1 file changed, 13 insertions(+), 21 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 c515427d..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,27 +342,19 @@ public class DefaultFilesetArchetypeGenerator implements 
FilesetArchetypeGenerat
             String packageName,
             String moduleOffset,
             Context context) {
-        // 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;
+        // 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());
 
         outputFileName = replaceFilenameTokens(outputFileName, context);
 

Reply via email to