Skip to content

Commit cb11e6e

Browse files
authored
Merge pull request #1949 from sajeerzeji/toolchain_devmode
Toolchain support for DevMode
2 parents ad750f0 + 6bf5136 commit cb11e6e

File tree

4 files changed

+273
-9
lines changed

4 files changed

+273
-9
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package net.wasdev.wlp.test.dev.it;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.File;
6+
7+
import org.apache.commons.io.FileUtils;
8+
import org.junit.Test;
9+
10+
public class DevToolchainTest extends BaseDevTest {
11+
12+
@Test
13+
public void libertyToolchainWithoutCompilerToolchainLogsInfoAndUsesToolchainVersion() throws Exception {
14+
setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null);
15+
try {
16+
String additionalConfigMarker = "<!-- ADDITIONAL_CONFIGURATION -->";
17+
String additionalConfigReplacement = "<jdkToolchain>\n" +
18+
" <version>11</version>\n" +
19+
" </jdkToolchain>\n" +
20+
" <!-- ADDITIONAL_CONFIGURATION -->";
21+
replaceString(additionalConfigMarker, additionalConfigReplacement, pom);
22+
23+
startProcess(null, true, "mvn liberty:");
24+
25+
assertTrue(verifyLogMessageExists("Maven compiler plugin is not configured with a jdkToolchain. Using Liberty Maven Plugin jdkToolchain configuration for Java compiler options.", 120000));
26+
assertTrue(verifyLogMessageExists("Setting compiler source to toolchain JDK version 11", 120000));
27+
28+
// Trigger a recompile by modifying a Java source file
29+
File javaFile = new File(tempProj, "src/main/java/com/demo/HelloWorld.java");
30+
String originalContent = "public String helloWorld() {\n\t\treturn \"helloWorld\";\n\t}";
31+
String modifiedContent = "public String helloWorld() {\n\t\treturn \"helloWorldModified\";\n\t}";
32+
String fileContent = FileUtils.readFileToString(javaFile, "UTF-8");
33+
String newContent = fileContent.replace(originalContent, modifiedContent);
34+
FileUtils.writeStringToFile(javaFile, newContent, "UTF-8");
35+
36+
// Verify that recompilation used compiler options
37+
assertTrue(verifyLogMessageExists("Recompiling with compiler options:", 120000));
38+
assertTrue(verifyLogMessageExists("-source, 11", 120000));
39+
assertTrue(verifyLogMessageExists("-target, 11", 120000));
40+
} finally {
41+
cleanUpAfterClass();
42+
}
43+
}
44+
45+
@Test
46+
public void noToolchainConfigurationDoesNotEmitToolchainMessages() throws Exception {
47+
setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null);
48+
try {
49+
startProcess(null, true, "mvn liberty:");
50+
51+
assertTrue(verifyLogMessageDoesNotExist(
52+
"Maven compiler plugin is not configured with a jdkToolchain. Using Liberty Maven Plugin jdkToolchain configuration for Java compiler options.",
53+
120000));
54+
assertTrue(verifyLogMessageDoesNotExist(
55+
"Liberty Maven Plugin jdkToolchain configuration matches the Maven Compiler Plugin jdkToolchain configuration",
56+
120000));
57+
assertTrue(verifyLogMessageDoesNotExist(
58+
"Liberty Maven Plugin jdkToolchain configuration (version",
59+
120000));
60+
} finally {
61+
cleanUpAfterClass();
62+
}
63+
}
64+
65+
@Test
66+
public void matchingToolchainConfigurationsLogInfoMessage() throws Exception {
67+
setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null);
68+
try {
69+
String additionalConfigMarker = "<!-- ADDITIONAL_CONFIGURATION -->";
70+
String additionalConfigReplacement = "<jdkToolchain>\n" +
71+
" <version>11</version>\n" +
72+
" </jdkToolchain>\n" +
73+
" <!-- ADDITIONAL_CONFIGURATION -->";
74+
replaceString(additionalConfigMarker, additionalConfigReplacement, pom);
75+
76+
String pluginsEndMarker = "</plugins>";
77+
String compilerPluginReplacement = "<plugin>\n" +
78+
" <groupId>org.apache.maven.plugins</groupId>\n" +
79+
" <artifactId>maven-compiler-plugin</artifactId>\n" +
80+
" <version>3.11.0</version>\n" +
81+
" <configuration>\n" +
82+
" <jdkToolchain>\n" +
83+
" <version>11</version>\n" +
84+
" </jdkToolchain>\n" +
85+
" <release>11</release>\n" +
86+
" <source>11</source>\n" +
87+
" <target>11</target>\n" +
88+
" </configuration>\n" +
89+
" </plugin>\n" +
90+
" </plugins>";
91+
replaceString(pluginsEndMarker, compilerPluginReplacement, pom);
92+
93+
startProcess(null, true, "mvn liberty:");
94+
95+
assertTrue(verifyLogMessageExists("Liberty Maven Plugin jdkToolchain configuration matches the Maven Compiler Plugin jdkToolchain configuration: version 11.", 120000));
96+
} finally {
97+
cleanUpAfterClass();
98+
}
99+
}
100+
101+
@Test
102+
public void mismatchedToolchainConfigurationsLogWarningMessage() throws Exception {
103+
setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null);
104+
try {
105+
String additionalConfigMarker = "<!-- ADDITIONAL_CONFIGURATION -->";
106+
String additionalConfigReplacement = "<jdkToolchain>\n" +
107+
" <version>11</version>\n" +
108+
" </jdkToolchain>\n" +
109+
" <!-- ADDITIONAL_CONFIGURATION -->";
110+
replaceString(additionalConfigMarker, additionalConfigReplacement, pom);
111+
112+
String pluginsEndMarker = "</plugins>";
113+
String compilerPluginReplacement = "<plugin>\n" +
114+
" <groupId>org.apache.maven.plugins</groupId>\n" +
115+
" <artifactId>maven-compiler-plugin</artifactId>\n" +
116+
" <version>3.11.0</version>\n" +
117+
" <configuration>\n" +
118+
" <jdkToolchain>\n" +
119+
" <version>8</version>\n" +
120+
" </jdkToolchain>\n" +
121+
" <release>8</release>\n" +
122+
" <source>8</source>\n" +
123+
" <target>8</target>\n" +
124+
" </configuration>\n" +
125+
" </plugin>\n" +
126+
" </plugins>";
127+
replaceString(pluginsEndMarker, compilerPluginReplacement, pom);
128+
129+
startProcess(null, true, "mvn liberty:");
130+
131+
assertTrue(verifyLogMessageExists("Liberty Maven Plugin jdkToolchain configuration (version 11) does not match the Maven Compiler Plugin jdkToolchain configuration (version 8). The Liberty Maven Plugin jdkToolchain configuration will be used for compilation.", 120000));
132+
} finally {
133+
cleanUpAfterClass();
134+
}
135+
}
136+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package net.wasdev.wlp.test.dev.it;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
6+
7+
import org.apache.maven.model.Plugin;
8+
import org.apache.maven.plugin.logging.SystemStreamLog;
9+
import org.codehaus.plexus.util.xml.Xpp3Dom;
10+
import org.junit.Test;
11+
12+
import io.openliberty.tools.maven.utils.ExecuteMojoUtil;
13+
14+
public class ToolchainSurefireFailsafeConfigTest {
15+
16+
@Test
17+
public void jdkToolchainIsPreservedForMavenSurefirePluginTestGoal() {
18+
Plugin plugin = new Plugin();
19+
plugin.setGroupId("org.apache.maven.plugins");
20+
plugin.setArtifactId("maven-surefire-plugin");
21+
plugin.setVersion("3.1.2");
22+
23+
Xpp3Dom config = new Xpp3Dom("configuration");
24+
Xpp3Dom jdkToolchain = new Xpp3Dom("jdkToolchain");
25+
Xpp3Dom version = new Xpp3Dom("version");
26+
version.setValue("11");
27+
jdkToolchain.addChild(version);
28+
config.addChild(jdkToolchain);
29+
30+
plugin.setConfiguration(config);
31+
32+
Xpp3Dom goalConfig = ExecuteMojoUtil.getPluginGoalConfig(plugin, "test", new SystemStreamLog());
33+
Xpp3Dom jdkToolchainChild = goalConfig.getChild("jdkToolchain");
34+
Xpp3Dom versionChild = jdkToolchainChild.getChild("version");
35+
36+
assertNotNull("jdkToolchain element should be preserved for maven-surefire-plugin:test", jdkToolchainChild);
37+
assertNotNull("version child should be present under jdkToolchain", versionChild);
38+
assertEquals("11", versionChild.getValue());
39+
}
40+
41+
@Test
42+
public void jdkToolchainIsPreservedForMavenFailsafePluginIntegrationTestGoal() {
43+
Plugin plugin = new Plugin();
44+
plugin.setGroupId("org.apache.maven.plugins");
45+
plugin.setArtifactId("maven-failsafe-plugin");
46+
plugin.setVersion("3.1.2");
47+
48+
Xpp3Dom config = new Xpp3Dom("configuration");
49+
Xpp3Dom jdkToolchain = new Xpp3Dom("jdkToolchain");
50+
Xpp3Dom version = new Xpp3Dom("version");
51+
version.setValue("11");
52+
jdkToolchain.addChild(version);
53+
config.addChild(jdkToolchain);
54+
55+
plugin.setConfiguration(config);
56+
57+
Xpp3Dom goalConfig = ExecuteMojoUtil.getPluginGoalConfig(plugin, "integration-test", new SystemStreamLog());
58+
Xpp3Dom jdkToolchainChild = goalConfig.getChild("jdkToolchain");
59+
Xpp3Dom versionChild = jdkToolchainChild.getChild("version");
60+
61+
assertNotNull("jdkToolchain element should be preserved for maven-failsafe-plugin:integration-test", jdkToolchainChild);
62+
assertNotNull("version child should be present under jdkToolchain", versionChild);
63+
assertEquals("11", versionChild.getValue());
64+
}
65+
}

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

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import io.openliberty.tools.common.plugins.util.LibertyPropFilesUtility;
4545
import io.openliberty.tools.maven.utils.CommonLogger;
46+
import org.apache.commons.lang3.StringUtils;
4647
import org.apache.maven.artifact.Artifact;
4748
import org.apache.maven.artifact.DependencyResolutionRequiredException;
4849
import org.apache.maven.execution.MavenSession;
@@ -424,6 +425,14 @@ public String getProjectName() {
424425
return project.getArtifactId();
425426
}
426427

