Skip to content

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

Merged
jamesfredley merged 2 commits into
7.0.xfrom
fix/7.0.x-dbmigration-bootwar-npe-15377
Jul 29, 2026
Merged

jamesfredley merged 2 commits into
7.0.xfrom
fix/7.0.x-dbmigration-bootwar-npe-15377

Conversation

@jamesfredley

Copy link
Copy Markdown
Contributor

Summary

Backport to 7.0.x of #15815. Fixes the Cannot invoke "String.split(String)" because "sources" is null NPE 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-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.

Fixes #15377

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
Copilot AI review requested due to automatic review settings July 22, 2026 11:40

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

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 SpringApplicationWebApplicationInitializer return early (inert) when no Spring-Application-Source-Classes manifest entry is present.
  • Add a Spock regression spec to ensure onStartup doesn’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.

@jamesfredley jamesfredley linked an issue Jul 22, 2026 that may be closed by this pull request
@jamesfredley jamesfredley moved this to In Progress in Apache Grails Jul 22, 2026
@jamesfredley jamesfredley added this to the grails:7.0.15 milestone Jul 22, 2026
@jamesfredley jamesfredley self-assigned this Jul 22, 2026
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
@testlens-app

testlens-app Bot commented Jul 22, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 4a7af94
▶️ Tests: 9240 executed
⚪️ Checks: 51/51 completed


Learn more about TestLens at testlens.app.

@jdaugherty jdaugherty 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.

I am ok merging this if there is agreement to remove in 8.x since 8.x addresses the root cause

@jamesfredley jamesfredley moved this from In Progress to Todo in Apache Grails Jul 24, 2026
@jamesfredley
jamesfredley merged commit 22866b7 into 7.0.x Jul 29, 2026
94 of 95 checks passed
@jamesfredley
jamesfredley deleted the fix/7.0.x-dbmigration-bootwar-npe-15377 branch July 29, 2026 13:25
@github-project-automation github-project-automation Bot moved this from Todo to Done in Apache Grails Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Split CLI Dependencies from runtime classpath

3 participants