From 91ef59899f6b135b474221be76bf569ad52709bf Mon Sep 17 00:00:00 2001 From: James Fredley Date: Thu, 2 Jul 2026 21:19:37 -0400 Subject: [PATCH 1/2] Fix bootWar NPE from stray CLI WebApplicationInitializer (#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 --- grails-shell-cli/build.gradle | 5 ++ ...gApplicationWebApplicationInitializer.java | 15 +++++- ...cationWebApplicationInitializerSpec.groovy | 54 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy diff --git a/grails-shell-cli/build.gradle b/grails-shell-cli/build.gradle index f3d4d7d0680..1498cebab6d 100644 --- a/grails-shell-cli/build.gradle +++ b/grails-shell-cli/build.gradle @@ -110,6 +110,11 @@ dependencies { testImplementation 'org.slf4j:slf4j-simple' testImplementation 'org.spockframework:spock-core' + // Required to load SpringApplicationWebApplicationInitializer (extends SpringBootServletInitializer) + // in tests; these are compileOnly for the main source set. + testImplementation 'org.springframework.boot:spring-boot' + testImplementation 'org.springframework:spring-web' + testRuntimeOnly 'net.bytebuddy:byte-buddy' // Required by Spock's mocking support // any project that should be included in the end distribution should be included here diff --git a/grails-shell-cli/src/main/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializer.java b/grails-shell-cli/src/main/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializer.java index c993cdd6954..f73a9216d22 100644 --- a/grails-shell-cli/src/main/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializer.java +++ b/grails-shell-cli/src/main/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializer.java @@ -51,15 +51,28 @@ public void onStartup(ServletContext servletContext) throws ServletException { catch (IOException ex) { throw new IllegalStateException(ex); } + // This initializer only applies to CLI-packaged WARs produced by the Grails shell 'war' + // command, which records the application source classes in the WAR manifest via the + // 'Spring-Application-Source-Classes' entry. When that entry is absent (for example a + // standard 'bootWar' archive deployed to an external servlet container such as Tomcat), + // this initializer is not applicable and must stay inert so it does not interfere with + // the application's own SpringBootServletInitializer. + // See https://github.com/apache/grails-core/issues/15377 + if (this.sources == null || this.sources.length == 0) { + return; + } super.onStartup(servletContext); } private String[] getSources(ServletContext servletContext) throws IOException { Manifest manifest = getManifest(servletContext); if (manifest == null) { - throw new IllegalStateException("Unable to read manifest"); + return null; } String sources = manifest.getMainAttributes().getValue(SOURCE_ENTRY); + if (sources == null || sources.isBlank()) { + return null; + } return sources.split(","); } diff --git a/grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy b/grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy new file mode 100644 index 00000000000..8e43bb0bc00 --- /dev/null +++ b/grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.cli.boot + +import jakarta.servlet.ServletContext +import spock.lang.Specification + +/** + * Verifies that {@link SpringApplicationWebApplicationInitializer} stays inert when it is + * discovered on the classpath of a standard {@code bootWar} deployed to an external servlet + * container, i.e. when the {@code Spring-Application-Source-Classes} manifest entry is absent. + * + * Regression test for #15377. + */ +class SpringApplicationWebApplicationInitializerSpec extends Specification { + + void "onStartup is inert when the source-classes manifest entry is absent"() { + given: 'an initializer discovered in a non CLI-packaged WAR' + def initializer = new SpringApplicationWebApplicationInitializer() + ServletContext servletContext = Mock(ServletContext) { + getResourceAsStream('/META-INF/MANIFEST.MF') >> manifestStream + } + + when: 'the servlet container invokes onStartup' + initializer.onStartup(servletContext) + + then: 'the application boot is not disturbed and no NPE is raised' + noExceptionThrown() + + where: 'the manifest is missing or lacks the source-classes entry' + manifestStream << [ + null, + new ByteArrayInputStream('Manifest-Version: 1.0\r\n\r\n'.getBytes('UTF-8')), + new ByteArrayInputStream( + 'Manifest-Version: 1.0\r\nImplementation-Title: my-app\r\n\r\n'.getBytes('UTF-8')) + ] + } +} From 7d6a85ff0133af3d715a5f88c4d9e1aafd1dff02 Mon Sep 17 00:00:00 2001 From: James Fredley Date: Fri, 3 Jul 2026 08:42:03 -0400 Subject: [PATCH 2/2] Cover blank/whitespace manifest entry in dbmigration bootWar test (#15377) 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 --- .../SpringApplicationWebApplicationInitializerSpec.groovy | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy b/grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy index 8e43bb0bc00..7eee547e061 100644 --- a/grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy +++ b/grails-shell-cli/src/test/groovy/org/grails/cli/boot/SpringApplicationWebApplicationInitializerSpec.groovy @@ -43,12 +43,16 @@ class SpringApplicationWebApplicationInitializerSpec extends Specification { then: 'the application boot is not disturbed and no NPE is raised' noExceptionThrown() - where: 'the manifest is missing or lacks the source-classes entry' + where: 'the manifest is missing, lacks the entry, or has a blank/whitespace-only entry' manifestStream << [ null, new ByteArrayInputStream('Manifest-Version: 1.0\r\n\r\n'.getBytes('UTF-8')), new ByteArrayInputStream( - 'Manifest-Version: 1.0\r\nImplementation-Title: my-app\r\n\r\n'.getBytes('UTF-8')) + 'Manifest-Version: 1.0\r\nImplementation-Title: my-app\r\n\r\n'.getBytes('UTF-8')), + new ByteArrayInputStream( + 'Manifest-Version: 1.0\r\nSpring-Application-Source-Classes: \r\n\r\n'.getBytes('UTF-8')), + new ByteArrayInputStream( + 'Manifest-Version: 1.0\r\nSpring-Application-Source-Classes: \r\n\r\n'.getBytes('UTF-8')) ] } }