428+
@Override
429+
protected boolean recompileJava(Collection<File> javaFilesChanged, Set<String> artifactPaths, ThreadPoolExecutor executor, boolean tests, File outputDirectory, File testOutputDirectory, String projectName, File projectBuildFile, JavaCompilerOptions projectCompilerOptions, boolean forceSkipUTs, boolean skipRunningTests) throws PluginExecutionException {
430+
if (projectCompilerOptions != null && projectCompilerOptions.getOptions() != null) {
431+
getLog().info("Recompiling with compiler options: " + projectCompilerOptions.getOptions());
432+
}
433+
return super.recompileJava(javaFilesChanged, artifactPaths, executor, tests, outputDirectory, testOutputDirectory, projectName, projectBuildFile, projectCompilerOptions, forceSkipUTs, skipRunningTests);
434+
}
435+
427436
@Override
428437
public void libertyCreate() throws PluginExecutionException {
429438
try {
@@ -520,7 +529,16 @@ public ServerTask getServerTask() throws Exception {
520529

521530
// set environment variables for server start task
522531
serverTask.setOperation("debug");
523-
serverTask.setEnvironmentVariables(getDebugEnvironmentVariables());
532+
533+
// Merge toolchain environment with debug environment
534+
Map<String, String> debugEnv = getDebugEnvironmentVariables();
535+
if (toolchain != null) {
536+
String toolchainJavaHome = getJdkHomeFromToolchain(toolchain);
537+
if (toolchainJavaHome != null) {
538+
debugEnv.put("JAVA_HOME", toolchainJavaHome);
539+
}
540+
}
541+
serverTask.setEnvironmentVariables(debugEnv);
524542
} else {
525543
serverTask.setOperation("run");
526544
}
@@ -1740,24 +1758,69 @@ private JavaCompilerOptions getMavenCompilerOptions(MavenProject currentProject)
17401758
String release = getCompilerOption(configuration, "release", "maven.compiler.release", currentProject);
17411759
String source = getCompilerOption(configuration, "source", "maven.compiler.source", currentProject);
17421760
String target = getCompilerOption(configuration, "target", "maven.compiler.target", currentProject);
1761+
1762+
// Fetch the toolchain version configured for the project
1763+
String jdkToolchainVersion = jdkToolchain != null ? jdkToolchain.get("version") : null;
1764+
if (StringUtils.isNotEmpty(jdkToolchainVersion)) {
1765+
// Fetch the toolchain version configured for the maven-compiler-plugin
1766+
String compilerJdkToolchainVersion = null;
1767+
if (configuration != null) {
1768+
Xpp3Dom compilerJdkToolchain = configuration.getChild("jdkToolchain");
1769+
if (compilerJdkToolchain != null) {
1770+
Xpp3Dom versionChild = compilerJdkToolchain.getChild("version");
1771+
if (versionChild != null) {
1772+
compilerJdkToolchainVersion = StringUtils.trimToNull(versionChild.getValue());
1773+
}
1774+
}
1775+
}
1776+
1777+
// Log which toolchain version is being used for maven-compiler-plugin
1778+
if (compilerJdkToolchainVersion == null) {
1779+
getLog().info("Maven compiler plugin is not configured with a jdkToolchain. "
1780+
+ "Using Liberty Maven Plugin jdkToolchain configuration for Java compiler options.");
1781+
} else {
1782+
if (jdkToolchainVersion.equals(compilerJdkToolchainVersion)) {
1783+
getLog().info("Liberty Maven Plugin jdkToolchain configuration matches the Maven Compiler Plugin jdkToolchain "
1784+
+ "configuration: version " + jdkToolchainVersion + ".");
1785+
} else {
1786+
getLog().warn("Liberty Maven Plugin jdkToolchain configuration (version " + jdkToolchainVersion
1787+
+ ") does not match the Maven Compiler Plugin jdkToolchain configuration "
1788+
+ "(version " + compilerJdkToolchainVersion
1789+
+ "). The Liberty Maven Plugin jdkToolchain configuration will be used for compilation.");
1790+
}
1791+
}
1792+
}
1793+
17431794
if (release != null) {
1744-
getLog().debug("Setting compiler release to " + release);
1795+
if (StringUtils.isNotEmpty(jdkToolchainVersion)) {
1796+
getLog().info("Setting compiler release to toolchain JDK version " + jdkToolchainVersion);
1797+
} else {
1798+
getLog().debug("Setting compiler release to " + release);
1799+
}
17451800
if (source != null) {
17461801
getLog().debug("Compiler option source will be ignored since release is specified");
17471802
}
17481803
if (target != null) {
17491804
getLog().debug("Compiler option target will be ignored since release is specified");
17501805
}
1751-
compilerOptions.setRelease(release);
1806+
compilerOptions.setRelease(StringUtils.isNotEmpty(jdkToolchainVersion) ? jdkToolchainVersion : release);
17521807
} else {
17531808
// add source and target only if release is not set
17541809
if (source != null) {
1755-
getLog().debug("Setting compiler source to " + source);
1756-
compilerOptions.setSource(source);
1810+
if (StringUtils.isNotEmpty(jdkToolchainVersion)) {
1811+
getLog().info("Setting compiler source to toolchain JDK version " + jdkToolchainVersion);
1812+
} else {
1813+
getLog().debug("Setting compiler source to " + source);
1814+
}
1815+
compilerOptions.setSource(StringUtils.isNotEmpty(jdkToolchainVersion) ? jdkToolchainVersion : source);
17571816
}
17581817
if (target != null) {
1759-
getLog().debug("Setting compiler target to " + target);
1760-
compilerOptions.setTarget(target);
1818+
if (StringUtils.isNotEmpty(jdkToolchainVersion)) {
1819+
getLog().info("Setting compiler target to toolchain JDK version " + jdkToolchainVersion);
1820+
} else {
1821+
getLog().debug("Setting compiler target to " + target);
1822+
}
1823+
compilerOptions.setTarget(StringUtils.isNotEmpty(jdkToolchainVersion) ? jdkToolchainVersion : target);
17611824
}
17621825
}
17631826

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class ExecuteMojoUtil {
9898
"testFailureIgnore", "testNGArtifactName", "threadCount", "threadCountClasses",
9999
"threadCountMethods", "threadCountSuites", "trimStackTrace", "useFile",
100100
"useManifestOnlyJar", "useModulePath", "useSystemClassLoader",
101-
"useUnlimitedThreads", "workingDirectory"
101+
"useUnlimitedThreads", "workingDirectory", "jdkToolchain"
102102
));
103103

104104
// https://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html
@@ -120,7 +120,7 @@ public class ExecuteMojoUtil {
120120
"systemPropertyVariables", "tempDir", "test", "testClassesDirectory",
121121
"testNGArtifactName", "threadCount", "threadCountClasses", "threadCountMethods",
122122
"threadCountSuites", "trimStackTrace", "useFile", "useManifestOnlyJar",
123-
"useModulePath", "useSystemClassLoader", "useUnlimitedThreads", "workingDirectory"
123+
"useModulePath", "useSystemClassLoader", "useUnlimitedThreads", "workingDirectory", "jdkToolchain"
124124
));
125125

126126
// https://maven.apache.org/surefire/maven-failsafe-plugin/verify-mojo.html

0 commit comments

Comments
 (0)