Skip to content

Commit 708e059

Browse files
committed
Fix resource targetPath handling according to PR feedback
Based on PR comments, the solution has been updated: 1. SourceRoot.targetPath() returns a Path relative to the project's basedir (e.g., 'target/classes/custom-output') - Reverted DefaultSourceRoot to resolve targetPath against baseDir 2. ConnectedResource.getTargetPath() (Maven 3 API) returns a path relative to the output directory (e.g., 'custom-output') - Added computeRelativeTargetPath() method to make the path relative to the output directory when converting from SourceRoot to Resource This maintains backward compatibility with Maven 3 API while using the new Maven 4 API internally. Changes: - DefaultSourceRoot: Reverted to resolve targetPath against baseDir - ConnectedResource: Added computeRelativeTargetPath() to convert from basedir-relative to outputdir-relative paths - DefaultSourceRootTest: Updated tests to expect basedir-relative paths - ResourceIncludeTest: Tests still pass with the new conversion logic
1 parent 2b154b2 commit 708e059

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

impl/maven-core/src/main/java/org/apache/maven/project/ConnectedResource.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,53 @@ class ConnectedResource extends Resource {
4242
.includes(sourceRoot.includes())
4343
.excludes(sourceRoot.excludes())
4444
.filtering(Boolean.toString(sourceRoot.stringFiltering()))
45-
.targetPath(sourceRoot.targetPath().map(Path::toString).orElse(null))
45+
.targetPath(computeRelativeTargetPath(sourceRoot, scope, project))
4646
.build());
4747
this.originalSourceRoot = sourceRoot;
4848
this.scope = scope;
4949
this.project = project;
5050
}
5151

52+
/**
53+
* Computes the targetPath relative to the output directory.
54+
* In Maven 3 API, Resource.getTargetPath() is expected to be relative to the output directory
55+
* (e.g., "custom-output"), while SourceRoot.targetPath() is relative to the project basedir
56+
* (e.g., "target/classes/custom-output").
57+
*/
58+
private static String computeRelativeTargetPath(SourceRoot sourceRoot, ProjectScope scope, MavenProject project) {
59+
return sourceRoot
60+
.targetPath()
61+
.map(targetPath -> {
62+
// Get the output directory for this scope
63+
String outputDir = scope == ProjectScope.MAIN
64+
? project.getBuild().getOutputDirectory()
65+
: project.getBuild().getTestOutputDirectory();
66+
Path outputDirPath = Path.of(outputDir);
67+
68+
// If targetPath is absolute, try to make it relative to the output directory
69+
if (targetPath.isAbsolute()) {
70+
if (targetPath.startsWith(outputDirPath)) {
71+
return outputDirPath.relativize(targetPath).toString();
72+
}
73+
return targetPath.toString();
74+
}
75+
76+
// If targetPath is relative, check if it starts with the output directory
77+
// (e.g., "target/classes/custom-output" should become "custom-output")
78+
Path baseDir = project.getBaseDirectory();
79+
Path resolvedTargetPath = baseDir.resolve(targetPath);
80+
Path resolvedOutputDir = baseDir.resolve(outputDirPath);
81+
82+
if (resolvedTargetPath.startsWith(resolvedOutputDir)) {
83+
return resolvedOutputDir.relativize(resolvedTargetPath).toString();
84+
}
85+
86+
// Otherwise, return as-is
87+
return targetPath.toString();
88+
})
89+
.orElse(null);
90+
}
91+
5292
@Override
5393
public void addInclude(String include) {
5494
// Update the underlying Resource model

impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public DefaultSourceRoot(final Path baseDir, ProjectScope scope, Resource resour
169169
resource.getIncludes(),
170170
resource.getExcludes(),
171171
Boolean.parseBoolean(resource.getFiltering()),
172-
nonBlank(resource.getTargetPath()).map(Path::of).orElse(null),
172+
nonBlank(resource.getTargetPath()).map(baseDir::resolve).orElse(null),
173173
true);
174174
}
175175

impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultSourceRootTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ void testExtractsTargetPathFromResource() {
197197

198198
Optional<Path> targetPath = sourceRoot.targetPath();
199199
assertTrue(targetPath.isPresent(), "targetPath should be present");
200-
assertEquals(Path.of("test-output"), targetPath.get(), "targetPath should be kept as relative path");
200+
assertEquals(
201+
Path.of("myproject/test-output"), targetPath.get(), "targetPath should be resolved against baseDir");
201202
assertEquals(Path.of("myproject", "src", "test", "resources"), sourceRoot.directory());
202203
assertEquals(ProjectScope.TEST, sourceRoot.scope());
203204
assertEquals(Language.RESOURCES, sourceRoot.language());
@@ -246,9 +247,9 @@ void testHandlesPropertyPlaceholderInTargetPath() {
246247
Optional<Path> targetPath = sourceRoot.targetPath();
247248
assertTrue(targetPath.isPresent(), "Property placeholder targetPath should be present");
248249
assertEquals(
249-
Path.of("${project.build.directory}/custom"),
250+
Path.of("myproject/${project.build.directory}/custom"),
250251
targetPath.get(),
251-
"Property placeholder should be preserved as relative path");
252+
"Property placeholder should be resolved against baseDir");
252253
}
253254

254255
/*GH-11381*/
@@ -280,9 +281,9 @@ void testResourceConstructorPreservesOtherProperties() {
280281

281282
// Verify all properties are preserved
282283
assertEquals(
283-
Path.of("test-classes"),
284+
Path.of("myproject/test-classes"),
284285
sourceRoot.targetPath().orElseThrow(),
285-
"targetPath should be kept as relative path");
286+
"targetPath should be resolved against baseDir");
286287
assertTrue(sourceRoot.stringFiltering(), "Filtering should be true");
287288
assertEquals(1, sourceRoot.includes().size());
288289
assertTrue(sourceRoot.includes().contains("*.properties"));

0 commit comments

Comments
 (0)