Skip to content

Commit

Permalink
Allow making an executable-jar image by only providing an executable JAR
Browse files Browse the repository at this point in the history
  • Loading branch information
sap-ali committed Jun 19, 2024
1 parent 8c65240 commit 2e6a7a3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The Paketo Buildpack for Executable JAR is a Cloud Native Buildpack that contrib

## Behavior

This buildpack will participate if all the following conditions are met:
This buildpack will participate if any the following conditions are met:

* `<APPLICATION_ROOT>/META-INF/MANIFEST.MF` contains a `Main-Class` entry
* `<APPLICATION_ROOT>/**/*.jar` exists and that JAR has a `/META-INF/MANIFEST.MF` file which contains a `Main-Class` entry
Expand Down
8 changes: 7 additions & 1 deletion executable/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
PlanEntrySyft = "syft"
)

type Detect struct{
type Detect struct {
Logger bard.Logger
}

Expand Down Expand Up @@ -65,9 +65,15 @@ func (d Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error
return libcnb.DetectResult{}, fmt.Errorf("unable to read manifest in %s\n%w", context.Application.Path, err)
}

jarGlob, _ := cr.Resolve("BP_EXECUTABLE_JAR_LOCATION")
_, props, _ := findExecutableJAR(context.Application.Path, jarGlob)

if _, ok := m.Get("Main-Class"); ok {
d.Logger.Info("PASSED: 'Main-Class' manifest attribute found")
result.Plans[0].Provides = append(result.Plans[0].Provides, libcnb.BuildPlanProvide{Name: PlanEntryJVMApplicationPackage})
} else if _, ok := props.Get("Main-Class"); ok {
d.Logger.Info("PASSED: Jar file with 'Main-Class' manifest attribute found")
result.Plans[0].Provides = append(result.Plans[0].Provides, libcnb.BuildPlanProvide{Name: PlanEntryJVMApplicationPackage})
}

if cr.ResolveBool("BP_LIVE_RELOAD_ENABLED") {
Expand Down
26 changes: 26 additions & 0 deletions executable/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,32 @@ func testDetect(t *testing.T, context spec.G, it spec.S) {
})
})

context("META-INF/MANIFEST.MF not found but there is JAR with Main-Class", func() {
it.Before(func() {
Expect(CreateJAR(filepath.Join(ctx.Application.Path, "a.jar"), map[string]string{"Main-Class": "test.Main"})).To(Succeed())
})

it("requires and provides jvm-application-package", func() {
Expect(detect.Detect(ctx)).To(Equal(libcnb.DetectResult{
Pass: true,
Plans: []libcnb.BuildPlan{
{
Provides: []libcnb.BuildPlanProvide{
{Name: "jvm-application"},
{Name: "jvm-application-package"},
},
Requires: []libcnb.BuildPlanRequire{
{Name: "syft"},
{Name: "jre", Metadata: map[string]interface{}{"launch": true}},
{Name: "jvm-application-package"},
{Name: "jvm-application"},
},
},
},
}))
})
})

context("$BP_LIVE_RELOAD_ENABLED is set", func() {
it.Before(func() {
Expect(os.Setenv("BP_LIVE_RELOAD_ENABLED", "true")).To(Succeed())
Expand Down

0 comments on commit 2e6a7a3

Please sign in to comment.