You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/06/04 18:26:12 UTC

(maven) branch master updated: Fix Source.targetPath incorrectly aligned to basedir (#12206)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new dac6ab14a1 Fix Source.targetPath incorrectly aligned to basedir (#12206)
dac6ab14a1 is described below

commit dac6ab14a1cb9298ad8eec693ad3a4e22f8b24a2
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Jun 4 20:26:06 2026 +0200

    Fix Source.targetPath incorrectly aligned to basedir (#12206)
    
    The DefaultModelPathTranslator was aligning Source.targetPath to the
    project basedir, converting relative paths like "META-INF/tags/rdc"
    into absolute paths. This caused maven-resources-plugin to copy
    resources to wrong locations (e.g., /META-INF/tags/rdc instead of
    target/classes/META-INF/tags/rdc).
    
    The targetPath is relative to the output directory (target/classes or
    target/test-classes), not the project basedir, so it must not be
    resolved against basedir during model path translation.
    
    Co-authored-by: Claude Opus 4.6 <no...@anthropic.com>
---
 .../apache/maven/project/ProjectBuilderTest.java   |  95 ++++++++++++++++++
 .../project-builder/resource-target-path/pom.xml   |  48 +++++++++
 .../project-builder/source-target-path/pom.xml     |  49 ++++++++++
 .../impl/model/DefaultModelPathTranslator.java     |   8 +-
 .../impl/model/DefaultModelPathTranslatorTest.java | 107 +++++++++++++++++++++
 5 files changed, 302 insertions(+), 5 deletions(-)

diff --git a/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java b/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
index c79d8cd9aa..f7521ac819 100644
--- a/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
+++ b/impl/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java
@@ -996,4 +996,99 @@ void testDuplicateEnabledSources() throws Exception {
                 testJavaRoots.get(0).module().orElse(null),
                 "Test source root should be for com.example.dup module");
     }
+
+    @Test
+    void testResourceTargetPathRemainsRelativeInCompatLayer() throws Exception {
+        File pom = getProject("resource-target-path");
+
+        MavenSession mavenSession = createMavenSession(null);
+        ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
+        configuration.setRepositorySession(mavenSession.getRepositorySession());
+
+        ProjectBuildingResult result = getContainer()
+                .lookup(org.apache.maven.project.ProjectBuilder.class)
+                .build(pom, configuration);
+
+        MavenProject project = result.getProject();
+
+        // Verify main resources via SourceRoot API
+        List<SourceRoot> mainResources = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
+                .toList();
+        assertEquals(1, mainResources.size());
+        assertTrue(mainResources.get(0).targetPath().isPresent(), "Main resource should have a targetPath");
+        assertFalse(
+                mainResources.get(0).targetPath().get().isAbsolute(),
+                "SourceRoot targetPath must be relative, got: "
+                        + mainResources.get(0).targetPath().get());
+        assertEquals(
+                Path.of("META-INF/tags/rdc"), mainResources.get(0).targetPath().get());
+
+        // Verify test resources via SourceRoot API
+        List<SourceRoot> testResources = project.getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)
+                .toList();
+        assertEquals(1, testResources.size());
+        assertTrue(testResources.get(0).targetPath().isPresent(), "Test resource should have a targetPath");
+        assertFalse(
+                testResources.get(0).targetPath().get().isAbsolute(),
+                "SourceRoot targetPath must be relative, got: "
+                        + testResources.get(0).targetPath().get());
+        assertEquals(
+                Path.of("org/apache/maven/messages"),
+                testResources.get(0).targetPath().get());
+
+        // Verify compat layer: MavenProject.getResources() must return relative targetPath
+        List<org.apache.maven.model.Resource> resources = project.getResources();
+        assertEquals(1, resources.size());
+        assertEquals(
+                "META-INF" + File.separator + "tags" + File.separator + "rdc",
+                resources.get(0).getTargetPath(),
+                "Resource targetPath from getResources() must remain relative");
+
+        // Verify compat layer: MavenProject.getTestResources() must return relative targetPath
+        List<org.apache.maven.model.Resource> testResourceList = project.getTestResources();
+        assertEquals(1, testResourceList.size());
+        assertEquals(
+                "org" + File.separator + "apache" + File.separator + "maven" + File.separator + "messages",
+                testResourceList.get(0).getTargetPath(),
+                "Test resource targetPath from getTestResources() must remain relative");
+    }
+
+    @Test
+    void testSourceTargetPathRemainsRelative() throws Exception {
+        File pom = getProject("source-target-path");
+
+        MavenSession mavenSession = createMavenSession(null);
+        ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
+        configuration.setRepositorySession(mavenSession.getRepositorySession());
+
+        ProjectBuildingResult result = getContainer()
+                .lookup(org.apache.maven.project.ProjectBuilder.class)
+                .build(pom, configuration);
+
+        MavenProject project = result.getProject();
+
+        // Verify main resources have relative targetPath preserved
+        List<SourceRoot> mainResources = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
+                .toList();
+        assertEquals(1, mainResources.size());
+        assertTrue(mainResources.get(0).targetPath().isPresent());
+        assertEquals(Path.of(".grammar"), mainResources.get(0).targetPath().get());
+
+        // Verify test resources have relative targetPath preserved
+        List<SourceRoot> testResources = project.getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)
+                .toList();
+        assertEquals(1, testResources.size());
+        assertTrue(testResources.get(0).targetPath().isPresent());
+        assertEquals(Path.of("META-INF/test"), testResources.get(0).targetPath().get());
+
+        // Verify the compat layer also returns relative targetPath
+        List<org.apache.maven.model.Resource> resources = project.getResources();
+        assertEquals(1, resources.size());
+        assertEquals(".grammar", resources.get(0).getTargetPath());
+
+        List<org.apache.maven.model.Resource> testResourceList = project.getTestResources();
+        assertEquals(1, testResourceList.size());
+        assertEquals(
+                "META-INF" + File.separator + "test", testResourceList.get(0).getTargetPath());
+    }
 }
