Skip to content

[maven-4.0.x] Fix #12306: normalize targetPath in DefaultSourceRoot - #12307

Merged
gnodet merged 1 commit into
maven-4.0.xfrom
fix-12306-targetpath-dot
Jun 18, 2026
Merged

gnodet merged 1 commit into
maven-4.0.xfrom
fix-12306-targetpath-dot

Conversation

@gnodet

@gnodet gnodet commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Problem

When a resource is declared with <targetPath>.</targetPath>, Maven 4 throws a NoSuchFileException. Closes #12306.

Root Cause

In DefaultSourceRoot's canonical constructor, the incoming targetPathOrNull was normalized via Path.normalize() and stored as-is. On JDK 21 (Linux), Path.of(".").normalize() returns a Path whose toString() is "" (empty string) but whose getNameCount() is 1. 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 causing NoSuchFileException.

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 a targetPath.

Changes

  • impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultSourceRoot.java — normalize-and-nullify logic
  • impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultSourceRootTest.java — two new tests:
    • testHandlesDotTargetPathFromResource — verifies "." is treated as absent
    • testHandlesDotRelativeTargetPathFromResource — verifies "./subdir" normalizes to "subdir" and is preserved

Test plan

  • mvn -pl impl/maven-impl -am clean test -DskipITs -Drat.skip=true passes (453 tests, 0 failures)
  • New tests testHandlesDotTargetPathFromResource and testHandlesDotRelativeTargetPathFromResource pass

🤖 Generated with Claude Code

`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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DefaultSourceRoot to normalize targetPath, then nullify it when normalization yields an empty-string path.
  • Add unit tests covering targetPath="." (treated as absent) and targetPath="./subdir" (normalized to subdir and 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.

@gnodet gnodet changed the title Fix NoSuchFileException when resource targetPath is "." [maven-4.0.x] Fix #12306: normalize targetPath in DefaultSourceRoot Jun 18, 2026
@gnodet gnodet added the bug Something isn't working label Jun 18, 2026
@gnodet gnodet self-assigned this Jun 18, 2026
@gnodet gnodet added this to the 4.0.0-rc-6 milestone Jun 18, 2026
@gnodet
gnodet merged commit 19cdb5e into maven-4.0.x Jun 18, 2026
25 checks passed
@gnodet
gnodet deleted the fix-12306-targetpath-dot branch June 18, 2026 12:20
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants