From 5dd42641e5b8a80bed05b1379aa83186cc380b73 Mon Sep 17 00:00:00 2001 From: Anthony Dahanne Date: Thu, 23 May 2024 11:26:08 -0400 Subject: [PATCH] Fix #481: Rely on JRE_HOME for Java location * JRE_HOME is now set in libjvm, see https://github.com/paketo-buildpacks/libjvm/pull/395 * thanks to that, we know for sure that the JRE_HOME/bin/java will be used for the training run --- boot/spring_performance.go | 14 ++++++++---- boot/spring_performance_test.go | 38 +++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/boot/spring_performance.go b/boot/spring_performance.go index 0e1780e..5cee61b 100644 --- a/boot/spring_performance.go +++ b/boot/spring_performance.go @@ -106,7 +106,13 @@ func (s SpringPerformance) Contribute(layer libcnb.Layer) (libcnb.Layer, error) os.RemoveAll(s.AppPath) } - if err := s.springBootJarCDSLayoutExtract(jarPath); err != nil { + javaCommand := "java" + jreHome := sherpa.GetEnvWithDefault("JRE_HOME", sherpa.GetEnvWithDefault("JAVA_HOME", "")) + if jreHome != "" { + javaCommand = jreHome + "/bin/java" + } + + if err := s.springBootJarCDSLayoutExtract(javaCommand, jarPath); err != nil { return layer, fmt.Errorf("error extracting Boot jar at %s\n%w", jarPath, err) } startClassValue, _ := s.Manifest.Get("Start-Class") @@ -140,7 +146,7 @@ func (s SpringPerformance) Contribute(layer libcnb.Layer) (libcnb.Layer, error) // perform the training run, application.dsa, the cache file, will be created if err := s.Executor.Execute(effect.Execution{ - Command: "java", + Command: javaCommand, Env: trainingRunEnvVariables, Args: trainingRunArgs, Dir: s.AppPath, @@ -163,10 +169,10 @@ func (s SpringPerformance) Name() string { return s.LayerContributor.Name } -func (s SpringPerformance) springBootJarCDSLayoutExtract(jarPath string) error { +func (s SpringPerformance) springBootJarCDSLayoutExtract(javaCommand string, jarPath string) error { s.Logger.Bodyf("Extracting Jar") if err := s.Executor.Execute(effect.Execution{ - Command: "java", + Command: javaCommand, Args: []string{"-Djarmode=tools", "-jar", jarPath, "extract", "--destination", s.AppPath}, Dir: filepath.Dir(jarPath), Stdout: s.Logger.InfoWriter(), diff --git a/boot/spring_performance_test.go b/boot/spring_performance_test.go index 900585a..d8a3547 100644 --- a/boot/spring_performance_test.go +++ b/boot/spring_performance_test.go @@ -250,8 +250,8 @@ Spring-Boot-Lib: BOOT-INF/lib cwd, _ := os.Getwd() old := filepath.Join(cwd, "testdata", "spring-cloud-bindings", "spring-cloud-bindings-1.2.3.jar") - new := filepath.Join(ctx.Application.Path, "BOOT-INF", "lib", "spring-cloud-bindings-1.2.3.jar") - os.Symlink(old, new) + now := filepath.Join(ctx.Application.Path, "BOOT-INF", "lib", "spring-cloud-bindings-1.2.3.jar") + os.Symlink(old, now) props, err := libjvm.NewManifest(ctx.Application.Path) Expect(err).NotTo(HaveOccurred()) @@ -284,6 +284,40 @@ Spring-Boot-Lib: BOOT-INF/lib }) + it("fails with a non existing JRE_HOME path", func() { + Expect(os.Setenv("JRE_HOME", "/that/does/not/exist")).To(Succeed()) + + aotEnabled, cdsEnabled = true, true + dc := libpak.DependencyCache{CachePath: "testdata"} + executor.On("Execute", mock.Anything).Return(nil) + + Expect(os.WriteFile(filepath.Join(ctx.Application.Path, "META-INF", "MANIFEST.MF"), []byte(` +Spring-Boot-Version: 3.3.1 +Spring-Boot-Classes: BOOT-INF/classes +Spring-Boot-Lib: BOOT-INF/lib +`), 0644)).To(Succeed()) + props, err := libjvm.NewManifest(ctx.Application.Path) + Expect(err).NotTo(HaveOccurred()) + + s := boot.NewSpringPerformance(dc, ctx.Application.Path, props, aotEnabled, cdsEnabled, "", true) + s.Executor = executor + + layer, err := ctx.Layers.Layer("test-layer") + Expect(err).NotTo(HaveOccurred()) + + layer, err = s.Contribute(layer) + Expect(err).NotTo(HaveOccurred()) + + Expect(executor.Calls).To(HaveLen(2)) + e, ok := executor.Calls[1].Arguments[0].(effect.Execution) + Expect(ok).To(BeTrue()) + + Expect(e.Command).To(Equal("/that/does/not/exist/bin/java")) + Expect(layer.Build).To(BeTrue()) + + Expect(os.Unsetenv("JRE_HOME")).To(Succeed()) + }) + } func unzip(src, dest string) error {