Repository navigation
Fix bootWar NPE from stray CLI WebApplicationInitializer (#15377) - #15815
jamesfredley wants to merge 2 commits into
Conversation
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
There was a problem hiding this comment.
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
SpringApplicationWebApplicationInitializerreturn early (skipsuper.onStartup) when source classes are missing/blank. - Add a regression Spock spec ensuring
onStartupdoes 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.
|
To exercise the 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 |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ 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
🚀 New features to boost your workflow:
|
…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
✅ All tests passed ✅🏷️ Commit: 7d6a85f Learn more about TestLens at testlens.app. |
|
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 ? |
|
Closing this in favor of the CLI split |
|
Will recreate this on 7.0.x, since it fixes the issue on 7.x.x |
Summary
Fixes the
Cannot invoke "String.split(String)" because "sources" is nullNPE 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-dbmigrationandgrails-data-hibernate7-dbmigration- depend ongrails-shell-cliatimplementationscope. That placesorg.grails.cli.boot.SpringApplicationWebApplicationInitializeron the classpath of a normal application WAR.That class is a
SpringBootServletInitializer(aWebApplicationInitializer) intended only for CLI-packaged WARs produced by the shellwarcommand (WarCommand), which records the application source classes in the WAR manifest via theSpring-Application-Source-Classesentry.When a standard
bootWaris deployed to an external servlet container such as Tomcat, Spring'sSpringServletContainerInitializerdiscovers the stray initializer and invokesonStartup. A normal WAR manifest has noSpring-Application-Source-Classesentry, sosourceswasnullandsources.split(",")threw an NPE, crashing context startup. The application's ownSpringBootServletInitializernever got a chance to bootstrap.Fix
Make the initializer inert when the manifest source-classes entry is absent:
onStartupnow returns beforesuper.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-climodule resolves the issue for both dbmigration plugins, and - importantly - requires no changes in consuming applications (avoiding thedevelopmentOnly/ feature-variant / separate-artifact options that would each require app-side build changes).Changes
SpringApplicationWebApplicationInitializer.java: early-return when there are no source classes;getManifestreturningnulland a blank/absent entry now yieldnull(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:testImplementationforspring-boot+spring-web(needed to load the class under test; they arecompileOnlyin 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