Skip to content

Fix bootWar NPE from stray CLI WebApplicationInitializer (#15377) - #15815

Closed
jamesfredley wants to merge 2 commits into
8.0.xfrom
feat/8.0.x-dbmigration-bootwar-npe-15377
Closed

jamesfredley wants to merge 2 commits into
8.0.xfrom
feat/8.0.x-dbmigration-bootwar-npe-15377

Conversation

@jamesfredley

Copy link
Copy Markdown
Contributor

Summary

Fixes the Cannot invoke "String.split(String)" because "sources" is null NPE that crashes a classic servlet-container (Tomcat) WAR deployment of a Grails 8 application using the Database Migration plugin.

Root cause

Both Database Migration plugins - grails-data-hibernate5-dbmigration and grails-data-hibernate7-dbmigration - depend on grails-shell-cli at implementation scope. That places org.grails.cli.boot.SpringApplicationWebApplicationInitializer on the classpath of a normal application WAR.

That class is a SpringBootServletInitializer (a WebApplicationInitializer) intended only for CLI-packaged WARs produced by the shell war command (WarCommand), which records the application source classes in the WAR manifest via the Spring-Application-Source-Classes entry.

When a standard bootWar is deployed to an external servlet container such as Tomcat, Spring's SpringServletContainerInitializer discovers the stray initializer and invokes onStartup. A normal WAR manifest has no Spring-Application-Source-Classes entry, so sources was null and sources.split(",") threw an NPE, crashing context startup. The application's own SpringBootServletInitializer never got a chance to bootstrap.

Fix

Make the initializer inert when the manifest source-classes entry is absent: onStartup now returns before super.onStartup(...) instead of throwing, so a standard WAR is unaffected and the application's own initializer bootstraps it normally. The CLI-packaged WAR path is unchanged because that manifest entry is always present there.

This single fix in the shared grails-shell-cli module resolves the issue for both dbmigration plugins, and - importantly - requires no changes in consuming applications (avoiding the developmentOnly / feature-variant / separate-artifact options that would each require app-side build changes).

Changes

  • SpringApplicationWebApplicationInitializer.java: early-return when there are no source classes; getManifest returning null and a blank/absent entry now yield null (inert) instead of throwing.
  • SpringApplicationWebApplicationInitializerSpec.groovy (new): regression test covering a missing manifest, a manifest without the entry, and a manifest with an unrelated entry - all must not throw.
  • grails-shell-cli/build.gradle: testImplementation for spring-boot + spring-web (needed to load the class under test; they are compileOnly in the main source set).

Verification

  • ./gradlew :grails-shell-cli:test --tests "org.grails.cli.boot.SpringApplicationWebApplicationInitializerSpec" - BUILD SUCCESSFUL, all cases pass.
  • ./gradlew clean aggregateViolations - Checkstyle, CodeNarc, PMD, and SpotBugs all report no violations.

Fixes #15377

The Database Migration plugins for Hibernate 5 and Hibernate 7 depend on
grails-shell-cli at implementation scope, which places
org.grails.cli.boot.SpringApplicationWebApplicationInitializer on the
classpath of a normal application WAR. That class is a
SpringBootServletInitializer intended only for CLI-packaged WARs built by
the shell 'war' command, which records the application source classes in the
WAR manifest via the 'Spring-Application-Source-Classes' entry.

When a standard bootWar is deployed to an external servlet container such as
Tomcat, Spring discovers the stray initializer and invokes onStartup. The
manifest has no source-classes entry, so sources was null and split(",")
threw a NullPointerException, crashing context startup.

Make the initializer inert when the manifest entry is absent: it now returns
before super.onStartup instead of throwing, so a normal WAR is unaffected
while the application's own SpringBootServletInitializer bootstraps it. The
CLI-packaged WAR path is unchanged because that manifest entry is present.
This resolves the issue for both dbmigration plugins with no changes required
in consuming applications.

Fixes #15377

Assisted-by: claude-code:claude-4.8-opus
Copilot AI review requested due to automatic review settings July 3, 2026 01:20

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 WAR deployment crash where org.grails.cli.boot.SpringApplicationWebApplicationInitializer (pulled in transitively via CLI dependencies) is discovered in a standard bootWar and throws due to a missing Spring-Application-Source-Classes manifest entry. The initializer is now effectively a no-op unless that manifest entry is present, allowing the application’s own SpringBootServletInitializer to bootstrap normally in external servlet containers.

Changes:

  • Make SpringApplicationWebApplicationInitializer return early (skip super.onStartup) when source classes are missing/blank.
  • Add a regression Spock spec ensuring onStartup does not throw when the manifest is missing or lacks the entry.
  • Add missing test-scope Spring dependencies so the initializer class can be loaded during tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
grails-shell-cli/src/main/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializer.java Avoids NPE / unintended bootstrapping by making the initializer inert when the manifest source-classes entry is absent/blank.
grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy Adds regression coverage for missing manifest / missing entry scenarios (WAR deployment safety).
grails-shell-cli/build.gradle Adds test dependencies required to load the servlet initializer in the test JVM.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@bito-code-review

Copy link
Copy Markdown

To exercise the isBlank() path in SpringApplicationWebApplicationInitializer, you should update the where block in SpringApplicationWebApplicationInitializerSpec.groovy to include cases with an empty string and a whitespace-only string for the Spring-Application-Source-Classes manifest entry. You can add these cases to the manifestStream list as follows:

        manifestStream << [
                null,
                new ByteArrayInputStream('Manifest-Version: 1.0

'.getBytes('UTF-8')),
                new ByteArrayInputStream('Manifest-Version: 1.0
Spring-Application-Source-Classes: 

'.getBytes('UTF-8')),
                new ByteArrayInputStream('Manifest-Version: 1.0
Spring-Application-Source-Classes:   

'.getBytes('UTF-8'))
        ]

grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy

manifestStream << [
                null,
                new ByteArrayInputStream('Manifest-Version: 1.0

'.getBytes('UTF-8')),
                new ByteArrayInputStream('Manifest-Version: 1.0
Spring-Application-Source-Classes: 

'.getBytes('UTF-8')),
                new ByteArrayInputStream('Manifest-Version: 1.0
Spring-Application-Source-Classes:   

'.getBytes('UTF-8'))
        ]

@codecov

codecov Bot commented Jul 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.4864%. Comparing base (66cd24c) to head (7d6a85f).
⚠️ Report is 5848 commits behind head on 8.0.x.

Files with missing lines Patch % Lines
...ot/SpringApplicationWebApplicationInitializer.java 60.0000% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #15815        +/-   ##
==================================================
+ Coverage     49.4842%   49.4864%   +0.0022%     
- Complexity      16697      16701         +4     
==================================================
  Files            1947       1947                
  Lines           92474      92478         +4     
  Branches        16152      16154         +2     
==================================================
+ Hits            45760      45764         +4     
+ Misses          39606      39600         -6     
- Partials         7108       7114         +6     
Files with missing lines Coverage Δ
...ot/SpringApplicationWebApplicationInitializer.java 44.0000% <60.0000%> (+44.0000%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…5377)

Add two Spock where-cases exercising a present-but-empty and a
whitespace-only Spring-Application-Source-Classes manifest value, so the
initializer's isBlank() inert path is covered in addition to the
missing-manifest and missing-entry cases. Addresses PR review feedback.

Assisted-by: claude-code:claude-4.8-opus
@testlens-app

testlens-app Bot commented Jul 3, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 7d6a85f
▶️ Tests: 7465 executed
🟡 Checks: 45/96 completed


Learn more about TestLens at testlens.app.

@jamesfredley jamesfredley moved this to In Progress in Apache Grails Jul 3, 2026
@jamesfredley jamesfredley added this to the grails:8.0.0-M3 milestone Jul 3, 2026
@jamesfredley jamesfredley self-assigned this Jul 3, 2026
@jdaugherty

Copy link
Copy Markdown
Contributor

Why is this not developmentOnly? Is the CLI even needed on the production classpath? Is this just because we haven't split the db migration plugin commands from the update code ?

@jdaugherty

Copy link
Copy Markdown
Contributor

Closing this in favor of the CLI split

@jdaugherty jdaugherty closed this Jul 9, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Apache Grails Jul 9, 2026
@jdaugherty
jdaugherty deleted the feat/8.0.x-dbmigration-bootwar-npe-15377 branch July 9, 2026 15:25
@jamesfredley
jamesfredley restored the feat/8.0.x-dbmigration-bootwar-npe-15377 branch July 22, 2026 11:02
@jamesfredley jamesfredley reopened this Jul 22, 2026
@jamesfredley
jamesfredley changed the base branch from 8.0.x to 7.0.x July 22, 2026 11:03
@jamesfredley
jamesfredley changed the base branch from 7.0.x to 8.0.x July 22, 2026 11:03
@jamesfredley

Copy link
Copy Markdown
Contributor Author

Will recreate this on 7.0.x, since it fixes the issue on 7.x.x

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

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Split CLI Dependencies from runtime classpath

3 participants