Skip to content

Commit f1c7eab

Browse files
authored
Merge pull request #1946 from venmanyarun/compile_jsp_toolchain_support
configuring toolchain for compile-jsp
2 parents fce88d9 + 1b465bf commit f1c7eab

File tree

6 files changed

+146
-90
lines changed

6 files changed

+146
-90
lines changed

liberty-maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<dependency>
3939
<groupId>io.openliberty.tools</groupId>
4040
<artifactId>liberty-ant-tasks</artifactId>
41-
<version>1.9.17</version>
41+
<version>1.9.18-SNAPSHOT</version>
4242
</dependency>
4343
<dependency>
4444
<groupId>org.apache.maven</groupId>

liberty-maven-plugin/src/it/compile-jsp-source-17-it/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<version>@pom.version@</version>
6767
<extensions>true</extensions>
6868
<configuration>
69+
<jdkToolchain>
70+
<version>11</version>
71+
</jdkToolchain>
6972
<stripVersion>true</stripVersion>
7073
<serverName>test</serverName>
7174
<serverXmlFile>src/test/resources/server.xml</serverXmlFile>

liberty-maven-plugin/src/it/compile-jsp-source-17-it/src/test/java/net/wasdev/wlp/maven/test/app/CompileJSPTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package net.wasdev.wlp.maven.test.app;
22

3+
34
import java.awt.List;
45
import java.util.ArrayList;
56
import java.io.File;
7+
import java.io.FileNotFoundException;
68
import java.io.FileInputStream;
79

810
import javax.xml.parsers.DocumentBuilder;
@@ -11,8 +13,9 @@
1113
import javax.xml.xpath.XPathConstants;
1214
import javax.xml.xpath.XPathFactory;
1315

14-
import org.junit.Assert;
16+
import java.util.Scanner;
1517
import org.junit.Test;
18+
import org.junit.Assert;
1619
import org.w3c.dom.Document;
1720
import org.w3c.dom.NodeList;
1821
import org.w3c.dom.Node;
@@ -28,6 +31,8 @@
2831
public class CompileJSPTest {
2932

3033
public final String COMPILE_JSP_SEVER_XML = "compileJsp/servers/defaultServer/server.xml";
34+
public final String LOG_LOCATION = "liberty/wlp/usr/servers/test/logs/messages.log";
35+
static final String TOOLCHAIN_CONFIGURED_FOR_GOAL = "CWWKM4101I: The %s goal is using the configured toolchain JDK located at";
3136

3237
@Test
3338
public void testServerXMLPropFileExist() throws Exception {
@@ -79,4 +84,36 @@ public void testXmlElements() throws Exception {
7984
}
8085
}
8186
}
87+
88+
@Test
89+
public void testToolchainLogs() throws Exception {
90+
File buildLog = new File("../build.log");
91+
Assert.assertTrue(buildLog.exists());
92+
93+
Assert.assertTrue("Did not find toolchain honored message for create goal in build.log", logContainsMessage(buildLog, String.format(TOOLCHAIN_CONFIGURED_FOR_GOAL, "create")));
94+
Assert.assertTrue("Did not find toolchain honored message for start goal in build.log", logContainsMessage(buildLog, String.format(TOOLCHAIN_CONFIGURED_FOR_GOAL, "start")));
95+
Assert.assertTrue("Did not find toolchain honored message for compile-jsp goal in build.log", logContainsMessage(buildLog, String.format(TOOLCHAIN_CONFIGURED_FOR_GOAL, "compile-jsp")));
96+
97+
File f = new File(LOG_LOCATION);
98+
Assert.assertTrue(f.getCanonicalFile() + " doesn't exist", f.exists());
99+
// should contain java.version = 11 since <jdkToolChain> is defined as Java 11
100+
Assert.assertTrue("Did not find toolchain version in messages.log", logContainsMessage(f, "java.version = 11"));
101+
102+
}
103+
104+
105+
private boolean logContainsMessage( File logFile, String message) throws FileNotFoundException {
106+
Assert.assertTrue("Log file not found at location: "+ LOG_LOCATION, logFile.exists());
107+
boolean found = false;
108+
109+
try (Scanner scanner = new Scanner(logFile);) {
110+
while (scanner.hasNextLine()) {
111+
if(scanner.nextLine().contains(message)) {
112+
found = true;
113+
}
114+
}
115+
}
116+
117+
return found;
118+
}
82119
}

liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/ServerFeatureSupport.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222

