GetJREVersion gets the desired JRE version from environment or uses default Supports BP_JAVA_VERSION (simple version) and JBP_CONFIG_ (complex config)
(ctx *common.Context, jreName string)
| 229 | // GetJREVersion gets the desired JRE version from environment or uses default |
| 230 | // Supports BP_JAVA_VERSION (simple version) and JBP_CONFIG_<JRE_NAME> (complex config) |
| 231 | func GetJREVersion(ctx *common.Context, jreName string) (libbuildpack.Dependency, error) { |
| 232 | // Check for simple BP_JAVA_VERSION environment variable first |
| 233 | // Format: "8", "11", "17", "21", etc. or version patterns like "11.+", "17.*" |
| 234 | if bpVersion := os.Getenv("BP_JAVA_VERSION"); bpVersion != "" { |
| 235 | ctx.Log.Debug("Using Java version from BP_JAVA_VERSION: %s", bpVersion) |
| 236 | |
| 237 | // Normalize version to a pattern that FindMatchingVersion understands |
| 238 | versionPattern := normalizeVersionPattern(bpVersion) |
| 239 | |
| 240 | // Get all available versions for this JRE |
| 241 | availableVersions := ctx.Manifest.AllDependencyVersions(jreName) |
| 242 | if len(availableVersions) == 0 { |
| 243 | return libbuildpack.Dependency{}, fmt.Errorf("no versions found for %s", jreName) |
| 244 | } |
| 245 | |
| 246 | // Find the highest matching version |
| 247 | matchedVersion, err := libbuildpack.FindMatchingVersion(versionPattern, availableVersions) |
| 248 | if err != nil { |
| 249 | ctx.Log.Warning("Could not find %s matching version %s: %s", jreName, versionPattern, err.Error()) |
| 250 | return libbuildpack.Dependency{}, fmt.Errorf("no version of %s matching %s found", jreName, versionPattern) |
| 251 | } |
| 252 | |
| 253 | ctx.Log.Debug("Resolved %s version %s from pattern %s", jreName, matchedVersion, versionPattern) |
| 254 | return libbuildpack.Dependency{Name: jreName, Version: matchedVersion}, nil |
| 255 | } |
| 256 | |
| 257 | // Check for JBP_CONFIG_<JRE_NAME> environment variable |
| 258 | // Try both the auto-generated name and the documented name for backward compatibility |
| 259 | envKey := fmt.Sprintf("JBP_CONFIG_%s", strings.ToUpper(strings.ReplaceAll(jreName, "-", "_"))) |
| 260 | envVal := os.Getenv(envKey) |
| 261 | |
| 262 | // If not found, check for documented environment variable name (e.g., JBP_CONFIG_OPEN_JDK_JRE) |
| 263 | // This ensures backward compatibility with documented naming conventions |
| 264 | if envVal == "" { |
| 265 | if documentedEnvKey, exists := jreNameToDocumentedEnvVar[jreName]; exists { |
| 266 | envVal = os.Getenv(documentedEnvKey) |
| 267 | if envVal != "" { |
| 268 | envKey = documentedEnvKey |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if envVal != "" { |
| 274 | ctx.Log.Debug("Found %s='%s'", envKey, envVal) |
| 275 | |
| 276 | versionPattern := parseJBPConfigVersion(envVal) |
| 277 | if versionPattern == "" { |
| 278 | // No version specified — env var is used for other settings (e.g. memory_calculator). |
| 279 | // Fall back to manifest default. |
| 280 | ctx.Log.Debug("%s set but contains no version field, using manifest default", envKey) |
| 281 | } else { |
| 282 | ctx.Log.Debug("Parsed version pattern from %s: '%s'", envKey, versionPattern) |
| 283 | |
| 284 | normalizedPattern := normalizeVersionPattern(versionPattern) |
| 285 | ctx.Log.Debug("Normalized pattern: '%s' -> '%s'", versionPattern, normalizedPattern) |
| 286 | |
| 287 | availableVersions := ctx.Manifest.AllDependencyVersions(jreName) |
| 288 | if len(availableVersions) == 0 { |
no test coverage detected