MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/buildpacks / ExecutableJar

Function ExecutableJar

pkg/java/java.go:70–97  ·  view source on GitHub ↗

ExecutableJar looks for the jar with a Main-Class manifest. If there is not exactly 1 of these jars, throw an error.

(ctx *gcp.Context)

Source from the content-addressed store, hash-verified

68
69// ExecutableJar looks for the jar with a Main-Class manifest. If there is not exactly 1 of these jars, throw an error.
70func ExecutableJar(ctx *gcp.Context) (string, error) {
71 var buildable = os.Getenv(env.Buildable)
72 currentJarPaths := jarPaths
73 if buildable != "" {
74 currentJarPaths = append([][]string{
75 []string{buildable, "target"},
76 []string{buildable}},
77 currentJarPaths...)
78 }
79 for i, path := range currentJarPaths {
80 path = append([]string{ctx.ApplicationRoot()}, path...)
81 path = append(path, "*.jar")
82 jars, err := ctx.Glob(filepath.Join(path...))
83 if err != nil {
84 return "", fmt.Errorf("finding jars: %w", err)
85 }
86 // There may be multiple jars due to some frameworks like Quarkus creating multiple jars,
87 // so we look for the jar that contains a Main-Class entry in its manifest.
88 executables := filterExecutables(ctx, jars)
89 // We've found a path with exactly 1 jar, so return that jar.
90 if len(executables) == 1 {
91 return executables[0], nil
92 } else if len(executables) > 1 {
93 return "", gcp.UserErrorf("found more than one jar with a Main-Class manifest entry in %s: %v, please specify an entrypoint", currentJarPaths[i], executables)
94 }
95 }
96 return "", gcp.UserErrorf("did not find any jar files with a Main-Class manifest entry")
97}
98
99func filterExecutables(ctx *gcp.Context, jars []string) []string {
100 var executables []string

Callers 6

buildDefaultFunction · 0.92
customEntrypointFunction · 0.92
BuildFnFunction · 0.92
TestExecutableJarFunction · 0.85

Calls 3

filterExecutablesFunction · 0.85
ApplicationRootMethod · 0.80
GlobMethod · 0.80

Tested by 1

TestExecutableJarFunction · 0.68