2323
import java.io.File;
2424
import java.io.IOException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
2527
import java.text.MessageFormat;
2628
import java.util.ArrayList;
29+
import java.util.Collections;
30+
import java.util.HashMap;
2731
import java.util.LinkedHashSet;
2832
import java.util.List;
2933
import java.util.Map;
@@ -440,4 +444,91 @@ protected String getJdkHomeFromToolchain(Toolchain toolchain) {
440444
}
441445
return null;
442446
}
447+
448+
/**
449+
* @return environment variable map with Toolchain JDK
450+
*/
451+
protected Map<String, String> getToolchainEnvVar() {
452+
453+
if (toolchain == null) {
454+
return Collections.emptyMap();
455+
}
456+
String jdkHome = getJdkHomeFromToolchain(toolchain);
457+
if (jdkHome == null) {
458+
getLog().warn("Could not determine JDK home from toolchain. Toolchain will not be honored");
459+
return Collections.emptyMap();
460+
}
461+
462+
// 1. Read existing config files
463+
List<String> serverEnvLines = readConfigFileLines(getServerEnvFile());
464+
List<String> jvmOptionsLines = readConfigFileLines(new File(serverDirectory, "jvm.options"));
465+
466+
// 2. Check for existing JAVA_HOME configuration
467+
// if user has configured JAVA_HOME in server.env or jvm.options, this will get higher precedence over toolchain JDK
468+
// hence a warning will be issued
469+
if (isJavaHomeSet(serverEnvLines, jvmOptionsLines)) {
470+
getLog().warn(MessageFormat.format(
471+
messages.getString("warn.server.env.java.home.configured"),
472+
mojoExecution.getGoal()
473+
));
474+
} else {
475+
// 3. Apply toolchain configuration
476+
return populateEnviornmentVariablesMap(jdkHome);
477+
}
478+
return Collections.emptyMap();
479+
}
480+
481+
/**
482+
* Determines the primary server.env file to read.
483+
* Checks serverEnvFile first, then a default location in serverDirectory.
484+
*
485+
* @return The File object for the server.env, or null if neither exists or is specified.
486+
*/
487+
private File getServerEnvFile() {
488+
if (serverEnvFile != null && serverEnvFile.exists()) {
489+
return serverEnvFile;
490+
}
491+
File defaultServerEnv = new File(serverDirectory, "server.env");
492+
if (defaultServerEnv.exists()) {
493+
return defaultServerEnv;
494+
}
495+
return null;
496+
}
497+
498+
/**
499+
* Reads all lines from a configuration file, handling null/non-existent files
500+
* and I/O exceptions gracefully.
501+
*
502+
* @param configFile The file to read.
503+
* @return A list of strings, each representing a line in the file. Returns an empty list on failure.
504+
*/
505+
private List<String> readConfigFileLines(File configFile) {
506+
if (configFile == null || !configFile.exists()) {
507+
return Collections.emptyList();
508+
}
509+
Path configPath = configFile.toPath();
510+
try {
511+
return Files.readAllLines(configPath);
512+
} catch (IOException e) {
513+
getLog().warn("Error reading config file: " + configPath);
514+
return Collections.emptyList();
515+
}
516+
}
517+
518+
/**
519+
* Applies the toolchain's JDK home to the ServerTask's environment variables.
520+
*
521+
* @param jdkHome The resolved JDK home path.
522+
* @return envVars
523+
*/
524+
private Map<String, String> populateEnviornmentVariablesMap(String jdkHome) {
525+
getLog().info(MessageFormat.format(
526+
messages.getString("info.toolchain.configured"),
527+
mojoExecution.getGoal(),
528+
jdkHome
529+
));
530+
Map<String, String> envVars = new HashMap<>();
531+
envVars.put("JAVA_HOME", jdkHome);
532+
return envVars;
533+
}
443534
}

liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/jsp/CompileJspMojo.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import java.util.HashSet;
2121
import java.util.Iterator;
2222
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Objects;
2325
import java.util.Properties;
2426
import java.util.Set;
2527
import java.util.TreeSet;
2628

