Skip to content

Throw MavenParsingException when a POM is missing groupId or version#7480

Merged
timtebeek merged 1 commit intomainfrom
fix/rawpom-missing-coordinates
May 4, 2026
Merged

Throw MavenParsingException when a POM is missing groupId or version#7480
timtebeek merged 1 commit intomainfrom
fix/rawpom-missing-coordinates

Conversation

@steve-aom-elliott
Copy link
Copy Markdown
Contributor

What

RawPom.toPom calls Objects.requireNonNull on the project's groupId and version (RawPom.java:437,439). When either is absent, the resulting NullPointerException propagates out of mod build / MavenParser.parseInputs with no artifact context, only this stack:

Caused by: java.lang.NullPointerException
    at java.util.Objects.requireNonNull (Objects.java:203)
    at org.openrewrite.maven.internal.RawPom.toPom (RawPom.java:437)
    at org.openrewrite.maven.internal.MavenPomDownloader.download (MavenPomDownloader.java:595)
    at org.openrewrite.maven.tree.ResolvedPom.resolveDependencies (ResolvedPom.java:1050)
    ...

There is no signal as to which transitive dependency's POM was malformed.

Why

A POM can legitimately omit a <groupId> or <version> element (relying on <parent> inheritance), but it can also be invalid — for example:

  • a stub POM uploaded to an internal Artifactory/Nexus alongside a vendored JAR via mvn install:install-file or REST upload (which bypasses ModelValidator)
  • a POM emitted by non-Maven tooling that wrote <groupId/> or <groupId></groupId> instead of omitting the element (Jackson-XML's EMPTY_ELEMENT_AS_NULL deserializes those to null)
  • a POM with whitespace-only content like <version> </version> (which MavenXmlMapper's StringTrimModule reduces to "", then sailed past every downstream null-check)

In each of these cases the resolver cannot construct a usable Pom, but the user has no way of telling which artifact is the source.

How

Two contained changes inside RawPom:

  1. getGroupId() and getVersion() now treat blank (null or whitespace-only) values as missing, so an empty <groupId/> or <version> </version> is treated the same as an omitted element. Scope is limited to these two methods; nothing outside RawPom is affected.

  2. toPom() checks the resolved coordinates and throws MavenParsingException (already used at sibling validation points in ResolvedPom) naming the artifact and source repository:

    POM is missing a required coordinate: groupId for {null:my-app:1} (from https://repo.example.com/...)
    

MavenParsingException is unchecked, so no signature changes ripple out to MavenPomDownloader, MavenParser, Assertions, or tests.

Tests

Added five tests to RawPomTest:

  • missingGroupIdThrowsParsingException
  • missingVersionThrowsParsingException
  • emptyGroupIdElementThrowsParsingException
  • whitespaceVersionElementThrowsParsingException
  • groupIdInheritedFromParentStillResolves (regression check that legitimate parent inheritance still works)

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Apr 27, 2026
@steve-aom-elliott steve-aom-elliott added bug Something isn't working maven test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail labels Apr 27, 2026
@steve-aom-elliott steve-aom-elliott marked this pull request as ready for review April 27, 2026 18:25
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Apr 27, 2026
RawPom.toPom previously called Objects.requireNonNull on the project's
groupId and version, producing a context-free NullPointerException when
either coordinate was absent. The NPE bubbled all the way out of
mod build / parse without naming the offending artifact, making
diagnosis painful.

Treat blank (null or whitespace-only) values as missing in
getGroupId / getVersion, and throw MavenParsingException naming the
artifact and source repository when either coordinate cannot be
resolved from the POM or its <parent> element.
@steve-aom-elliott steve-aom-elliott force-pushed the fix/rawpom-missing-coordinates branch from e094931 to c8ac0ec Compare April 27, 2026 21:53
Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Great to capture these with more details, thanks!

@timtebeek timtebeek merged commit 6025024 into main May 4, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite May 4, 2026
@timtebeek timtebeek deleted the fix/rawpom-missing-coordinates branch May 4, 2026 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working maven test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants