@@ -2,11 +2,12 @@ package jres
22
33import (
44 "fmt"
5- "github.com/cloudfoundry/java-buildpack/src/java/common"
65 "os"
76 "path/filepath"
7+ "regexp"
88 "strings"
99
10+ "github.com/cloudfoundry/java-buildpack/src/java/common"
1011 "github.com/cloudfoundry/libbuildpack"
1112)
1213
@@ -214,10 +215,23 @@ func GetJREVersion(ctx *common.Context, jreName string) (libbuildpack.Dependency
214215 // Check for legacy JBP_CONFIG_<JRE_NAME> environment variable
215216 envKey := fmt .Sprintf ("JBP_CONFIG_%s" , strings .ToUpper (strings .ReplaceAll (jreName , "-" , "_" )))
216217 if envVal := os .Getenv (envKey ); envVal != "" {
217- // Parse version from env (e.g., '{jre: {version: 11.+}}')
218- // For now, simplified - just log it
219- ctx .Log .Debug ("JRE version override from %s: %s" , envKey , envVal )
220- // TODO: Parse YAML-like config from envVal
218+ versionPattern := parseJBPConfigVersion (envVal )
219+ if versionPattern == "" {
220+ return libbuildpack.Dependency {}, fmt .Errorf ("could not parse version from %s='%s'" , envKey , envVal )
221+ }
222+
223+ normalizedPattern := normalizeVersionPattern (versionPattern )
224+ availableVersions := ctx .Manifest .AllDependencyVersions (jreName )
225+ if len (availableVersions ) == 0 {
226+ return libbuildpack.Dependency {}, fmt .Errorf ("no versions of %s found in manifest" , jreName )
227+ }
228+
229+ matchedVersion , err := libbuildpack .FindMatchingVersion (normalizedPattern , availableVersions )
230+ if err != nil {
231+ return libbuildpack.Dependency {}, fmt .Errorf ("no version of %s matching '%s' found in manifest. Available versions: %v" , jreName , versionPattern , availableVersions )
232+ }
233+
234+ return libbuildpack.Dependency {Name : jreName , Version : matchedVersion }, nil
221235 }
222236
223237 // Get default version from manifest (no version constraint)
@@ -229,18 +243,25 @@ func GetJREVersion(ctx *common.Context, jreName string) (libbuildpack.Dependency
229243 return dep , nil
230244}
231245
232- // normalizeVersionPattern converts user-friendly version strings to manifest patterns
233- // Examples: "8" -> "8.*", "11" -> "11.*", "17.0" -> "17.0.*", "11.+" -> "11.+"
234246func normalizeVersionPattern (version string ) string {
235- // If already has wildcard, return as-is
236- if strings .Contains (version , "*" ) || strings .Contains (version , "+" ) {
247+ if strings .Contains (version , "+" ) {
248+ return strings .ReplaceAll (version , "+" , "*" )
249+ }
250+ if strings .Contains (version , "*" ) {
237251 return version
238252 }
239-
240- // Otherwise append ".*" to match any patch version
241253 return version + ".*"
242254}
243255
256+ func parseJBPConfigVersion (configValue string ) string {
257+ re := regexp .MustCompile (`version:\s*['"]?([0-9]+[0-9.*+]*)['"]?` )
258+ matches := re .FindStringSubmatch (configValue )
259+ if len (matches ) >= 2 {
260+ return strings .TrimSpace (matches [1 ])
261+ }
262+ return ""
263+ }
264+
244265// WriteJavaOpts writes JAVA_OPTS to a .opts file for centralized assembly
245266// JRE components use priority 05 to run early (before frameworks)
246267func WriteJavaOpts (ctx * common.Context , opts string ) error {
0 commit comments