From 5a19012987c8db02efbcd6ecea5bf62ca363f79f Mon Sep 17 00:00:00 2001 From: Cheryl King Date: Sat, 4 May 2024 11:18:00 -0500 Subject: [PATCH] Determine correct jspEngine attribute based on Liberty version --- .../tools/ant/jsp/CompileJSPs.java | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/openliberty/tools/ant/jsp/CompileJSPs.java b/src/main/java/io/openliberty/tools/ant/jsp/CompileJSPs.java index 6a34be9..892cfde 100644 --- a/src/main/java/io/openliberty/tools/ant/jsp/CompileJSPs.java +++ b/src/main/java/io/openliberty/tools/ant/jsp/CompileJSPs.java @@ -27,6 +27,7 @@ import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Properties; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -59,6 +60,7 @@ public class CompileJSPs extends Task { private String classpath = ""; private String classpathRef; private String source; + private boolean useJdkSourceLevel = false; // By default allow 30 seconds to compile the jsps private int timeout = 30; @@ -365,7 +367,37 @@ private ServerTask createServerTask(File usrDir) { return server; } + private String determineSourceAttribute(File serverDir) { + String sourceAttrToUse = "jdkSourceLevel"; + File f = new File(wlpHome,"lib/versions/openliberty.properties"); + + if (f.exists()) { + try (FileInputStream input = new FileInputStream(f);) { + Properties libertyProductProperties = new Properties(); + + libertyProductProperties.load(input); + String version = libertyProductProperties.getProperty("com.ibm.websphere.productVersion"); + if (version != null && version.startsWith("24.")) { + if (useJdkSourceLevel && source.equals("18")) { + // change source 18 to 8 and use javaSourceLevel instead + source = "8"; + useJdkSourceLevel = false; + sourceAttrToUse = "javaSourceLevel"; + } else if (!useJdkSourceLevel) { + sourceAttrToUse = "javaSourceLevel"; + } + } + } catch (IOException e) { + } + } + return sourceAttrToUse; + } + private void writeServerXML(File serverDir) throws FileNotFoundException { + // Need to determine the version of Liberty installed. For versions prior to 24.0.0.1, use the jdkSourceLevel attribute + // on the jspEngine element. For versions 24.0.0.1 and later, use the javaSourceLevel attribute. + String sourceAttribute = determineSourceAttribute(serverDir); + PrintStream ps = new PrintStream(new File(serverDir, "server.xml")); ps.println(""); ps.println(""); @@ -384,7 +416,7 @@ private void writeServerXML(File serverDir) throws FileNotFoundException { ps.println(""); } ps.println(""); - ps.print(""); + ps.println(""); ps.println(""); ps.println(""); ps.println(""); @@ -489,6 +521,8 @@ public void setClasspathRef(String classpathRef) { } public void setSource(String src) { + useJdkSourceLevel = false; + source = null; if ("1.3".equals(src)) { source = "13"; } else if ("1.4".equals(src)) { @@ -501,6 +535,10 @@ public void setSource(String src) { source = "17"; } else if ("1.8".equals(src) || "8".equals(src)) { source = "18"; + } + + if (source != null) { + useJdkSourceLevel = true; } else { // for Java 11 and beyond source = src; }