Skip to content

Fix NullPointerException in Antlr4MojoTest by downgrading Maven API to 3.6.3#4947

Open
Nokhrin wants to merge 2 commits into
antlr:devfrom
Nokhrin:fix/maven-plugin-tests-npe
Open

Fix NullPointerException in Antlr4MojoTest by downgrading Maven API to 3.6.3#4947
Nokhrin wants to merge 2 commits into
antlr:devfrom
Nokhrin:fix/maven-plugin-tests-npe

Conversation

@Nokhrin

@Nokhrin Nokhrin commented Jun 12, 2026

Copy link
Copy Markdown

Problem

Running mvn test (or mvn package without -DskipTests) in the
antlr4-maven-plugin module fails with NullPointerException in all
4 tests of Antlr4MojoTest:

java.lang.NullPointerException: Cannot invoke
  "org.apache.maven.model.Plugin.getGroupId()" because the return value
  of "org.apache.maven.plugin.MojoExecution.getPlugin()" is null

Root cause: The module depends on takari-plugin-testing:3.0.0,
which was last released in 2018 and relies on internal Maven APIs that
were changed in Maven 3.8+. Specifically, MojoExecution.getPlugin()
now returns null in the test harness, triggering the NPE in
DefaultMojoExecutionConfigurator.

Solution

Downgrade the mavenVersion property in
antlr4-maven-plugin/pom.xml from 3.8.5 to 3.6.3. This is the
last Maven version that is compatible with takari-plugin-testing:3.0.0.

The plugin itself continues to work with modern Maven installations
— this change only affects the version of Maven API used by the test
harness.

Before

<properties>
    <mavenVersion>3.8.5</mavenVersion>
</properties>

After

<properties>
    <mavenVersion>3.6.3</mavenVersion>
</properties>

Verification

Before the fix

Running mvn test -pl antlr4-maven-plugin resulted in:

[ERROR] Tests run: 4, Failures: 0, Errors: 4, Skipped: 0
[ERROR]   Antlr4MojoTest.importTokens:59 » NullPointer Cannot invoke 
  "org.apache.maven.model.Plugin.getGroupId()" because the return value 
  of "MojoExecution.getPlugin()" is null
[ERROR] BUILD FAILURE

After the fix

After downgrading mavenVersion from 3.8.5 to 3.6.3:

$ mvn test -pl antlr4-maven-plugin

Result:

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.antlr.mojo.antlr4.Antlr4MojoTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.495 s
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

All 4 tests that previously failed with NullPointerException now pass successfully:

  • importTokens
  • importsCustomLayout
  • importsStandardLayout
  • processWhenDependencyRemoved

Note

A more thorough fix would be to migrate from the abandoned
takari-plugin-testing:3.0.0 to the official Apache
maven-plugin-testing-harness:3.3.0, which is actively maintained
and compatible with modern Maven versions. However, such a migration
would require significant refactoring of the test suite:

  • TestMavenRuntime and TestResources (JUnit 4 @Rule-based API)
    would need to be replaced with LookupMojo and PlexusTestCase
    from the Apache harness.
  • All 4 tests (~350 lines) would need to be rewritten.
  • The test projects under src/test/projects/ might require
    adjustments to their POM structures.

This minimal fix restores the CI pipeline without introducing the
risk of breaking the test suite. The migration to
maven-plugin-testing-harness is recommended as a follow-up task
but is outside the scope of this PR.

Nokhrin added 2 commits June 12, 2026 11:31
- Fix typo in class name: 'Intrepreter' -> 'Interpreter'
- Replace abstract 'input-filename(s)' with 'source-code-file(s)' to
  clarify that the tool expects plain text source code files, not
  compiled binaries.
- Update the stdin fallback message to use the new terminology and
  replace the ambiguous word 'rig' with 'the tool'.

Signed-off-by: Aleksandr Nokhrin <a.v.nokhrin@yandex.ru>
Signed-off-by: Aleksandr Nokhrin <a.v.nokhrin@yandex.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant