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)
| 68 | |
| 69 | // ExecutableJar looks for the jar with a Main-Class manifest. If there is not exactly 1 of these jars, throw an error. |
| 70 | func 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 | |
| 99 | func filterExecutables(ctx *gcp.Context, jars []string) []string { |
| 100 | var executables []string |