[maven-4.0.x] Fix #12306: normalize targetPath in DefaultSourceRoot - #12307
Merged
Merged
Conversation
`Path.of(".").normalize()` returns a path whose `toString()` is empty
but `getNameCount()` is 1, which was subsequently passed to plugins as
an empty string, causing the plugin to construct an absolute path
(`/org/apache/...`) and a NoSuchFileException.
Treat any targetPath that normalizes to an empty string the same as a
null (absent) targetPath.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Maven 4 regression where a resource <targetPath>.</targetPath> could be normalized into a “blank” Path that resolves unexpectedly (e.g., to /org/apache/...), leading to NoSuchFileException during resource copying. The change ensures that a normalized target path with an empty string representation is treated as “absent”, restoring Maven 3.x-equivalent behavior.
Changes:
- Adjust
DefaultSourceRootto normalizetargetPath, then nullify it when normalization yields an empty-string path. - Add unit tests covering
targetPath="."(treated as absent) andtargetPath="./subdir"(normalized tosubdirand preserved).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java | Nullifies normalized empty-string targetPath to avoid erroneous path resolution/root writes. |
| impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultSourceRootTest.java | Adds regression tests for "." and "./subdir" targetPath normalization behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cstamas
approved these changes
Jun 18, 2026
gnodet
added a commit
that referenced
this pull request
Jun 18, 2026
) (#12312) `Path.of(".").normalize()` returns a path whose `toString()` is empty but `getNameCount()` is 1, which was subsequently passed to plugins as an empty string, causing the plugin to construct an absolute path (`/org/apache/...`) and a NoSuchFileException. Treat any targetPath that normalizes to an empty string the same as a null (absent) targetPath. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When a resource is declared with
<targetPath>.</targetPath>, Maven 4 throws aNoSuchFileException. Closes #12306.Root Cause
In
DefaultSourceRoot's canonical constructor, the incomingtargetPathOrNullwas normalized viaPath.normalize()and stored as-is. On JDK 21 (Linux),Path.of(".").normalize()returns aPathwhosetoString()is""(empty string) but whosegetNameCount()is1. This empty-string path was then passed to plugins, which concatenated it into an absolute path like/org/apache/…, bypassing the output directory entirely and causingNoSuchFileException.Fix
After normalizing the target path, treat any path whose
toString()is empty (which includes"."after normalization) the same as a null/absent target path. This preserves the Maven 3.x behavior where<targetPath>.</targetPath>is equivalent to not specifying atargetPath.Changes
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java— normalize-and-nullify logicimpl/maven-impl/src/test/java/org/apache/maven/impl/DefaultSourceRootTest.java— two new tests:testHandlesDotTargetPathFromResource— verifies"."is treated as absenttestHandlesDotRelativeTargetPathFromResource— verifies"./subdir"normalizes to"subdir"and is preservedTest plan
mvn -pl impl/maven-impl -am clean test -DskipITs -Drat.skip=truepasses (453 tests, 0 failures)testHandlesDotTargetPathFromResourceandtestHandlesDotRelativeTargetPathFromResourcepass🤖 Generated with Claude Code