@@ -63,47 +63,23 @@ func (t *TomcatContainer) Supply() error {
6363 }
6464
6565 if javaHome != "" {
66- javaMajorVersion , versionErr := common . DetermineJavaVersion (javaHome )
66+ versionPattern , versionErr := SelectTomcatVersionPattern (javaHome , DetermineTomcatVersion ( t . config . Tomcat . Version ) )
6767 if versionErr != nil {
68- t .context .Log .Warning ("Unable to determine Java version: %s (defaulting to 17)" , versionErr .Error ())
69- javaMajorVersion = 17
68+ return versionErr
7069 }
7170
72- tomcatVersion := DetermineTomcatVersion (t .config .Tomcat .Version )
73- t .context .Log .Debug ("Detected Java major version: %d" , javaMajorVersion )
74-
75- // Select Tomcat version pattern based on Java version
76- var versionPattern string
77- if tomcatVersion == "" {
78- t .context .Log .Info ("Tomcat version not specified" )
79- if javaMajorVersion >= 11 {
80- // Java 11+: Use Tomcat 10.x (Jakarta EE 9+)
81- versionPattern = "10.x"
82- t .context .Log .Info ("Using Tomcat 10.x for Java %d" , javaMajorVersion )
83- } else {
84- // Java 8-10: Use Tomcat 9.x (Java EE 8)
85- versionPattern = "9.x"
86- t .context .Log .Info ("Using Tomcat 9.x for Java %d" , javaMajorVersion )
71+ if versionPattern != "" {
72+ allVersions := t .context .Manifest .AllDependencyVersions ("tomcat" )
73+ resolvedVersion , err := libbuildpack .FindMatchingVersion (versionPattern , allVersions )
74+ if err != nil {
75+ return fmt .Errorf ("tomcat version resolution error for pattern %q: %w" , versionPattern , err )
8776 }
77+ dep .Name = "tomcat"
78+ dep .Version = resolvedVersion
79+ t .context .Log .Debug ("Resolved Tomcat version pattern '%s' to %s" , versionPattern , resolvedVersion )
8880 } else {
89- versionPattern = tomcatVersion
90- t .context .Log .Info ("Using Tomcat %s for Java %d" , versionPattern , javaMajorVersion )
91- }
92-
93- if strings .HasPrefix (versionPattern , "10." ) && javaMajorVersion < 11 {
94- return fmt .Errorf ("Tomcat 10.x requires Java 11+, but Java %d detected" , javaMajorVersion )
95- }
96-
97- // Resolve the version pattern to actual version using libbuildpack
98- allVersions := t .context .Manifest .AllDependencyVersions ("tomcat" )
99- resolvedVersion , err := libbuildpack .FindMatchingVersion (versionPattern , allVersions )
100- if err != nil {
101- return fmt .Errorf ("tomcat version resolution error for pattern %q: %w" , versionPattern , err )
81+ t .context .Log .Warning ("Unable to determine Java version from JAVA_HOME, falling back to manifest default Tomcat version" )
10282 }
103-
104- dep .Name = "tomcat"
105- dep .Version = resolvedVersion
106- t .context .Log .Debug ("Resolved Tomcat version pattern '%s' to %s" , versionPattern , resolvedVersion )
10783 }
10884
10985 // Fallback to default version if we couldn't determine Java version
@@ -463,7 +439,37 @@ func getKeys(m map[string]string) []string {
463439 return keys
464440}
465441
466- // DetermineTomcatVersion determines the version of the tomcat
442+ // SelectTomcatVersionPattern determines the Tomcat version pattern to use based on the
443+ // detected Java version and any user-configured Tomcat version.
444+ // Returns ("", nil) when Java version cannot be determined — the caller should fall back
445+ // to the manifest default (matching Ruby buildpack behaviour).
446+ func SelectTomcatVersionPattern (javaHome , configVersion string ) (string , error ) {
447+ if javaHome == "" {
448+ return "" , nil
449+ }
450+
451+ javaMajorVersion , err := common .DetermineJavaVersion (javaHome )
452+ if err != nil {
453+ if configVersion != "" {
454+ return configVersion , nil
455+ }
456+ return "" , nil
457+ }
458+
459+ if configVersion != "" {
460+ if strings .HasPrefix (configVersion , "10." ) && javaMajorVersion < 11 {
461+ return "" , fmt .Errorf ("Tomcat 10.x requires Java 11+, but Java %d detected" , javaMajorVersion )
462+ }
463+ return configVersion , nil
464+ }
465+
466+ if javaMajorVersion >= 11 {
467+ return "10.x" , nil
468+ }
469+ return "9.x" , nil
470+ }
471+
472+
467473// based on the JBP_CONFIG_TOMCAT field from manifest.
468474// It looks for a tomcat block with a version of the form "<major>.+" (e.g. "9.+", "10.+", "10.1.+").
469475// Returns the pattern with "+" replaced by "*" (e.g. "9.*", "10.*", "10.1.*") so libbuildpack can resolve it.
0 commit comments