diff --git a/liberty-maven-plugin/src/it/dev-it/src/test/java/net/wasdev/wlp/test/dev/it/DevToolchainTest.java b/liberty-maven-plugin/src/it/dev-it/src/test/java/net/wasdev/wlp/test/dev/it/DevToolchainTest.java new file mode 100644 index 000000000..05c35397e --- /dev/null +++ b/liberty-maven-plugin/src/it/dev-it/src/test/java/net/wasdev/wlp/test/dev/it/DevToolchainTest.java @@ -0,0 +1,136 @@ +package net.wasdev.wlp.test.dev.it; + +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.apache.commons.io.FileUtils; +import org.junit.Test; + +public class DevToolchainTest extends BaseDevTest { + + @Test + public void libertyToolchainWithoutCompilerToolchainLogsInfoAndUsesToolchainVersion() throws Exception { + setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null); + try { + String additionalConfigMarker = ""; + String additionalConfigReplacement = "\n" + + " 11\n" + + " \n" + + " "; + replaceString(additionalConfigMarker, additionalConfigReplacement, pom); + + startProcess(null, true, "mvn liberty:"); + + assertTrue(verifyLogMessageExists("Maven compiler plugin is not configured with a jdkToolchain. Using Liberty Maven Plugin jdkToolchain configuration for Java compiler options.", 120000)); + assertTrue(verifyLogMessageExists("Setting compiler source to toolchain JDK version 11", 120000)); + + // Trigger a recompile by modifying a Java source file + File javaFile = new File(tempProj, "src/main/java/com/demo/HelloWorld.java"); + String originalContent = "public String helloWorld() {\n\t\treturn \"helloWorld\";\n\t}"; + String modifiedContent = "public String helloWorld() {\n\t\treturn \"helloWorldModified\";\n\t}"; + String fileContent = FileUtils.readFileToString(javaFile, "UTF-8"); + String newContent = fileContent.replace(originalContent, modifiedContent); + FileUtils.writeStringToFile(javaFile, newContent, "UTF-8"); + + // Verify that recompilation used compiler options + assertTrue(verifyLogMessageExists("Recompiling with compiler options:", 120000)); + assertTrue(verifyLogMessageExists("-source, 11", 120000)); + assertTrue(verifyLogMessageExists("-target, 11", 120000)); + } finally { + cleanUpAfterClass(); + } + } + + @Test + public void noToolchainConfigurationDoesNotEmitToolchainMessages() throws Exception { + setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null); + try { + startProcess(null, true, "mvn liberty:"); + + assertTrue(verifyLogMessageDoesNotExist( + "Maven compiler plugin is not configured with a jdkToolchain. Using Liberty Maven Plugin jdkToolchain configuration for Java compiler options.", + 120000)); + assertTrue(verifyLogMessageDoesNotExist( + "Liberty Maven Plugin jdkToolchain configuration matches the Maven Compiler Plugin jdkToolchain configuration", + 120000)); + assertTrue(verifyLogMessageDoesNotExist( + "Liberty Maven Plugin jdkToolchain configuration (version", + 120000)); + } finally { + cleanUpAfterClass(); + } + } + + @Test + public void matchingToolchainConfigurationsLogInfoMessage() throws Exception { + setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null); + try { + String additionalConfigMarker = ""; + String additionalConfigReplacement = "\n" + + " 11\n" + + " \n" + + " "; + replaceString(additionalConfigMarker, additionalConfigReplacement, pom); + + String pluginsEndMarker = ""; + String compilerPluginReplacement = "\n" + + " org.apache.maven.plugins\n" + + " maven-compiler-plugin\n" + + " 3.11.0\n" + + " \n" + + " \n" + + " 11\n" + + " \n" + + " 11\n" + + " 11\n" + + " 11\n" + + " \n" + + " \n" + + " "; + replaceString(pluginsEndMarker, compilerPluginReplacement, pom); + + startProcess(null, true, "mvn liberty:"); + + assertTrue(verifyLogMessageExists("Liberty Maven Plugin jdkToolchain configuration matches the Maven Compiler Plugin jdkToolchain configuration: version 11.", 120000)); + } finally { + cleanUpAfterClass(); + } + } + + @Test + public void mismatchedToolchainConfigurationsLogWarningMessage() throws Exception { + setUpBeforeClass(null, "../resources/basic-dev-project", true, false, null, null); + try { + String additionalConfigMarker = ""; + String additionalConfigReplacement = "\n" + + " 11\n" + + " \n" + + " "; + replaceString(additionalConfigMarker, additionalConfigReplacement, pom); + + String pluginsEndMarker = ""; + String compilerPluginReplacement = "\n" + + " org.apache.maven.plugins\n" + + " maven-compiler-plugin\n" + + " 3.11.0\n" + + " \n" + + " \n" + + " 8\n" + + " \n" + + " 8\n" + + " 8\n" + + " 8\n" + + " \n" + + " \n" + + " "; + replaceString(pluginsEndMarker, compilerPluginReplacement, pom); + + startProcess(null, true, "mvn liberty:"); + + 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)); + } finally { + cleanUpAfterClass(); + } + } +} diff --git a/liberty-maven-plugin/src/it/toolchain-run-it/src/test/java/net/wasdev/wlp/test/dev/it/ToolchainSurefireFailsafeConfigTest.java b/liberty-maven-plugin/src/it/toolchain-run-it/src/test/java/net/wasdev/wlp/test/dev/it/ToolchainSurefireFailsafeConfigTest.java new file mode 100644 index 000000000..e0cc50955 --- /dev/null +++ b/liberty-maven-plugin/src/it/toolchain-run-it/src/test/java/net/wasdev/wlp/test/dev/it/ToolchainSurefireFailsafeConfigTest.java @@ -0,0 +1,65 @@ +package net.wasdev.wlp.test.dev.it; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.apache.maven.model.Plugin; +import org.apache.maven.plugin.logging.SystemStreamLog; +import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.junit.Test; + +import io.openliberty.tools.maven.utils.ExecuteMojoUtil; + +public class ToolchainSurefireFailsafeConfigTest { + + @Test + public void jdkToolchainIsPreservedForMavenSurefirePluginTestGoal() { + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-surefire-plugin"); + plugin.setVersion("3.1.2"); + + Xpp3Dom config = new Xpp3Dom("configuration"); + Xpp3Dom jdkToolchain = new Xpp3Dom("jdkToolchain"); + Xpp3Dom version = new Xpp3Dom("version"); + version.setValue("11"); + jdkToolchain.addChild(version); + config.addChild(jdkToolchain); + + plugin.setConfiguration(config); + + Xpp3Dom goalConfig = ExecuteMojoUtil.getPluginGoalConfig(plugin, "test", new SystemStreamLog()); + Xpp3Dom jdkToolchainChild = goalConfig.getChild("jdkToolchain"); + Xpp3Dom versionChild = jdkToolchainChild.getChild("version"); + + assertNotNull("jdkToolchain element should be preserved for maven-surefire-plugin:test", jdkToolchainChild); + assertNotNull("version child should be present under jdkToolchain", versionChild); + assertEquals("11", versionChild.getValue()); + } + + @Test + public void jdkToolchainIsPreservedForMavenFailsafePluginIntegrationTestGoal() { + Plugin plugin = new Plugin(); + plugin.setGroupId("org.apache.maven.plugins"); + plugin.setArtifactId("maven-failsafe-plugin"); + plugin.setVersion("3.1.2"); + + Xpp3Dom config = new Xpp3Dom("configuration"); + Xpp3Dom jdkToolchain = new Xpp3Dom("jdkToolchain"); + Xpp3Dom version = new Xpp3Dom("version"); + version.setValue("11"); + jdkToolchain.addChild(version); + config.addChild(jdkToolchain); + + plugin.setConfiguration(config); + + Xpp3Dom goalConfig = ExecuteMojoUtil.getPluginGoalConfig(plugin, "integration-test", new SystemStreamLog()); + Xpp3Dom jdkToolchainChild = goalConfig.getChild("jdkToolchain"); + Xpp3Dom versionChild = jdkToolchainChild.getChild("version"); + + assertNotNull("jdkToolchain element should be preserved for maven-failsafe-plugin:integration-test", jdkToolchainChild); + assertNotNull("version child should be present under jdkToolchain", versionChild); + assertEquals("11", versionChild.getValue()); + } +} diff --git a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/DevMojo.java b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/DevMojo.java index 1a86f6f14..cd9b14585 100644 --- a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/DevMojo.java +++ b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/server/DevMojo.java @@ -43,6 +43,7 @@ import io.openliberty.tools.common.plugins.util.LibertyPropFilesUtility; import io.openliberty.tools.maven.utils.CommonLogger; +import org.apache.commons.lang3.StringUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.execution.MavenSession; @@ -424,6 +425,14 @@ public String getProjectName() { return project.getArtifactId(); } + @Override + protected boolean recompileJava(Collection javaFilesChanged, Set artifactPaths, ThreadPoolExecutor executor, boolean tests, File outputDirectory, File testOutputDirectory, String projectName, File projectBuildFile, JavaCompilerOptions projectCompilerOptions, boolean forceSkipUTs, boolean skipRunningTests) throws PluginExecutionException { + if (projectCompilerOptions != null && projectCompilerOptions.getOptions() != null) { + getLog().info("Recompiling with compiler options: " + projectCompilerOptions.getOptions()); + } + return super.recompileJava(javaFilesChanged, artifactPaths, executor, tests, outputDirectory, testOutputDirectory, projectName, projectBuildFile, projectCompilerOptions, forceSkipUTs, skipRunningTests); + } + @Override public void libertyCreate() throws PluginExecutionException { try { @@ -520,7 +529,16 @@ public ServerTask getServerTask() throws Exception { // set environment variables for server start task serverTask.setOperation("debug"); - serverTask.setEnvironmentVariables(getDebugEnvironmentVariables()); + + // Merge toolchain environment with debug environment + Map debugEnv = getDebugEnvironmentVariables(); + if (toolchain != null) { + String toolchainJavaHome = getJdkHomeFromToolchain(toolchain); + if (toolchainJavaHome != null) { + debugEnv.put("JAVA_HOME", toolchainJavaHome); + } + } + serverTask.setEnvironmentVariables(debugEnv); } else { serverTask.setOperation("run"); } @@ -1740,24 +1758,69 @@ private JavaCompilerOptions getMavenCompilerOptions(MavenProject currentProject) String release = getCompilerOption(configuration, "release", "maven.compiler.release", currentProject); String source = getCompilerOption(configuration, "source", "maven.compiler.source", currentProject); String target = getCompilerOption(configuration, "target", "maven.compiler.target", currentProject); + + // Fetch the toolchain version configured for the project + String jdkToolchainVersion = jdkToolchain != null ? jdkToolchain.get("version") : null; + if (StringUtils.isNotEmpty(jdkToolchainVersion)) { + // Fetch the toolchain version configured for the maven-compiler-plugin + String compilerJdkToolchainVersion = null; + if (configuration != null) { + Xpp3Dom compilerJdkToolchain = configuration.getChild("jdkToolchain"); + if (compilerJdkToolchain != null) { + Xpp3Dom versionChild = compilerJdkToolchain.getChild("version"); + if (versionChild != null) { + compilerJdkToolchainVersion = StringUtils.trimToNull(versionChild.getValue()); + } + } + } + + // Log which toolchain version is being used for maven-compiler-plugin + if (compilerJdkToolchainVersion == null) { + getLog().info("Maven compiler plugin is not configured with a jdkToolchain. " + + "Using Liberty Maven Plugin jdkToolchain configuration for Java compiler options."); + } else { + if (jdkToolchainVersion.equals(compilerJdkToolchainVersion)) { + getLog().info("Liberty Maven Plugin jdkToolchain configuration matches the Maven Compiler Plugin jdkToolchain " + + "configuration: version " + jdkToolchainVersion + "."); + } else { + getLog().warn("Liberty Maven Plugin jdkToolchain configuration (version " + jdkToolchainVersion + + ") does not match the Maven Compiler Plugin jdkToolchain configuration " + + "(version " + compilerJdkToolchainVersion + + "). The Liberty Maven Plugin jdkToolchain configuration will be used for compilation."); + } + } + } + if (release != null) { - getLog().debug("Setting compiler release to " + release); + if (StringUtils.isNotEmpty(jdkToolchainVersion)) { + getLog().info("Setting compiler release to toolchain JDK version " + jdkToolchainVersion); + } else { + getLog().debug("Setting compiler release to " + release); + } if (source != null) { getLog().debug("Compiler option source will be ignored since release is specified"); } if (target != null) { getLog().debug("Compiler option target will be ignored since release is specified"); } - compilerOptions.setRelease(release); + compilerOptions.setRelease(StringUtils.isNotEmpty(jdkToolchainVersion) ? jdkToolchainVersion : release); } else { // add source and target only if release is not set if (source != null) { - getLog().debug("Setting compiler source to " + source); - compilerOptions.setSource(source); + if (StringUtils.isNotEmpty(jdkToolchainVersion)) { + getLog().info("Setting compiler source to toolchain JDK version " + jdkToolchainVersion); + } else { + getLog().debug("Setting compiler source to " + source); + } + compilerOptions.setSource(StringUtils.isNotEmpty(jdkToolchainVersion) ? jdkToolchainVersion : source); } if (target != null) { - getLog().debug("Setting compiler target to " + target); - compilerOptions.setTarget(target); + if (StringUtils.isNotEmpty(jdkToolchainVersion)) { + getLog().info("Setting compiler target to toolchain JDK version " + jdkToolchainVersion); + } else { + getLog().debug("Setting compiler target to " + target); + } + compilerOptions.setTarget(StringUtils.isNotEmpty(jdkToolchainVersion) ? jdkToolchainVersion : target); } } diff --git a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/utils/ExecuteMojoUtil.java b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/utils/ExecuteMojoUtil.java index a716ebfd4..cfa050671 100644 --- a/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/utils/ExecuteMojoUtil.java +++ b/liberty-maven-plugin/src/main/java/io/openliberty/tools/maven/utils/ExecuteMojoUtil.java @@ -98,7 +98,7 @@ public class ExecuteMojoUtil { "testFailureIgnore", "testNGArtifactName", "threadCount", "threadCountClasses", "threadCountMethods", "threadCountSuites", "trimStackTrace", "useFile", "useManifestOnlyJar", "useModulePath", "useSystemClassLoader", - "useUnlimitedThreads", "workingDirectory" + "useUnlimitedThreads", "workingDirectory", "jdkToolchain" )); // https://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html @@ -120,7 +120,7 @@ public class ExecuteMojoUtil { "systemPropertyVariables", "tempDir", "test", "testClassesDirectory", "testNGArtifactName", "threadCount", "threadCountClasses", "threadCountMethods", "threadCountSuites", "trimStackTrace", "useFile", "useManifestOnlyJar", - "useModulePath", "useSystemClassLoader", "useUnlimitedThreads", "workingDirectory" + "useModulePath", "useSystemClassLoader", "useUnlimitedThreads", "workingDirectory", "jdkToolchain" )); // https://maven.apache.org/surefire/maven-failsafe-plugin/verify-mojo.html