Skip to content

Commit 32072db

Browse files
author
Zaker Farahi
committed
add support for spring boot 4, with a fallback to 3
1 parent d051cb9 commit 32072db

File tree

3 files changed

+62
-19
lines changed

3 files changed

+62
-19
lines changed

manifest.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ default_versions:
3939
- name: auto-reconfiguration
4040
version: 2.x
4141
- name: java-cfenv
42+
version: 4.x
43+
- name: java-cfenv-sb3
4244
version: 3.x
4345
- name: client-certificate-mapper
4446
version: 2.x
@@ -283,14 +285,22 @@ dependencies:
283285
- cflinuxfs4
284286
source: https://repo1.maven.org/maven2/org/jacoco/jacoco/0.8.14/jacoco-0.8.14.zip
285287
source_sha256: 0372447f54900b4e77bcb216985b7d31cf5318fc599f8f9346ee35830448c125
286-
- name: java-cfenv
288+
- name: java-cfenv-sb3
287289
version: 3.5.0
288290
uri: https://java-buildpack.cloudfoundry.org/java-cfenv/java-cfenv-3.5.0.jar
289291
sha256: 6a761fe530783c0ec9e6d1713ef54f6504803bf1ad02856d3ee7b46211f905c5
290292
cf_stacks:
291293
- cflinuxfs4
292294
source: https://repo1.maven.org/maven2/io/pivotal/cfenv/java-cfenv/3.5.0/java-cfenv-3.5.0.jar
293295
source_sha256: 9cc8d4c368bc90eafb7b6b14bc34c57ef5523f3ec8546e3fbd91326cdfc13500
296+
- name: java-cfenv
297+
version: 4.0.0
298+
uri: https://java-buildpack.cloudfoundry.org/java-cfenv/java-cfenv-4.0.0.jar
299+
sha256: caa4a78642a39df4fe01ed2013fef1f58ac7ce5376d7791f9b4c8f6eba9da94e
300+
cf_stacks:
301+
- cflinuxfs4
302+
source: https://repo1.maven.org/maven2/io/pivotal/cfenv/java-cfenv/4.0.0/java-cfenv-4.0.0.jar
303+
source_sha256: caa4a78642a39df4fe01ed2013fef1f58ac7ce5376d7791f9b4c8f6eba9da94e
294304
- name: java-memory-assistant
295305
version: 0.5.0
296306
uri: https://github.com/SAP/java-memory-assistant/releases/download/0.5.0/java-memory-assistant-0.5.0.jar

src/java/frameworks/java_cf_env.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package frameworks
22

33
import (
44
"fmt"
5-
"github.com/cloudfoundry/java-buildpack/src/java/common"
65
"os"
76
"path/filepath"
87
"strings"
98

9+
"github.com/cloudfoundry/java-buildpack/src/java/common"
10+
1011
"github.com/cloudfoundry/libbuildpack"
1112
"gopkg.in/yaml.v2"
1213
)
@@ -31,8 +32,8 @@ func (j *JavaCfEnvFramework) Detect() (string, error) {
3132
return "", nil
3233
}
3334

