View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.io.File;
22  import java.nio.charset.StandardCharsets;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Optional;
29  import java.util.Properties;
30  import java.util.Set;
31  import java.util.concurrent.atomic.AtomicInteger;
32  import java.util.stream.Collectors;
33  
34  import org.apache.maven.AbstractCoreMavenComponentTestCase;
35  import org.apache.maven.api.Language;
36  import org.apache.maven.api.ProjectScope;
37  import org.apache.maven.api.SourceRoot;
38  import org.apache.maven.execution.MavenSession;
39  import org.apache.maven.model.Dependency;
40  import org.apache.maven.model.InputLocation;
41  import org.apache.maven.model.Plugin;
42  import org.apache.maven.model.building.ModelBuildingRequest;
43  import org.apache.maven.model.building.ModelProblem;
44  import org.codehaus.plexus.util.FileUtils;
45  import org.junit.jupiter.api.Test;
46  import org.junit.jupiter.api.io.TempDir;
47  
48  import static org.apache.maven.project.ProjectBuildingResultWithLocationMatcher.projectBuildingResultWithLocation;
49  import static org.apache.maven.project.ProjectBuildingResultWithProblemMessageMatcher.projectBuildingResultWithProblemMessage;
50  import static org.hamcrest.MatcherAssert.assertThat;
51  import static org.hamcrest.Matchers.contains;
52  import static org.hamcrest.Matchers.containsString;
53  import static org.hamcrest.Matchers.empty;
54  import static org.hamcrest.Matchers.greaterThan;
55  import static org.hamcrest.Matchers.hasKey;
56  import static org.hamcrest.Matchers.is;
57  import static org.junit.jupiter.api.Assertions.assertEquals;
58  import static org.junit.jupiter.api.Assertions.assertFalse;
59  import static org.junit.jupiter.api.Assertions.assertNotNull;
60  import static org.junit.jupiter.api.Assertions.assertThrows;
61  import static org.junit.jupiter.api.Assertions.assertTrue;
62  
63  class ProjectBuilderTest extends AbstractCoreMavenComponentTestCase {
64      @Override
65      protected String getProjectsDirectory() {
66          return "src/test/projects/project-builder";
67      }
68  
69      @Test
70      void testSystemScopeDependencyIsPresentInTheCompileClasspathElements() throws Exception {
71          File pom = getProject("it0063");
72  
73          Properties eps = new Properties();
74          eps.setProperty("jre.home", new File(pom.getParentFile(), "jdk/jre").getPath());
75  
76          MavenSession session = createMavenSession(pom, eps);
77          MavenProject project = session.getCurrentProject();
78  
79          // Here we will actually not have any artifacts because the ProjectDependenciesResolver is not involved here. So
80          // right now it's not valid to ask for artifacts unless plugins require the artifacts.
81  
82          project.getCompileClasspathElements();
83      }
84  
85      @Test
86      void testBuildFromModelSource() throws Exception {
87          File pomFile = new File("src/test/resources/projects/modelsource/module01/pom.xml");
88          MavenSession mavenSession = createMavenSession(pomFile);
89          ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
90          configuration.setRepositorySession(mavenSession.getRepositorySession());
91          ProjectBuildingResult result = getContainer()
92                  .lookup(org.apache.maven.project.ProjectBuilder.class)
93                  .build(pomFile, configuration);
94  
95          assertNotNull(result.getProject().getParentFile());
96      }
97  
98      @Test
99      void testVersionlessManagedDependency() throws Exception {
100         File pomFile = new File("src/test/resources/projects/versionless-managed-dependency.xml");
101         MavenSession mavenSession = createMavenSession(null);
102         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
103         configuration.setRepositorySession(mavenSession.getRepositorySession());
104 
105         ProjectBuildingException e = assertThrows(ProjectBuildingException.class, () -> getContainer()
106                 .lookup(org.apache.maven.project.ProjectBuilder.class)
107                 .build(pomFile, configuration));
108         assertThat(
109                 e.getResults(),
110                 contains(
111                         projectBuildingResultWithProblemMessage(
112                                 "'dependencies.dependency.version' for groupId='org.apache.maven.its', artifactId='a', type='jar' is missing")));
113         assertThat(e.getResults(), contains(projectBuildingResultWithLocation(5, 9)));
114     }
115 
116     @Test
117     void testResolveDependencies() throws Exception {
118         File pomFile = new File("src/test/resources/projects/basic-resolveDependencies.xml");
119         MavenSession mavenSession = createMavenSession(null);
120         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
121         configuration.setRepositorySession(mavenSession.getRepositorySession());
122         configuration.setResolveDependencies(true);
123 
124         // single project build entry point
125         ProjectBuildingResult result = getContainer()
126                 .lookup(org.apache.maven.project.ProjectBuilder.class)
127                 .build(pomFile, configuration);
128         assertEquals(1, result.getProject().getArtifacts().size());
129         // multi projects build entry point
130         List<ProjectBuildingResult> results = getContainer()
131                 .lookup(org.apache.maven.project.ProjectBuilder.class)
132                 .build(Collections.singletonList(pomFile), false, configuration);
133         assertEquals(1, results.size());
134         MavenProject mavenProject = results.get(0).getProject();
135         assertEquals(1, mavenProject.getArtifacts().size());
136 
137         final MavenProject project = mavenProject;
138         final AtomicInteger artifactsResultInAnotherThread = new AtomicInteger();
139         Thread t = new Thread(new Runnable() {
140             @Override
141             public void run() {
142                 artifactsResultInAnotherThread.set(project.getArtifacts().size());
143             }
144         });
145         t.start();
146         t.join();
147         assertEquals(project.getArtifacts().size(), artifactsResultInAnotherThread.get());
148     }
149 
150     @Test
151     void testDontResolveDependencies() throws Exception {
152         File pomFile = new File("src/test/resources/projects/basic-resolveDependencies.xml");
153         MavenSession mavenSession = createMavenSession(null);
154         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
155         configuration.setRepositorySession(mavenSession.getRepositorySession());
156         configuration.setResolveDependencies(false);
157 
158         // single project build entry point
159         ProjectBuildingResult result = getContainer()
160                 .lookup(org.apache.maven.project.ProjectBuilder.class)
161                 .build(pomFile, configuration);
162         assertEquals(0, result.getProject().getArtifacts().size());
163         // multi projects build entry point
164         List<ProjectBuildingResult> results = getContainer()
165                 .lookup(org.apache.maven.project.ProjectBuilder.class)
166                 .build(Collections.singletonList(pomFile), false, configuration);
167         assertEquals(1, results.size());
168         MavenProject mavenProject = results.get(0).getProject();
169         assertEquals(0, mavenProject.getArtifacts().size());
170     }
171 
172     @Test
173     void testReadModifiedPoms(@TempDir Path tempDir) throws Exception {
174         // TODO a similar test should be created to test the dependency management (basically all usages
175         // of DefaultModelBuilder.getCache() are affected by MNG-6530
176 
177         FileUtils.copyDirectoryStructure(new File("src/test/resources/projects/grandchild-check"), tempDir.toFile());
178 
179         MavenSession mavenSession = createMavenSession(null);
180         mavenSession.getRequest().setRootDirectory(tempDir);
181         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
182         configuration.setRepositorySession(mavenSession.getRepositorySession());
183         org.apache.maven.project.ProjectBuilder projectBuilder =
184                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
185         File child = new File(tempDir.toFile(), "child/pom.xml");
186         // build project once
187         projectBuilder.build(child, configuration);
188         // modify parent
189         File parent = new File(tempDir.toFile(), "pom.xml");
190         String parentContent = new String(Files.readAllBytes(parent.toPath()), StandardCharsets.UTF_8);
191         parentContent = parentContent.replace(
192                 "<packaging>pom</packaging>",
193                 "<packaging>pom</packaging><properties><addedProperty>addedValue</addedProperty></properties>");
194         Files.write(parent.toPath(), parentContent.getBytes(StandardCharsets.UTF_8));
195         // re-build pom with modified parent
196         ProjectBuildingResult result = projectBuilder.build(child, configuration);
197         assertThat(result.getProject().getProperties(), hasKey((Object) "addedProperty"));
198     }
199 
200     @Test
201     void testReadErroneousMavenProjectContainsReference() throws Exception {
202         File pomFile = new File("src/test/resources/projects/artifactMissingVersion/pom.xml").getAbsoluteFile();
203         MavenSession mavenSession = createMavenSession(null);
204         mavenSession.getRequest().setRootDirectory(pomFile.getParentFile().toPath());
205         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
206         configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
207         configuration.setRepositorySession(mavenSession.getRepositorySession());
208         org.apache.maven.project.ProjectBuilder projectBuilder =
209                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
210 
211         // single project build entry point
212         ProjectBuildingException ex1 =
213                 assertThrows(ProjectBuildingException.class, () -> projectBuilder.build(pomFile, configuration));
214 
215         assertEquals(1, ex1.getResults().size());
216         MavenProject project1 = ex1.getResults().get(0).getProject();
217         assertNotNull(project1);
218         assertEquals("testArtifactMissingVersion", project1.getArtifactId());
219         assertEquals(pomFile, project1.getFile());
220 
221         // multi projects build entry point
222         ProjectBuildingException ex2 = assertThrows(
223                 ProjectBuildingException.class,
224                 () -> projectBuilder.build(Collections.singletonList(pomFile), true, configuration));
225 
226         assertEquals(1, ex2.getResults().size());
227         MavenProject project2 = ex2.getResults().get(0).getProject();
228         assertNotNull(project2);
229         assertEquals("testArtifactMissingVersion", project2.getArtifactId());
230         assertEquals(pomFile, project2.getFile());
231     }
232 
233     @Test
234     void testReadInvalidPom() throws Exception {
235         File pomFile = new File("src/test/resources/projects/badPom.xml").getAbsoluteFile();
236         MavenSession mavenSession = createMavenSession(null);
237         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
238         configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_STRICT);
239         configuration.setRepositorySession(mavenSession.getRepositorySession());
240         org.apache.maven.project.ProjectBuilder projectBuilder =
241                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
242 
243         // single project build entry point
244         Exception ex = assertThrows(Exception.class, () -> projectBuilder.build(pomFile, configuration));
245         assertThat(ex.getMessage(), containsString("Received non-all-whitespace CHARACTERS or CDATA event"));
246 
247         // multi projects build entry point
248         ProjectBuildingException pex = assertThrows(
249                 ProjectBuildingException.class,
250                 () -> projectBuilder.build(Collections.singletonList(pomFile), false, configuration));
251         assertEquals(1, pex.getResults().size());
252         assertNotNull(pex.getResults().get(0).getPomFile());
253         assertThat(pex.getResults().get(0).getProblems().size(), greaterThan(0));
254         assertThat(
255                 pex.getResults(),
256                 contains(projectBuildingResultWithProblemMessage(
257                         "Received non-all-whitespace CHARACTERS or CDATA event in nextTag()")));
258     }
259 
260     @Test
261     void testReadParentAndChildWithRegularVersionSetParentFile() throws Exception {
262         List<File> toRead = new ArrayList<>(2);
263         File parentPom = getProject("MNG-6723");
264         toRead.add(parentPom);
265         toRead.add(new File(parentPom.getParentFile(), "child/pom.xml"));
266         MavenSession mavenSession = createMavenSession(null);
267         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
268         configuration.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
269         configuration.setRepositorySession(mavenSession.getRepositorySession());
270         org.apache.maven.project.ProjectBuilder projectBuilder =
271                 getContainer().lookup(org.apache.maven.project.ProjectBuilder.class);
272 
273         // read poms separately
274         boolean parentFileWasFoundOnChild = false;
275         for (File file : toRead) {
276             List<ProjectBuildingResult> results =
277                     projectBuilder.build(Collections.singletonList(file), false, configuration);
278             assertResultShowNoError(results);
279             MavenProject project = findChildProject(results);
280             if (project != null) {
281                 assertEquals(parentPom, project.getParentFile());
282                 parentFileWasFoundOnChild = true;
283             }
284         }
285         assertTrue(parentFileWasFoundOnChild);
286 
287         // read projects together
288         List<ProjectBuildingResult> results = projectBuilder.build(toRead, false, configuration);
289         assertResultShowNoError(results);
290         assertEquals(parentPom, findChildProject(results).getParentFile());
291         Collections.reverse(toRead);
292         results = projectBuilder.build(toRead, false, configuration);
293         assertResultShowNoError(results);
294         assertEquals(parentPom, findChildProject(results).getParentFile());
295     }
296 
297     private MavenProject findChildProject(List<ProjectBuildingResult> results) {
298         for (ProjectBuildingResult result : results) {
299             if (result.getPomFile().getParentFile().getName().equals("child")) {
300                 return result.getProject();
301             }
302         }
303         return null;
304     }
305 
306     private void assertResultShowNoError(List<ProjectBuildingResult> results) {
307         for (ProjectBuildingResult result : results) {
308             assertThat(result.getProblems(), is(empty()));
309             assertNotNull(result.getProject());
310         }
311     }
312 
313     @Test
314     void testBuildProperties() throws Exception {
315         File file = new File(getProject("MNG-6716").getParentFile(), "project/pom.xml");
316         MavenSession mavenSession = createMavenSession(null);
317         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
318         configuration.setRepositorySession(mavenSession.getRepositorySession());
319         configuration.setResolveDependencies(true);
320         List<ProjectBuildingResult> result =
321                 projectBuilder.build(Collections.singletonList(file), false, configuration);
322         MavenProject project = result.get(0).getProject();
323         // verify a few typical parameters are not duplicated
324         assertEquals(1, project.getTestCompileSourceRoots().size());
325         assertEquals(1, project.getCompileSourceRoots().size());
326         assertEquals(1, project.getMailingLists().size());
327         assertEquals(1, project.getResources().size());
328     }
329 
330     @Test
331     void testPropertyInPluginManagementGroupId() throws Exception {
332         File pom = getProject("MNG-6983");
333 
334         MavenSession session = createMavenSession(pom);
335         MavenProject project = session.getCurrentProject();
336 
337         for (Plugin buildPlugin : project.getBuildPlugins()) {
338             assertNotNull(buildPlugin.getVersion(), "Missing version for build plugin " + buildPlugin.getKey());
339         }
340     }
341 
342     @Test
343     void testBuildFromModelSourceResolvesBasedir() throws Exception {
344         File pomFile = new File("src/test/resources/projects/modelsourcebasedir/pom.xml");
345         MavenSession mavenSession = createMavenSession(null);
346         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
347         configuration.setRepositorySession(mavenSession.getRepositorySession());
348         ProjectBuildingResult result = getContainer()
349                 .lookup(org.apache.maven.project.ProjectBuilder.class)
350                 .build(pomFile, configuration);
351 
352         assertEquals(
353                 pomFile.getAbsoluteFile(),
354                 result.getProject().getModel().getPomFile().getAbsoluteFile());
355         int errors = 0;
356         for (ModelProblem p : result.getProblems()) {
357             if (p.getSeverity() == ModelProblem.Severity.ERROR) {
358                 errors++;
359             }
360         }
361         assertEquals(0, errors);
362     }
363 
364     @Test
365     void testLocationTrackingResolution() throws Exception {
366         File pom = getProject("MNG-7648");
367 
368         MavenSession session = createMavenSession(pom);
369         MavenProject project = session.getCurrentProject();
370 
371         InputLocation dependencyLocation = null;
372         for (Dependency dependency : project.getDependencies()) {
373             if (dependency.getManagementKey().equals("org.apache.maven.its:a:jar")) {
374                 dependencyLocation = dependency.getLocation("version");
375             }
376         }
377         assertNotNull(dependencyLocation, "missing dependency");
378         assertEquals(
379                 "org.apache.maven.its:bom:0.1", dependencyLocation.getSource().getModelId());
380 
381         InputLocation pluginLocation = null;
382         for (Plugin plugin : project.getBuildPlugins()) {
383             if (plugin.getKey().equals("org.apache.maven.plugins:maven-clean-plugin")) {
384                 pluginLocation = plugin.getLocation("version");
385             }
386         }
387         assertNotNull(pluginLocation, "missing build plugin");
388         assertEquals(
389                 "org.apache.maven.its:parent:0.1", pluginLocation.getSource().getModelId());
390     }
391     /**
392      * Tests that a project with multiple modules defined in sources is detected as modular,
393      * and module-aware resource roots are injected for each module.
394      * <p>
395      * Acceptance Criterion: AC2 (unified source tracking for all lang/scope combinations)
396      *
397      * @see <a href="https://github.com/apache/maven/issues/11612">Issue #11612</a>
398      */
399     @Test
400     void testModularSourcesInjectResourceRoots() throws Exception {
401         File pom = getProject("modular-sources");
402 
403         MavenSession session = createMavenSession(pom);
404         MavenProject project = session.getCurrentProject();
405 
406         // Get all resource source roots for main scope
407         List<SourceRoot> mainResourceRoots = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
408                 .toList();
409 
410         // Should have resource roots for both modules
411         Set<String> modules = mainResourceRoots.stream()
412                 .map(SourceRoot::module)
413                 .flatMap(Optional::stream)
414                 .collect(Collectors.toSet());
415 
416         assertEquals(2, modules.size(), "Should have resource roots for 2 modules");
417         assertTrue(modules.contains("org.foo.moduleA"), "Should have resource root for moduleA");
418         assertTrue(modules.contains("org.foo.moduleB"), "Should have resource root for moduleB");
419 
420         // Get all resource source roots for test scope
421         List<SourceRoot> testResourceRoots = project.getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)
422                 .toList();
423 
424         // Should have test resource roots for both modules
425         Set<String> testModules = testResourceRoots.stream()
426                 .map(SourceRoot::module)
427                 .flatMap(Optional::stream)
428                 .collect(Collectors.toSet());
429 
430         assertEquals(2, testModules.size(), "Should have test resource roots for 2 modules");
431         assertTrue(testModules.contains("org.foo.moduleA"), "Should have test resource root for moduleA");
432         assertTrue(testModules.contains("org.foo.moduleB"), "Should have test resource root for moduleB");
433     }
434 
435     /**
436      * Tests that when modular sources are configured alongside explicit legacy resources, an error is raised.
437      * <p>
438      * This verifies the behavior described in the design:
439      * - Modular projects with explicit legacy {@code <resources>} configuration should raise an error
440      * - The modular resource roots are injected instead of using the legacy configuration
441      * <p>
442      * Acceptance Criteria:
443      * - AC2 (unified source tracking for all lang/scope combinations)
444      * - AC8 (legacy directories error - supersedes AC7 which originally used WARNING)
445      *
446      * @see <a href="https://github.com/apache/maven/issues/11612">Issue #11612</a>
447      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3858462609">AC8 definition</a>
448      */
449     @Test
450     void testModularSourcesWithExplicitResourcesIssuesError() throws Exception {
451         File pom = getProject("modular-sources-with-explicit-resources");
452 
453         MavenSession mavenSession = createMavenSession(null);
454         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
455         configuration.setRepositorySession(mavenSession.getRepositorySession());
456 
457         ProjectBuildingResult result = getContainer()
458                 .lookup(org.apache.maven.project.ProjectBuilder.class)
459                 .build(pom, configuration);
460 
461         MavenProject project = result.getProject();
462 
463         // Verify errors are raised for conflicting legacy resources (AC8)
464         List<ModelProblem> errors = result.getProblems().stream()
465                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
466                 .filter(p -> p.getMessage().contains("Legacy") && p.getMessage().contains("cannot be used"))
467                 .toList();
468 
469         assertEquals(2, errors.size(), "Should have 2 errors (one for resources, one for testResources)");
470         assertTrue(
471                 errors.stream().anyMatch(e -> e.getMessage().contains("<resources>")),
472                 "Should error about conflicting <resources>");
473         assertTrue(
474                 errors.stream().anyMatch(e -> e.getMessage().contains("<testResources>")),
475                 "Should error about conflicting <testResources>");
476 
477         // Verify modular resources are still injected correctly
478         List<SourceRoot> mainResourceRoots = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
479                 .toList();
480 
481         assertEquals(2, mainResourceRoots.size(), "Should have 2 modular resource roots (one per module)");
482 
483         Set<String> mainModules = mainResourceRoots.stream()
484                 .map(SourceRoot::module)
485                 .flatMap(Optional::stream)
486                 .collect(Collectors.toSet());
487 
488         assertEquals(2, mainModules.size(), "Should have resource roots for 2 modules");
489         assertTrue(mainModules.contains("org.foo.moduleA"), "Should have resource root for moduleA");
490         assertTrue(mainModules.contains("org.foo.moduleB"), "Should have resource root for moduleB");
491     }
492 
493     /**
494      * Tests AC8: ALL legacy directories are rejected when {@code <sources>} is configured.
495      * <p>
496      * Modular project with Java in {@code <sources>} for MAIN scope and explicit legacy
497      * {@code <sourceDirectory>} that differs from default. The legacy directory is rejected
498      * because modular projects cannot use legacy directories (content cannot be dispatched
499      * between modules).
500      *
501      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3897961755">Issue #11701 (AC8/AC9)</a>
502      */
503     @Test
504     void testModularWithJavaSourcesRejectsLegacySourceDirectory() throws Exception {
505         File pom = getProject("modular-java-with-explicit-source-dir");
506 
507         MavenSession mavenSession = createMavenSession(null);
508         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
509         configuration.setRepositorySession(mavenSession.getRepositorySession());
510 
511         ProjectBuildingResult result = getContainer()
512                 .lookup(org.apache.maven.project.ProjectBuilder.class)
513                 .build(pom, configuration);
514 
515         MavenProject project = result.getProject();
516 
517         // Verify ERROR for <sourceDirectory> (MAIN scope has Java in <sources>)
518         List<ModelProblem> errors = result.getProblems().stream()
519                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
520                 .filter(p -> p.getMessage().contains("Legacy") && p.getMessage().contains("cannot be used"))
521                 .filter(p -> p.getMessage().contains("<sourceDirectory>"))
522                 .toList();
523 
524         assertEquals(1, errors.size(), "Should have 1 error for <sourceDirectory>");
525 
526         // Verify modular source is used, not legacy
527         List<SourceRoot> mainJavaRoots = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.JAVA_FAMILY)
528                 .toList();
529         assertEquals(1, mainJavaRoots.size(), "Should have 1 modular main Java source root");
530         assertEquals("org.foo.app", mainJavaRoots.get(0).module().orElse(null), "Should have module org.foo.app");
531 
532         // Legacy sourceDirectory is NOT used
533         assertFalse(
534                 mainJavaRoots.get(0).directory().toString().contains("src/custom/main/java"),
535                 "Legacy sourceDirectory should not be used");
536     }
537 
538     /**
539      * Tests AC8: Modular project rejects legacy {@code <testSourceDirectory>} even when
540      * {@code <sources>} has NO Java for TEST scope.
541      * <p>
542      * Modular project with NO Java in {@code <sources>} for TEST scope and explicit legacy
543      * {@code <testSourceDirectory>} that differs from default. The legacy directory is rejected
544      * because modular projects cannot use legacy directories (content cannot be dispatched
545      * between modules).
546      *
547      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3897961755">Issue #11701 (AC8/AC9)</a>
548      */
549     @Test
550     void testModularWithoutTestSourcesRejectsLegacyTestSourceDirectory() throws Exception {
551         File pom = getProject("modular-no-test-java-with-explicit-test-source-dir");
552 
553         MavenSession mavenSession = createMavenSession(null);
554         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
555         configuration.setRepositorySession(mavenSession.getRepositorySession());
556 
557         ProjectBuildingResult result = getContainer()
558                 .lookup(org.apache.maven.project.ProjectBuilder.class)
559                 .build(pom, configuration);
560 
561         MavenProject project = result.getProject();
562 
563         // Verify ERROR for <testSourceDirectory> (modular projects reject all legacy directories)
564         List<ModelProblem> errors = result.getProblems().stream()
565                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
566                 .filter(p -> p.getMessage().contains("Legacy") && p.getMessage().contains("cannot be used"))
567                 .filter(p -> p.getMessage().contains("<testSourceDirectory>"))
568                 .toList();
569 
570         assertEquals(1, errors.size(), "Should have 1 error for <testSourceDirectory>");
571 
572         // No test Java sources (legacy rejected, none in <sources>)
573         List<SourceRoot> testJavaRoots = project.getEnabledSourceRoots(ProjectScope.TEST, Language.JAVA_FAMILY)
574                 .toList();
575         assertEquals(0, testJavaRoots.size(), "Should have no test Java sources");
576     }
577 
578     /**
579      * Tests AC9: explicit legacy directories raise an error in non-modular projects when
580      * {@code <sources>} has Java for that scope.
581      * <p>
582      * This test uses a non-modular project (no {@code <module>} attribute) with both:
583      * <ul>
584      *   <li>{@code <sources>} with main and test Java sources</li>
585      *   <li>Explicit {@code <sourceDirectory>} and {@code <testSourceDirectory>} (conflicting)</li>
586      * </ul>
587      * Both legacy directories should trigger ERROR because {@code <sources>} has Java.
588      *
589      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3897961755">Issue #11701 (AC8/AC9)</a>
590      */
591     @Test
592     void testClassicSourcesWithExplicitLegacyDirectories() throws Exception {
593         File pom = getProject("classic-sources-with-explicit-legacy");
594 
595         MavenSession mavenSession = createMavenSession(null);
596         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
597         configuration.setRepositorySession(mavenSession.getRepositorySession());
598 
599         ProjectBuildingResult result = getContainer()
600                 .lookup(org.apache.maven.project.ProjectBuilder.class)
601                 .build(pom, configuration);
602 
603         // Verify errors are raised for conflicting legacy directories (AC9)
604         List<ModelProblem> errors = result.getProblems().stream()
605                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
606                 .filter(p -> p.getMessage().contains("Legacy") && p.getMessage().contains("cannot be used"))
607                 .toList();
608 
609         assertEquals(2, errors.size(), "Should have 2 errors (one for sourceDirectory, one for testSourceDirectory)");
610 
611         // Verify error messages mention the conflicting elements
612         assertTrue(
613                 errors.stream().anyMatch(e -> e.getMessage().contains("<sourceDirectory>")),
614                 "Should have error for <sourceDirectory>");
615         assertTrue(
616                 errors.stream().anyMatch(e -> e.getMessage().contains("<testSourceDirectory>")),
617                 "Should have error for <testSourceDirectory>");
618     }
619 
620     /**
621      * Tests AC9: Non-modular project with only resources in {@code <sources>} uses implicit Java fallback.
622      * <p>
623      * When {@code <sources>} contains only resources (no Java sources), the legacy
624      * {@code <sourceDirectory>} and {@code <testSourceDirectory>} are used as implicit fallback.
625      * This enables incremental adoption of {@code <sources>} - customize resources while
626      * keeping the default Java directory structure.
627      *
628      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3897961755">Issue #11701 (AC8/AC9)</a>
629      */
630     @Test
631     void testNonModularResourcesOnlyWithImplicitJavaFallback() throws Exception {
632         File pom = getProject("non-modular-resources-only");
633 
634         MavenSession mavenSession = createMavenSession(null);
635         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
636         configuration.setRepositorySession(mavenSession.getRepositorySession());
637 
638         ProjectBuildingResult result = getContainer()
639                 .lookup(org.apache.maven.project.ProjectBuilder.class)
640                 .build(pom, configuration);
641 
642         MavenProject project = result.getProject();
643 
644         // Verify NO errors - legacy directories are used as fallback (AC9)
645         List<ModelProblem> errors = result.getProblems().stream()
646                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
647                 .filter(p -> p.getMessage().contains("Legacy") && p.getMessage().contains("cannot be used"))
648                 .toList();
649 
650         assertEquals(0, errors.size(), "Should have no errors - legacy directories used as fallback (AC9)");
651 
652         // Verify resources from <sources> are used
653         List<SourceRoot> mainResources = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
654                 .toList();
655         assertTrue(
656                 mainResources.stream().anyMatch(sr -> sr.directory()
657                         .toString()
658                         .replace(File.separatorChar, '/')
659                         .contains("src/main/custom-resources")),
660                 "Should have custom main resources from <sources>");
661 
662         // Verify legacy Java directories are used as fallback
663         List<SourceRoot> mainJavaRoots = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.JAVA_FAMILY)
664                 .toList();
665         assertEquals(1, mainJavaRoots.size(), "Should have 1 main Java source (implicit fallback)");
666         assertTrue(
667                 mainJavaRoots
668                         .get(0)
669                         .directory()
670                         .toString()
671                         .replace(File.separatorChar, '/')
672                         .endsWith("src/main/java"),
673                 "Should use default src/main/java as fallback");
674 
675         List<SourceRoot> testJavaRoots = project.getEnabledSourceRoots(ProjectScope.TEST, Language.JAVA_FAMILY)
676                 .toList();
677         assertEquals(1, testJavaRoots.size(), "Should have 1 test Java source (implicit fallback)");
678         assertTrue(
679                 testJavaRoots
680                         .get(0)
681                         .directory()
682                         .toString()
683                         .replace(File.separatorChar, '/')
684                         .endsWith("src/test/java"),
685                 "Should use default src/test/java as fallback");
686     }
687 
688     /**
689      * Tests AC9 violation: Non-modular project with only resources in {@code <sources>} and explicit legacy directories.
690      * <p>
691      * AC9 allows implicit fallback to legacy directories (when they match defaults).
692      * When legacy directories differ from the default, this is explicit configuration,
693      * which violates AC9's "implicit" requirement, so an ERROR is raised.
694      *
695      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3897961755">Issue #11701 (AC8/AC9)</a>
696      */
697     @Test
698     void testNonModularResourcesOnlyWithExplicitLegacyDirectoriesRejected() throws Exception {
699         File pom = getProject("non-modular-resources-only-explicit-legacy");
700 
701         MavenSession mavenSession = createMavenSession(null);
702         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
703         configuration.setRepositorySession(mavenSession.getRepositorySession());
704 
705         ProjectBuildingResult result = getContainer()
706                 .lookup(org.apache.maven.project.ProjectBuilder.class)
707                 .build(pom, configuration);
708 
709         MavenProject project = result.getProject();
710 
711         // Verify ERRORs for explicit legacy directories (differ from default)
712         List<ModelProblem> errors = result.getProblems().stream()
713                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
714                 .filter(p -> p.getMessage().contains("Legacy") && p.getMessage().contains("cannot be used"))
715                 .toList();
716 
717         assertEquals(2, errors.size(), "Should have 2 errors for explicit legacy directories");
718         assertTrue(
719                 errors.stream().anyMatch(e -> e.getMessage().contains("<sourceDirectory>")),
720                 "Should error about <sourceDirectory>");
721         assertTrue(
722                 errors.stream().anyMatch(e -> e.getMessage().contains("<testSourceDirectory>")),
723                 "Should error about <testSourceDirectory>");
724 
725         // Verify resources from <sources> are still used
726         List<SourceRoot> mainResources = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
727                 .toList();
728         assertTrue(
729                 mainResources.stream().anyMatch(sr -> sr.directory()
730                         .toString()
731                         .replace(File.separatorChar, '/')
732                         .contains("src/main/custom-resources")),
733                 "Should have custom main resources from <sources>");
734 
735         // Verify NO Java source roots (legacy was rejected, none in <sources>)
736         List<SourceRoot> mainJavaRoots = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.JAVA_FAMILY)
737                 .toList();
738         assertEquals(0, mainJavaRoots.size(), "Should have no main Java sources (legacy rejected)");
739 
740         List<SourceRoot> testJavaRoots = project.getEnabledSourceRoots(ProjectScope.TEST, Language.JAVA_FAMILY)
741                 .toList();
742         assertEquals(0, testJavaRoots.size(), "Should have no test Java sources (legacy rejected)");
743     }
744 
745     /**
746      * Tests AC8: Modular project with Java in {@code <sources>} and physical default legacy directories.
747      * <p>
748      * Even when legacy directories use Super POM defaults (no explicit override),
749      * if the physical directories exist on the filesystem, an ERROR is raised.
750      * This is because modular projects use paths like {@code src/<module>/main/java},
751      * so content in {@code src/main/java} would be silently ignored.
752      *
753      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3897961755">Issue #11701 (AC8/AC9)</a>
754      */
755     @Test
756     void testModularWithPhysicalDefaultLegacyDirectory() throws Exception {
757         File pom = getProject("modular-with-physical-legacy");
758 
759         MavenSession mavenSession = createMavenSession(null);
760         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
761         configuration.setRepositorySession(mavenSession.getRepositorySession());
762 
763         ProjectBuildingResult result = getContainer()
764                 .lookup(org.apache.maven.project.ProjectBuilder.class)
765                 .build(pom, configuration);
766 
767         // Verify ERRORs are raised for physical presence of default directories (AC8)
768         List<ModelProblem> errors = result.getProblems().stream()
769                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
770                 .filter(p -> p.getMessage().contains("Legacy directory")
771                         && p.getMessage().contains("exists"))
772                 .toList();
773 
774         // Should have 2 errors: one for src/main/java, one for src/test/java
775         assertEquals(2, errors.size(), "Should have 2 errors for physical legacy directories");
776         // Use File.separator for platform-independent path matching (backslash on Windows)
777         String mainJava = "src" + File.separator + "main" + File.separator + "java";
778         String testJava = "src" + File.separator + "test" + File.separator + "java";
779         assertTrue(
780                 errors.stream().anyMatch(e -> e.getMessage().contains(mainJava)),
781                 "Should error about physical src/main/java");
782         assertTrue(
783                 errors.stream().anyMatch(e -> e.getMessage().contains(testJava)),
784                 "Should error about physical src/test/java");
785     }
786 
787     /**
788      * Tests AC8: Modular project with only resources in {@code <sources>} and physical default legacy directories.
789      * <p>
790      * Even when {@code <sources>} only contains resources (no Java), if the physical
791      * default directories exist, an ERROR is raised for modular projects.
792      * Unlike non-modular projects (AC9), modular projects cannot use legacy directories as fallback.
793      *
794      * @see <a href="https://github.com/apache/maven/issues/11701#issuecomment-3897961755">Issue #11701 (AC8/AC9)</a>
795      */
796     @Test
797     void testModularResourcesOnlyWithPhysicalDefaultLegacyDirectory() throws Exception {
798         File pom = getProject("modular-resources-only-with-physical-legacy");
799 
800         MavenSession mavenSession = createMavenSession(null);
801         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
802         configuration.setRepositorySession(mavenSession.getRepositorySession());
803 
804         ProjectBuildingResult result = getContainer()
805                 .lookup(org.apache.maven.project.ProjectBuilder.class)
806                 .build(pom, configuration);
807 
808         // Verify ERRORs are raised for physical presence of default directories (AC8)
809         // Unlike non-modular (AC9), modular projects cannot use legacy as fallback
810         List<ModelProblem> errors = result.getProblems().stream()
811                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
812                 .filter(p -> p.getMessage().contains("Legacy directory")
813                         && p.getMessage().contains("exists"))
814                 .toList();
815 
816         // Should have 2 errors: one for src/main/java, one for src/test/java
817         assertEquals(
818                 2, errors.size(), "Should have 2 errors for physical legacy directories (no AC9 fallback for modular)");
819         // Use File.separator for platform-independent path matching (backslash on Windows)
820         String mainJava = "src" + File.separator + "main" + File.separator + "java";
821         String testJava = "src" + File.separator + "test" + File.separator + "java";
822         assertTrue(
823                 errors.stream().anyMatch(e -> e.getMessage().contains(mainJava)),
824                 "Should error about physical src/main/java");
825         assertTrue(
826                 errors.stream().anyMatch(e -> e.getMessage().contains(testJava)),
827                 "Should error about physical src/test/java");
828     }
829 
830     /**
831      * Tests that mixing modular and non-modular sources within {@code <sources>} is not allowed.
832      * <p>
833      * A project must be either fully modular (all sources have a module) or fully classic
834      * (no sources have a module). Mixing them within the same project is not supported
835      * because the compiler plugin cannot handle such configurations.
836      * <p>
837      * This verifies:
838      * - An ERROR is reported when both modular and non-modular sources exist in {@code <sources>}
839      * - sourceDirectory is not used because {@code <sources>} exists
840      * <p>
841      * Acceptance Criteria:
842      * - AC1 (boolean flags eliminated - uses hasSources() for source detection)
843      * - AC6 (mixed sources error - mixing modular and classic sources within {@code <sources>}
844      *   triggers an ERROR)
845      *
846      * @see <a href="https://github.com/apache/maven/issues/11612">Issue #11612</a>
847      */
848     @Test
849     void testSourcesMixedModulesWithinSources() throws Exception {
850         File pom = getProject("sources-mixed-modules");
851 
852         MavenSession mavenSession = createMavenSession(null);
853         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
854         configuration.setRepositorySession(mavenSession.getRepositorySession());
855 
856         ProjectBuildingResult result = getContainer()
857                 .lookup(org.apache.maven.project.ProjectBuilder.class)
858                 .build(pom, configuration);
859 
860         // Verify an ERROR is reported for mixing modular and non-modular sources
861         List<ModelProblem> errors = result.getProblems().stream()
862                 .filter(p -> p.getSeverity() == ModelProblem.Severity.ERROR)
863                 .filter(p -> p.getMessage().contains("Mixed modular and classic sources"))
864                 .toList();
865 
866         assertEquals(1, errors.size(), "Should have 1 error for mixed modular/classic configuration");
867         assertTrue(errors.get(0).getMessage().contains("lang=java"), "Error should mention java language");
868         assertTrue(errors.get(0).getMessage().contains("scope=main"), "Error should mention main scope");
869     }
870 
871     /**
872      * Tests that multiple source directories for the same (lang, scope, module) combination
873      * are allowed and all are added as source roots.
874      * <p>
875      * This is a valid use case for Phase 2: users may have generated sources alongside regular sources,
876      * both belonging to the same module. Different directories = different identities = not duplicates.
877      * <p>
878      * Acceptance Criterion: AC2 (unified source tracking - multiple directories per module supported)
879      *
880      * @see <a href="https://github.com/apache/maven/issues/11612">Issue #11612</a>
881      */
882     @Test
883     void testMultipleDirectoriesSameModule() throws Exception {
884         File pom = getProject("multiple-directories-same-module");
885 
886         MavenSession session = createMavenSession(pom);
887         MavenProject project = session.getCurrentProject();
888 
889         // Get main Java source roots
890         List<SourceRoot> mainJavaRoots = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.JAVA_FAMILY)
891                 .toList();
892 
893         // Should have 2 main sources: both for com.example.app but different directories
894         assertEquals(2, mainJavaRoots.size(), "Should have 2 main Java source roots for same module");
895 
896         // Both should be for the same module
897         long moduleCount = mainJavaRoots.stream()
898                 .filter(sr -> "com.example.app".equals(sr.module().orElse(null)))
899                 .count();
900         assertEquals(2, moduleCount, "Both main sources should be for com.example.app module");
901 
902         // One should be implicit directory, one should be generated-sources
903         boolean hasImplicitDir = mainJavaRoots.stream().anyMatch(sr -> sr.directory()
904                 .toString()
905                 .replace(File.separatorChar, '/')
906                 .contains("src/com.example.app/main/java"));
907         boolean hasGeneratedDir = mainJavaRoots.stream().anyMatch(sr -> sr.directory()
908                 .toString()
909                 .replace(File.separatorChar, '/')
910                 .contains("target/generated-sources/com.example.app/java"));
911 
912         assertTrue(hasImplicitDir, "Should have implicit source directory for module");
913         assertTrue(hasGeneratedDir, "Should have generated-sources directory for module");
914 
915         // Get test Java source roots
916         List<SourceRoot> testJavaRoots = project.getEnabledSourceRoots(ProjectScope.TEST, Language.JAVA_FAMILY)
917                 .toList();
918 
919         // Should have 2 test sources: both for com.example.app
920         assertEquals(2, testJavaRoots.size(), "Should have 2 test Java source roots for same module");
921 
922         // Both test sources should be for the same module
923         long testModuleCount = testJavaRoots.stream()
924                 .filter(sr -> "com.example.app".equals(sr.module().orElse(null)))
925                 .count();
926         assertEquals(2, testModuleCount, "Both test sources should be for com.example.app module");
927     }
928 
929     /**
930      * Tests duplicate handling with enabled discriminator.
931      * <p>
932      * Test scenario:
933      * - Same (lang, scope, module, directory) with enabled=true appearing twice → triggers WARNING
934      * - Same identity with enabled=false → should be filtered out (disabled sources are no-ops)
935      * - Different modules should be added normally
936      * <p>
937      * Verifies:
938      * - First enabled source wins, subsequent duplicates trigger WARNING
939      * - Disabled sources don't count as duplicates
940      * - Different modules are unaffected
941      * <p>
942      * Acceptance Criteria:
943      * - AC3 (duplicate detection - duplicates trigger WARNING)
944      * - AC4 (first enabled wins - duplicates are skipped)
945      * - AC5 (disabled sources unchanged - still added but filtered by getEnabledSourceRoots)
946      *
947      * @see <a href="https://github.com/apache/maven/issues/11612">Issue #11612</a>
948      */
949     @Test
950     void testDuplicateEnabledSources() throws Exception {
951         File pom = getProject("duplicate-enabled-sources");
952 
953         MavenSession mavenSession = createMavenSession(null);
954         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
955         configuration.setRepositorySession(mavenSession.getRepositorySession());
956 
957         ProjectBuildingResult result = getContainer()
958                 .lookup(org.apache.maven.project.ProjectBuilder.class)
959                 .build(pom, configuration);
960 
961         MavenProject project = result.getProject();
962 
963         // Verify warnings are issued for duplicate enabled sources
964         List<ModelProblem> duplicateWarnings = result.getProblems().stream()
965                 .filter(p -> p.getSeverity() == ModelProblem.Severity.WARNING)
966                 .filter(p -> p.getMessage().contains("Duplicate enabled source"))
967                 .toList();
968 
969         // We have 2 duplicate pairs: main scope and test scope for com.example.dup
970         assertEquals(2, duplicateWarnings.size(), "Should have 2 duplicate warnings (main and test scope)");
971 
972         // Get main Java source roots
973         List<SourceRoot> mainJavaRoots = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.JAVA_FAMILY)
974                 .toList();
975 
976         // Should have 2 main sources: 1 for com.example.dup (first wins) + 1 for com.example.other
977         // Note: MavenProject.addSourceRoot still adds all sources, but tracking only counts first enabled
978         assertEquals(2, mainJavaRoots.size(), "Should have 2 main Java source roots");
979 
980         // Verify com.example.other module is present
981         boolean hasOtherModule = mainJavaRoots.stream()
982                 .anyMatch(sr -> "com.example.other".equals(sr.module().orElse(null)));
983         assertTrue(hasOtherModule, "Should have source root for com.example.other module");
984 
985         // Verify com.example.dup module is present (first enabled wins)
986         boolean hasDupModule = mainJavaRoots.stream()
987                 .anyMatch(sr -> "com.example.dup".equals(sr.module().orElse(null)));
988         assertTrue(hasDupModule, "Should have source root for com.example.dup module");
989 
990         // Get test Java source roots
991         List<SourceRoot> testJavaRoots = project.getEnabledSourceRoots(ProjectScope.TEST, Language.JAVA_FAMILY)
992                 .toList();
993 
994         // Test scope has 1 source for com.example.dup (first wins)
995         assertEquals(1, testJavaRoots.size(), "Should have 1 test Java source root");
996 
997         // Verify it's for the dup module
998         assertEquals(
999                 "com.example.dup",
1000                 testJavaRoots.get(0).module().orElse(null),
1001                 "Test source root should be for com.example.dup module");
1002     }
1003 
1004     @Test
1005     void testResourceTargetPathRemainsRelativeInCompatLayer() throws Exception {
1006         File pom = getProject("resource-target-path");
1007 
1008         MavenSession mavenSession = createMavenSession(null);
1009         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
1010         configuration.setRepositorySession(mavenSession.getRepositorySession());
1011 
1012         ProjectBuildingResult result = getContainer()
1013                 .lookup(org.apache.maven.project.ProjectBuilder.class)
1014                 .build(pom, configuration);
1015 
1016         MavenProject project = result.getProject();
1017 
1018         // Verify main resources via SourceRoot API
1019         List<SourceRoot> mainResources = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
1020                 .toList();
1021         assertEquals(1, mainResources.size());
1022         assertTrue(mainResources.get(0).targetPath().isPresent(), "Main resource should have a targetPath");
1023         assertFalse(
1024                 mainResources.get(0).targetPath().get().isAbsolute(),
1025                 "SourceRoot targetPath must be relative, got: "
1026                         + mainResources.get(0).targetPath().get());
1027         assertEquals(
1028                 Path.of("META-INF/tags/rdc"), mainResources.get(0).targetPath().get());
1029 
1030         // Verify test resources via SourceRoot API
1031         List<SourceRoot> testResources = project.getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)
1032                 .toList();
1033         assertEquals(1, testResources.size());
1034         assertTrue(testResources.get(0).targetPath().isPresent(), "Test resource should have a targetPath");
1035         assertFalse(
1036                 testResources.get(0).targetPath().get().isAbsolute(),
1037                 "SourceRoot targetPath must be relative, got: "
1038                         + testResources.get(0).targetPath().get());
1039         assertEquals(
1040                 Path.of("org/apache/maven/messages"),
1041                 testResources.get(0).targetPath().get());
1042 
1043         // Verify compat layer: MavenProject.getResources() must return relative targetPath
1044         List<org.apache.maven.model.Resource> resources = project.getResources();
1045         assertEquals(1, resources.size());
1046         assertEquals(
1047                 "META-INF" + File.separator + "tags" + File.separator + "rdc",
1048                 resources.get(0).getTargetPath(),
1049                 "Resource targetPath from getResources() must remain relative");
1050 
1051         // Verify compat layer: MavenProject.getTestResources() must return relative targetPath
1052         List<org.apache.maven.model.Resource> testResourceList = project.getTestResources();
1053         assertEquals(1, testResourceList.size());
1054         assertEquals(
1055                 "org" + File.separator + "apache" + File.separator + "maven" + File.separator + "messages",
1056                 testResourceList.get(0).getTargetPath(),
1057                 "Test resource targetPath from getTestResources() must remain relative");
1058     }
1059 
1060     @Test
1061     void testSourceTargetPathRemainsRelative() throws Exception {
1062         File pom = getProject("source-target-path");
1063 
1064         MavenSession mavenSession = createMavenSession(null);
1065         ProjectBuildingRequest configuration = new DefaultProjectBuildingRequest();
1066         configuration.setRepositorySession(mavenSession.getRepositorySession());
1067 
1068         ProjectBuildingResult result = getContainer()
1069                 .lookup(org.apache.maven.project.ProjectBuilder.class)
1070                 .build(pom, configuration);
1071 
1072         MavenProject project = result.getProject();
1073 
1074         // Verify main resources have relative targetPath preserved
1075         List<SourceRoot> mainResources = project.getEnabledSourceRoots(ProjectScope.MAIN, Language.RESOURCES)
1076                 .toList();
1077         assertEquals(1, mainResources.size());
1078         assertTrue(mainResources.get(0).targetPath().isPresent());
1079         assertEquals(Path.of(".grammar"), mainResources.get(0).targetPath().get());
1080 
1081         // Verify test resources have relative targetPath preserved
1082         List<SourceRoot> testResources = project.getEnabledSourceRoots(ProjectScope.TEST, Language.RESOURCES)
1083                 .toList();
1084         assertEquals(1, testResources.size());
1085         assertTrue(testResources.get(0).targetPath().isPresent());
1086         assertEquals(Path.of("META-INF/test"), testResources.get(0).targetPath().get());
1087 
1088         // Verify the compat layer also returns relative targetPath
1089         List<org.apache.maven.model.Resource> resources = project.getResources();
1090         assertEquals(1, resources.size());
1091         assertEquals(".grammar", resources.get(0).getTargetPath());
1092 
1093         List<org.apache.maven.model.Resource> testResourceList = project.getTestResources();
1094         assertEquals(1, testResourceList.size());
1095         assertEquals(
1096                 "META-INF" + File.separator + "test", testResourceList.get(0).getTargetPath());
1097     }
1098 }