Skip to content

Fix NPE when Model.getLocation() returns null#2139

Open
EricBlanquer wants to merge 1 commit intoeclipse-m2e:mainfrom
EricBlanquer:fix/null-input-location-npe
Open

Fix NPE when Model.getLocation() returns null#2139
EricBlanquer wants to merge 1 commit intoeclipse-m2e:mainfrom
EricBlanquer:fix/null-input-location-npe

Conversation

@EricBlanquer
Copy link

Summary

Maven core extensions such as maven-git-versioning-extension modify the Maven model at runtime without preserving InputLocation metadata. This causes Model.getLocation("") to return null, leading to NullPointerException during project import and update.

Stack trace:

java.lang.NullPointerException: Cannot invoke "org.apache.maven.model.InputLocation.getSource()"
  because the return value of "org.apache.maven.model.Model.getLocation(Object)" is null
    at o.e.m2e.core.internal.lifecyclemapping.AnnotationMappingMetadataSource.parsePIs(AnnotationMappingMetadataSource.java:174)
java.lang.NullPointerException: Cannot invoke "org.apache.maven.model.InputLocation.getSource()"
  because "inputLocation" is null
    at o.e.m2e.core.internal.markers.SourceLocationHelper.getSourceLocation(SourceLocationHelper.java:243)

Changes

  • AnnotationMappingMetadataSource: add null checks in constructor and parsePIs() for Model.getLocation() and InputLocation.getSource()
  • SourceLocationHelper: add null checks in getSourceLocation(), findLocation() and getMavenDependency() for all getLocation(SELF) dereferences

Reproducer

Use any Maven core extension declared in .mvn/extensions.xml (e.g. maven-git-versioning-extension) and try to import or update a Maven project in Eclipse.

Fixes #1310

@laeubi
Copy link
Member

laeubi commented Feb 19, 2026

Use any Maven core extension declared in .mvn/extensions.xml (e.g. maven-git-versioning-extension) and try to import or update a Maven project in Eclipse.

We already have some tests that use extensions (mostly Tycho), so would you mind to try adding such test for the maven-git-versioning-extension (I don't think it is reproducible with any extension obviously)?

Beside that you must sign the Eclipse ECA so we can accept your contribution.

@EricBlanquer EricBlanquer force-pushed the fix/null-input-location-npe branch from cb0329f to 04ff530 Compare February 19, 2026 10:03
Maven core extensions such as maven-git-versioning-extension can
modify the Maven model at runtime without preserving InputLocation
metadata. This causes Model.getLocation("") to return null, leading
to NullPointerException in AnnotationMappingMetadataSource and
SourceLocationHelper.

Add null checks for InputLocation and InputSource in all code paths
that dereference Model.getLocation() results.

Fixes eclipse-m2e#1310
@EricBlanquer EricBlanquer force-pushed the fix/null-input-location-npe branch from 04ff530 to e60e738 Compare February 19, 2026 10:18
@EricBlanquer
Copy link
Author

EricBlanquer commented Feb 19, 2026

@laeubi I've added a test (testGitVersioningExtension in ExtensionsTest) that imports a project using maven-git-versioning-extension as a core extension. The test initializes a git repo in the workspace (required for the extension to activate and modify the model), then imports the project and asserts no errors.

Proof that the fix prevents the NPE

Without the fix, when maven-git-versioning-extension modifies the model, Model.getLocation("") returns null:

AnnotationMappingMetadataSource constructor (line 82):

projectId = project.getModel().getLocation(SELF).getSource().getModelId();
//                              ↑ returns null → NPE on .getSource()

AnnotationMappingMetadataSource.parsePIs() (line 174):

InputSource source = project.getModel().getLocation(SELF).getSource();
//                                      ↑ returns null → NPE

SourceLocationHelper (line 120):

if(pomFile.getAbsolutePath().equals(inputLocation.getSource().getLocation()))
//                                                ↑ getSource() is null → NPE

With the fix, null checks and graceful fallbacks prevent the crash:

InputLocation selfLocation = project.getModel().getLocation(SELF);
if(selfLocation != null && selfLocation.getSource() != null) {
    projectId = selfLocation.getSource().getModelId();
} else {
    projectId = project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion();
}

All SourceLocationHelper code paths now check for null on getSource(), getParent(), and getLocation() before dereferencing.

Also, my ECA is now valid and up to date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extensions in extensions.xml cause m2e not to update project in Eclipse

2 participants