Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 = "<!-- ADDITIONAL_CONFIGURATION -->";
String additionalConfigReplacement = "<jdkToolchain>\n" +
" <version>11</version>\n" +
" </jdkToolchain>\n" +
" <!-- ADDITIONAL_CONFIGURATION -->";
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 = "<!-- ADDITIONAL_CONFIGURATION -->";
String additionalConfigReplacement = "<jdkToolchain>\n" +
" <version>11</version>\n" +
" </jdkToolchain>\n" +
" <!-- ADDITIONAL_CONFIGURATION -->";
replaceString(additionalConfigMarker, additionalConfigReplacement, pom);

String pluginsEndMarker = "</plugins>";
String compilerPluginReplacement = "<plugin>\n" +
" <groupId>org.apache.maven.plugins</groupId>\n" +
" <artifactId>maven-compiler-plugin</artifactId>\n" +
" <version>3.11.0</version>\n" +
" <configuration>\n" +
" <jdkToolchain>\n" +
" <version>11</version>\n" +
" </jdkToolchain>\n" +
" <release>11</release>\n" +
" <source>11</source>\n" +
" <target>11</target>\n" +
" </configuration>\n" +
" </plugin>\n" +
" </plugins>";
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 = "<!-- ADDITIONAL_CONFIGURATION -->";
String additionalConfigReplacement = "<jdkToolchain>\n" +
" <version>11</version>\n" +
" </jdkToolchain>\n" +
" <!-- ADDITIONAL_CONFIGURATION -->";
replaceString(additionalConfigMarker, additionalConfigReplacement, pom);

String pluginsEndMarker = "</plugins>";
String compilerPluginReplacement = "<plugin>\n" +
" <groupId>org.apache.maven.plugins</groupId>\n" +
" <artifactId>maven-compiler-plugin</artifactId>\n" +
" <version>3.11.0</version>\n" +
" <configuration>\n" +
" <jdkToolchain>\n" +
" <version>8</version>\n" +
" </jdkToolchain>\n" +
" <release>8</release>\n" +
" <source>8</source>\n" +
" <target>8</target>\n" +
" </configuration>\n" +
" </plugin>\n" +
" </plugins>";
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -424,6 +425,14 @@ public String getProjectName() {
return project.getArtifactId();
}

@Override
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 {
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 {
Expand Down Expand Up @@ -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<String, String> debugEnv = getDebugEnvironmentVariables();
if (toolchain != null) {
String toolchainJavaHome = getJdkHomeFromToolchain(toolchain);
if (toolchainJavaHome != null) {
debugEnv.put("JAVA_HOME", toolchainJavaHome);
}
}
serverTask.setEnvironmentVariables(debugEnv);
} else {
serverTask.setOperation("run");
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down