Repository navigation
Fix bootWar NPE from stray CLI WebApplicationInitializer (#15377) - #16041
Conversation
Both Database Migration plugins depend on grails-shell-cli at
implementation scope, which places SpringApplicationWebApplicationInitializer
on the classpath of a normal application WAR. That class is a
SpringBootServletInitializer intended only for CLI-packaged WARs produced 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'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.
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.
Fixes #15377
Assisted-by: claude-code:claude-opus-4-8
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Backports a fix to prevent a Tomcat WAR deployment crash caused by a stray CLI WebApplicationInitializer being discovered on the classpath when the manifest source-classes entry is missing.
Changes:
- Make
SpringApplicationWebApplicationInitializerreturn early (inert) when noSpring-Application-Source-Classesmanifest entry is present. - Add a Spock regression spec to ensure
onStartupdoesn’t throw for missing/blank source-classes entries. - Add Spring dependencies to the test classpath to load/instantiate the initializer in tests.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| grails-shell-cli/src/main/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializer.java | Prevents NPE by treating missing/blank manifest entry as “inert” and returning before calling super.onStartup. |
| grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy | Adds regression coverage for absent/blank manifest scenarios to ensure no exception is thrown. |
| grails-shell-cli/build.gradle | Adds Spring Boot + Spring Web as test dependencies so the initializer class can be loaded in tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address review feedback on the SpringApplicationWebApplicationInitializer regression test: - Add a positive-path case proving onStartup skips the early return and proceeds to bootstrap when a Spring-Application-Source-Classes entry is present, via a RecordingInitializer that overrides the protected createRootApplicationContext hook (so no real application context starts). - Assert the CLI bootstrap path is not entered for the absent/blank cases. - Use StandardCharsets.UTF_8 and a manifest(String) helper to remove the repeated ByteArrayInputStream construction. Assisted-by: claude-code:claude-opus-4-8
✅ All tests passed ✅🏷️ Commit: 4a7af94 Learn more about TestLens at testlens.app. |
jdaugherty
left a comment
There was a problem hiding this comment.
I am ok merging this if there is agreement to remove in 8.x since 8.x addresses the root cause
Summary
Backport to
7.0.xof #15815. Fixes theCannot invoke "String.split(String)" because "sources" is nullNPE that crashes a classic servlet-container (Tomcat) WAR deployment of a Grails 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.Fixes #15377