-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix resource targetPath resolution to be relative to output directory (fixes #11381) #11394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gnodet
merged 10 commits into
apache:master
from
gnodet:fix-gh-11381-resource-targetpath
Nov 7, 2025
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eb23cfc
[GH-11381] Fix resource targetPath resolution to be relative to outpu…
gnodet b64a8d5
Fix ConnectedResource to handle null output directories and compute r…
gnodet e6ba7cf
Simplify targetPath handling by keeping it as relative path
gnodet 2b154b2
Remove unnecessary outputDir parameter from MavenProject and DefaultP…
gnodet 708e059
Fix resource targetPath handling according to PR feedback
gnodet c747855
Fix GH-11381: Ensure resource targetPath is relative to output directory
gnodet 642e43b
Improve SourceRoot javadoc to clarify targetPath behavior
gnodet da122e1
Simplify
gnodet 10734c5
Enhance javadoc for SourceRoot.targetPath() and Project.getOutputDire…
gnodet e14828b
Further refine Maven 4 API javadoc for explicit clarity
gnodet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...core-it-suite/src/test/java/org/apache/maven/it/MavenITgh11381ResourceTargetPathTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * 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.it; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * This is a test set for <a href="https://github.com/apache/maven/issues/11381">GH-11381</a>. | ||
| * | ||
| * Verifies that relative targetPath in resources is resolved relative to the output directory | ||
| * (target/classes) and not relative to the project base directory, maintaining Maven 3.x behavior. | ||
| * | ||
| * @since 4.0.0-rc-4 | ||
| */ | ||
| class MavenITgh11381ResourceTargetPathTest extends AbstractMavenIntegrationTestCase { | ||
|
|
||
| /** | ||
| * Verify that resources with relative targetPath are copied to target/classes/targetPath | ||
| * and not to the project root directory. | ||
| * | ||
| * @throws Exception in case of failure | ||
| */ | ||
| @Test | ||
| void testRelativeTargetPathInResources() throws Exception { | ||
| File testDir = extractResources("/gh-11381"); | ||
|
|
||
| Verifier verifier = newVerifier(testDir.getAbsolutePath()); | ||
| verifier.setAutoclean(false); | ||
| verifier.deleteDirectory("target"); | ||
| verifier.addCliArgument("process-resources"); | ||
| verifier.execute(); | ||
| verifier.verifyErrorFreeLog(); | ||
|
|
||
| // Verify that resources were copied to target/classes/target-dir (Maven 3.x behavior) | ||
| verifier.verifyFilePresent("target/classes/target-dir/test.yml"); | ||
| verifier.verifyFilePresent("target/classes/target-dir/subdir/another.yml"); | ||
|
|
||
| // Verify that resources were NOT copied to the project root target-dir directory | ||
| verifier.verifyFileNotPresent("target-dir/test.yml"); | ||
| verifier.verifyFileNotPresent("target-dir/subdir/another.yml"); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?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.gh11381</groupId> | ||
| <artifactId>test</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <name>Maven Integration Test :: GH-11381</name> | ||
| <description>Test for relative targetPath in resources - should be relative to output directory</description> | ||
|
|
||
| <build> | ||
| <resources> | ||
| <resource> | ||
| <directory>${project.basedir}/rest</directory> | ||
| <targetPath>target-dir</targetPath> | ||
| <includes> | ||
| <include>**/*.yml</include> | ||
| </includes> | ||
| </resource> | ||
| </resources> | ||
| </build> | ||
| </project> | ||
|
|
4 changes: 4 additions & 0 deletions
4
its/core-it-suite/src/test/resources/gh-11381/rest/subdir/another.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Another test YAML file for GH-11381 | ||
| another: | ||
| test: data | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Test YAML file for GH-11381 | ||
| test: | ||
| key: value | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is CWD relative?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Path.of(String)creates a path relative the to current working directory, which is not the desired output directory. I think thatDefaultSourceRootis okay, and the change rather needs to be applied in the codes that call this constructor. The problem seems to be in the following classes:org.apache.maven.project.ConnectedResourceline 124.org.apache.maven.project.DefaultProjectBuilderlines 704 and 707.org.apache.maven.project.MavenProjectline 840The above-cited codes set the first parameter to
project.getBaseDirectory()while it should beproject.getBuild().getOutputDirectory().Note: the
baseDirparameter name ofDefaultSourceRootis misleading. The Javadoc said "the base directory for resolving relative paths", which can be thebaseDirof the Maven project but not necessarily. It should be understood as any base directory for resolving paths. This parameter could be renamedparentDirifbaseDiris too misleading.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is maybe
resource.getTargetPath()absolute? So CWD does not matter?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be absolute, but I'm not sure that this guaranteed. It may depend on which code path as leaded to this constructor. I don't think that there is any reason to not be robust. We just need to find all calls to this constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And maybe rename the
baseDirparameter asoutputDir(but without change in the call toPath.resolve(String)).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree, but the replacement of
baseDir::resolvebyPath::ofdoes not fix that. The correction is rather to fix thebaseDirargument given in calls to theDefaultSourceRootconstructor. Actually, the first commit of this pull request seems correct to me, but it seems to have been reverted in subsequent commits.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think it does not ?
It makes the path truly relative, and later resolved against the correct directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. The responsibility of resolving the path is shifted to the
SourceRootuser instead of theSourceRootconstructor. But why not resolving at construction time? It would probably be less code to check for correctness.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I think that would be desired behavior, but then we need to make it truly relative in the ConnectedResource to not break the existing plugin.
So the behavior would be different between mvn3 and mvn4 api, but that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the difference would be that mvn3 would be relative to
targetwhile mvn4 would be relative tobaseDir, I think that this difference was a bug partially fixed in #11322, but we forgot the case of resources in that fix.I will prepare an alternative pull request tomorrow for illustrating the fix that I propose. The actual pull request at the end may be, possibly, a mix of the two.