29+
import org.apache.commons.lang3.ObjectUtils;
2730
import org.apache.maven.artifact.Artifact;
2831
import org.apache.maven.model.Plugin;
2932
import org.apache.maven.plugin.MojoExecutionException;
@@ -83,7 +86,11 @@ private void doCompileJsps() throws MojoExecutionException {
8386
compile.setDestdir(new File(getProject().getBuild().getOutputDirectory()));
8487
compile.setTempdir(new File(getProject().getBuild().getDirectory()));
8588
compile.setTimeout(timeout);
86-
89+
// put toolchain jdk into environment variables
90+
Map<String, String> envVars = getToolchainEnvVar();
91+
if(ObjectUtils.isNotEmpty(envVars)) {
92+
compile.setEnvironmentVariables(envVars);
93+
}
8794
// don't delete temporary server dir
8895
compile.setCleanup(false);
8996

liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/StartDebugMojoSupport.java

Lines changed: 5 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.nio.file.Paths;
3333
import java.text.MessageFormat;
3434
import java.util.ArrayList;
35-
import java.util.Collections;
3635
import java.util.Date;
3736
import java.util.EnumSet;
3837
import java.util.HashMap;
@@ -47,6 +46,7 @@
4746
import javax.xml.parsers.ParserConfigurationException;
4847
import javax.xml.transform.TransformerException;
4948

49+
import org.apache.commons.lang3.ObjectUtils;
5050
import org.apache.maven.artifact.Artifact;
5151
import org.apache.maven.artifact.versioning.ComparableVersion;
5252
import org.apache.maven.execution.MavenSession;
@@ -173,93 +173,11 @@ protected ServerTask initializeJava() {
173173
serverTask.setServerName(serverName);
174174
serverTask.setUserDir(userDirectory);
175175
serverTask.setOutputDir(outputDirectory);
176-
configureToolchainEnvVar(serverTask);
177-
return serverTask;
178-
}
179-
/**
180-
* @param serverTask
181-
*/
182-
private void configureToolchainEnvVar(ServerTask serverTask) {
183-
184-
if (toolchain == null) {
185-
return;
186-
}
187-
String jdkHome = getJdkHomeFromToolchain(toolchain);
188-
if (jdkHome == null) {
189-
getLog().warn("Could not determine JDK home from toolchain. Toolchain will not be honored");
190-
return;
191-
}
192-
193-
// 1. Read existing config files
194-
List<String> serverEnvLines = readConfigFileLines(getServerEnvFile());
195-
List<String> jvmOptionsLines = readConfigFileLines(new File(serverDirectory, "jvm.options"));
196-
197-
// 2. Check for existing JAVA_HOME configuration
198-
// if user has configured JAVA_HOME in server.env or jvm.options, this will get higher precedence over toolchain JDK
199-
// hence a warning will be issued
200-
if (isJavaHomeSet(serverEnvLines, jvmOptionsLines)) {
201-
getLog().warn(MessageFormat.format(
202-
messages.getString("warn.server.env.java.home.configured"),
203-
mojoExecution.getGoal()
204-
));
205-
} else {
206-
// 3. Apply toolchain configuration
207-
applyToolchainToTask(serverTask, jdkHome);
208-
}
209-
}
210-
211-
/**
212-
* Determines the primary server.env file to read.
213-
* Checks serverEnvFile first, then a default location in serverDirectory.
214-
*
215-
* @return The File object for the server.env, or null if neither exists or is specified.
216-
*/
217-
private File getServerEnvFile() {
218-
if (serverEnvFile != null && serverEnvFile.exists()) {
219-
return serverEnvFile;
176+
Map<String, String> envVars = getToolchainEnvVar();
177+
if(ObjectUtils.isNotEmpty(envVars)) {
178+
serverTask.setEnvironmentVariables(envVars);
220179
}
221-
File defaultServerEnv = new File(serverDirectory, "server.env");
222-
if (defaultServerEnv.exists()) {
223-
return defaultServerEnv;
224-
}
225-
return null;
226-
}
227-
228-
/**
229-
* Reads all lines from a configuration file, handling null/non-existent files
230-
* and I/O exceptions gracefully.
231-
*
232-
* @param configFile The file to read.
233-
* @return A list of strings, each representing a line in the file. Returns an empty list on failure.
234-
*/
235-
private List<String> readConfigFileLines(File configFile) {
236-
if (configFile == null || !configFile.exists()) {
237-
return Collections.emptyList();
238-
}
239-
Path configPath = configFile.toPath();
240-
try {
241-
return Files.readAllLines(configPath);
242-
} catch (IOException e) {
243-
getLog().warn("Error reading config file: " + configPath);
244-
return Collections.emptyList();
245-
}
246-
}
247-
248-
/**
249-
* Applies the toolchain's JDK home to the ServerTask's environment variables.
250-
*
251-
* @param serverTask The task to modify.
252-
* @param jdkHome The resolved JDK home path.
253-
*/
254-
private void applyToolchainToTask(ServerTask serverTask, String jdkHome) {
255-
getLog().info(MessageFormat.format(
256-
messages.getString("info.toolchain.configured"),
257-
mojoExecution.getGoal(),
258-
jdkHome
259-
));
260-
Map<String, String> envVars = new HashMap<>();
261-
envVars.put("JAVA_HOME", jdkHome);
262-
serverTask.setEnvironmentVariables(envVars);
180+
return serverTask;
263181
}
264182

265183
protected void runMojo(String groupId, String artifactId, String goal) throws MojoExecutionException {

0 commit comments

Comments
 (0)