Skip to content

Commit

Permalink
Fix #481: Rely on JRE_HOME for Java location
Browse files Browse the repository at this point in the history
* JRE_HOME is now set in libjvm, see paketo-buildpacks/libjvm#395
* thanks to that, we know for sure that the JRE_HOME/bin/java will be used for the training run
  • Loading branch information
anthonydahanne committed May 23, 2024
1 parent 017c4a4 commit 41466bb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
14 changes: 10 additions & 4 deletions boot/spring_performance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
38 changes: 36 additions & 2 deletions boot/spring_performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 41466bb

Please sign in to comment.