34-
// Check if Spring Boot 3.x is present
35-
if !j.isSpringBoot3() {
35+
// Check if Spring Boot 3.x/4.x is present
36+
if !j.isSpringBootMajor(4) && !j.isSpringBootMajor(3) {
3637
return "", nil
3738
}
3839

@@ -49,13 +50,21 @@ func (j *JavaCfEnvFramework) Detect() (string, error) {
4950
func (j *JavaCfEnvFramework) Supply() error {
5051
j.context.Log.BeginStep("Installing Java CF Env")
5152

53+
dependency := "java-cfenv"
54+
defaultVersion := "4.0.0"
55+
56+
if j.isSpringBootMajor(3) {
57+
dependency = "java-cfenv-sb3"
58+
defaultVersion = "3.1.0"
59+
}
60+
5261
// Get java-cfenv dependency from manifest
53-
dep, err := j.context.Manifest.DefaultVersion("java-cfenv")
62+
dep, err := j.context.Manifest.DefaultVersion(dependency)
5463
if err != nil {
5564
j.context.Log.Warning("Unable to determine Java CF Env version, using default")
5665
dep = libbuildpack.Dependency{
57-
Name: "java-cfenv",
58-
Version: "3.1.0", // Fallback version
66+
Name: dependency,
67+
Version: defaultVersion,
5968
}
6069
}
6170

@@ -139,15 +148,14 @@ func (j *JavaCfEnvFramework) isEnabled() bool {
139148
return true
140149
}
141150

142-
// isSpringBoot3 checks if the application is Spring Boot 3.x
143-
func (j *JavaCfEnvFramework) isSpringBoot3() bool {
144-
// Look for Spring Boot 3.x JARs
145-
// Spring Boot 3.x uses spring-boot-3.*.jar
151+
// isSpringBootMajor checks if the application is Spring Boot <major>.x
152+
func (j *JavaCfEnvFramework) isSpringBootMajor(major int) bool {
153+
jarGlob := fmt.Sprintf("spring-boot-%d.*.jar", major)
146154
patterns := []string{
147-
filepath.Join(j.context.Stager.BuildDir(), "**", "spring-boot-3.*.jar"),
148-
filepath.Join(j.context.Stager.BuildDir(), "WEB-INF", "lib", "spring-boot-3.*.jar"),
149-
filepath.Join(j.context.Stager.BuildDir(), "BOOT-INF", "lib", "spring-boot-3.*.jar"),
150-
filepath.Join(j.context.Stager.BuildDir(), "lib", "spring-boot-3.*.jar"),
155+
filepath.Join(j.context.Stager.BuildDir(), "**", jarGlob),
156+
filepath.Join(j.context.Stager.BuildDir(), "WEB-INF", "lib", jarGlob),
157+
filepath.Join(j.context.Stager.BuildDir(), "BOOT-INF", "lib", jarGlob),
158+
filepath.Join(j.context.Stager.BuildDir(), "lib", jarGlob),
151159
}
152160

153161
for _, pattern := range patterns {
@@ -161,7 +169,7 @@ func (j *JavaCfEnvFramework) isSpringBoot3() bool {
161169
manifestPath := filepath.Join(j.context.Stager.BuildDir(), "META-INF", "MANIFEST.MF")
162170
if content, err := os.ReadFile(manifestPath); err == nil {
163171
manifest := string(content)
164-
if strings.Contains(manifest, "Spring-Boot-Version: 3.") {
172+
if strings.Contains(manifest, fmt.Sprintf("Spring-Boot-Version: %d.", major)) {
165173
return true
166174
}
167175
}

src/java/supply/supply_test.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ dependencies: []
177177
})
178178
})
179179

180-
Context("When a Spring-boot application is present", func() {
180+
Context("When a Spring-boot 4 application is present", func() {
181181
BeforeEach(func() {
182182
// Create a Spring Boot JAR with BOOT-INF
183183
bootInfDir := filepath.Join(buildDir, "BOOT-INF")
@@ -186,13 +186,13 @@ dependencies: []
186186

187187
// Create META-INF/MANIFEST.MF with corresponding content of a Spring Boot app
188188
manifestFile := filepath.Join(buildDir, "META-INF", "MANIFEST.MF")
189-
Expect(os.WriteFile(manifestFile, []byte("Spring-Boot-Version: 3.x.x"), 0644)).To(Succeed())
189+
Expect(os.WriteFile(manifestFile, []byte("Spring-Boot-Version: 4.x.x"), 0644)).To(Succeed())
190190

191191
//Create install dir and mock for the java cf env spring boot related dependency
192192
javaCfEnvInstallDir := filepath.Join(depsDir, depsIdx, "java_cf_env")
193193
Expect(os.MkdirAll(filepath.Join(javaCfEnvInstallDir), 0755)).To(Succeed())
194194

195-
depJavaCfEnv := libbuildpack.Dependency{Name: "java-cfenv", Version: "3.5.0"}
195+
depJavaCfEnv := libbuildpack.Dependency{Name: "java-cfenv", Version: "4.0.0"}
196196
mockManifest.EXPECT().DefaultVersion("java-cfenv").Return(depJavaCfEnv, nil)
197197
mockInstaller.EXPECT().InstallDependency(depJavaCfEnv, javaCfEnvInstallDir).Return(nil)
198198
})
@@ -202,6 +202,31 @@ dependencies: []
202202
})
203203
})
204204

205+
Context("When a Spring-boot 3 application is present", func() {
206+
BeforeEach(func() {
207+
// Create a Spring Boot JAR with BOOT-INF
208+
bootInfDir := filepath.Join(buildDir, "BOOT-INF")
209+
Expect(os.MkdirAll(bootInfDir, 0755)).To(Succeed())
210+
Expect(os.MkdirAll(filepath.Join(buildDir, "META-INF"), 0755)).To(Succeed())
211+
212+
// Create META-INF/MANIFEST.MF with corresponding content of a Spring Boot app
213+
manifestFile := filepath.Join(buildDir, "META-INF", "MANIFEST.MF")
214+
Expect(os.WriteFile(manifestFile, []byte("Spring-Boot-Version: 3.x.x"), 0644)).To(Succeed())
215+
216+
//Create install dir and mock for the java cf env spring boot related dependency
217+
javaCfEnvInstallDir := filepath.Join(depsDir, depsIdx, "java_cf_env")
218+
Expect(os.MkdirAll(filepath.Join(javaCfEnvInstallDir), 0755)).To(Succeed())
219+
220+
depJavaCfEnv := libbuildpack.Dependency{Name: "java-cfenv-sb3", Version: "3.5.0"}
221+
mockManifest.EXPECT().DefaultVersion("java-cfenv-sb3").Return(depJavaCfEnv, nil)
222+
mockInstaller.EXPECT().InstallDependency(depJavaCfEnv, javaCfEnvInstallDir).Return(nil)
223+
})
224+
225+
It("Supply passes successfully", func() {
226+
Expect(supply.Run(supplier)).To(Succeed())
227+
})
228+
})
229+
205230
Context("When a Groovy application is present", func() {
206231
BeforeEach(func() {
207232
// Create a .groovy file

0 commit comments

Comments
 (0)