diff --git a/impl/maven-core/src/test/projects/project-builder/resource-target-path/pom.xml b/impl/maven-core/src/test/projects/project-builder/resource-target-path/pom.xml
new file mode 100644
index 0000000000..aa9dede047
--- /dev/null
+++ b/impl/maven-core/src/test/projects/project-builder/resource-target-path/pom.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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
+
+  https://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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng</groupId>
+  <artifactId>resource-target-path-test</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>Resource TargetPath Compat Test</name>
+  <description>Test that targetPath in legacy resource elements remains relative through the compat layer</description>
+
+  <build>
+    <resources>
+      <resource>
+        <directory>src/main/resources</directory>
+        <targetPath>META-INF/tags/rdc</targetPath>
+      </resource>
+    </resources>
+    <testResources>
+      <testResource>
+        <directory>src/test/resources</directory>
+        <targetPath>org/apache/maven/messages</targetPath>
+      </testResource>
+    </testResources>
+  </build>
+</project>
diff --git a/impl/maven-core/src/test/projects/project-builder/source-target-path/pom.xml b/impl/maven-core/src/test/projects/project-builder/source-target-path/pom.xml
new file mode 100644
index 0000000000..bd8aae771f
--- /dev/null
+++ b/impl/maven-core/src/test/projects/project-builder/source-target-path/pom.xml
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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
+
+  https://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.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 https://maven.apache.org/xsd/maven-4.1.0.xsd">
+  <modelVersion>4.1.0</modelVersion>
+
+  <groupId>org.apache.maven.its.mng</groupId>
+  <artifactId>source-target-path-test</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>Source TargetPath Test</name>
+  <description>Test that targetPath in source elements remains relative to output directory</description>
+
+  <build>
+    <sources>
+      <source>
+        <lang>resources</lang>
+        <directory>src/main/resources</directory>
+        <targetPath>.grammar</targetPath>
+      </source>
+      <source>
+        <lang>resources</lang>
+        <scope>test</scope>
+        <directory>src/test/resources</directory>
+        <targetPath>META-INF/test</targetPath>
+      </source>
+    </sources>
+  </build>
+</project>
diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelPathTranslator.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelPathTranslator.java
index ad4135d94b..52ebbbf9d9 100644
--- a/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelPathTranslator.java
+++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelPathTranslator.java
@@ -134,11 +134,9 @@ private Source alignToBaseDirectory(Source source, Path basedir) {
             if (newDir != oldDir) {
                 source = source.withDirectory(newDir);
             }
-            oldDir = source.getTargetPath();
-            newDir = alignToBaseDirectory(oldDir, basedir);
-            if (newDir != oldDir) {
-                source = source.withTargetPath(newDir);
-            }
+            // Note: targetPath is intentionally NOT aligned to basedir.
+            // It is relative to the output directory (target/classes or target/test-classes),
+            // not to the project base directory. See maven.mdo Source.targetPath documentation.
         }
         return source;
     }
diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelPathTranslatorTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelPathTranslatorTest.java
new file mode 100644
index 0000000000..be71eff2d5
--- /dev/null
+++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelPathTranslatorTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.
+ */
+package org.apache.maven.impl.model;
+
+import java.nio.file.Path;
+
+import org.apache.maven.api.model.Build;
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.model.Resource;
+import org.apache.maven.api.model.Source;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DefaultModelPathTranslatorTest {
+
+    private DefaultModelPathTranslator translator;
+    private Path basedir;
+
+    @BeforeEach
+    void setUp() {
+        translator = new DefaultModelPathTranslator(new DefaultPathTranslator());
+        basedir = Path.of("/home/user/myproject").toAbsolutePath();
+    }
+
+    @Test
+    void sourceDirectoryIsAlignedToBasedir() {
+        Source source = Source.newBuilder().directory("src/main/java").build();
+        Model model = Model.newBuilder()
+                .build(Build.newBuilder().sources(java.util.List.of(source)).build())
+                .build();
+
+        Model result = translator.alignToBaseDirectory(model, basedir, null);
+
+        String dir = result.getBuild().getSources().get(0).getDirectory();
+        assertTrue(Path.of(dir).isAbsolute(), "directory should be absolute after alignment");
+        assertTrue(Path.of(dir).endsWith(Path.of("src/main/java")), "directory should end with original path");
+    }
+
+    @Test
+    void sourceTargetPathIsNotAlignedToBasedir() {
+        Source source = Source.newBuilder()
+                .directory("src/main/resources")
+                .targetPath("META-INF/resources")
+                .build();
+        Model model = Model.newBuilder()
+                .build(Build.newBuilder().sources(java.util.List.of(source)).build())
+                .build();
+
+        Model result = translator.alignToBaseDirectory(model, basedir, null);
+
+        String targetPath = result.getBuild().getSources().get(0).getTargetPath();
+        assertEquals("META-INF/resources", targetPath, "targetPath should remain relative");
+    }
+
+    @Test
+    void sourceTargetPathDotPrefixRemainsRelative() {
+        Source source = Source.newBuilder()
+                .directory("src/main/resources")
+                .targetPath(".grammar")
+                .build();
+        Model model = Model.newBuilder()
+                .build(Build.newBuilder().sources(java.util.List.of(source)).build())
+                .build();
+
+        Model result = translator.alignToBaseDirectory(model, basedir, null);
+
+        String targetPath = result.getBuild().getSources().get(0).getTargetPath();
+        assertEquals(".grammar", targetPath, "dot-prefixed targetPath should remain relative");
+    }
+
+    @Test
+    void resourceDirectoryIsAlignedButTargetPathIsNot() {
+        Resource resource = Resource.newBuilder()
+                .directory("src/main/resources")
+                .targetPath("custom-output")
+                .build();
+        Model model = Model.newBuilder()
+                .build(Build.newBuilder().resources(java.util.List.of(resource)).build())
+                .build();
+
+        Model result = translator.alignToBaseDirectory(model, basedir, null);
+
+        Resource aligned = result.getBuild().getResources().get(0);
+        assertTrue(
+                Path.of(aligned.getDirectory()).isAbsolute(), "resource directory should be absolute after alignment");
+        assertEquals("custom-output", aligned.getTargetPath(), "resource targetPath should remain relative");
+    }
+}