From b00eac7645dc75d448acafb8581619a1540325e3 Mon Sep 17 00:00:00 2001 From: Adam Wisniewski Date: Wed, 25 Sep 2019 15:29:22 -0400 Subject: [PATCH] add dev mode IT --- .../src/it/test-liberty-dev-mode/pom.xml | 91 +++++++ .../resources/SystemHealth.java | 20 ++ .../resources/basic-dev-project/pom.xml | 178 ++++++++++++++ .../src/main/java/com/demo/HelloWorld.java | 8 + .../src/main/resources/placeHolder.txt | 0 .../wasdev/wlp/test/dev/it/BaseDevTest.java | 211 +++++++++++++++++ .../wlp/test/dev/it/DevHotTestingTest.java | 56 +++++ .../net/wasdev/wlp/test/dev/it/DevTest.java | 224 ++++++++++++++++++ .../runtimes/openliberty/LibertyRuntime.java | 48 ++-- 9 files changed, 822 insertions(+), 14 deletions(-) create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/pom.xml create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/SystemHealth.java create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/pom.xml create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/src/main/java/com/demo/HelloWorld.java create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/src/main/resources/placeHolder.txt create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/BaseDevTest.java create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevHotTestingTest.java create mode 100644 boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevTest.java diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/pom.xml b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/pom.xml new file mode 100644 index 00000000..67495677 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/pom.xml @@ -0,0 +1,91 @@ + + + 4.0.0 + + io.openliberty.tools.it + test-liberty-dev-mode + 1.0-SNAPSHOT + jar + + + UTF-8 + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.12 + + + io.openliberty.tools + liberty-maven-plugin + 3.0.1 + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.19.1 + + true + once + 1200 + -enableassertions + ${project.build.directory} + + **/*Test.java + + + @pom.version@ + ${runtimeVersion} + + false + + + + integration-test + + integration-test + + + + verify + install + + verify + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + true + + + + org.apache.maven.plugins + maven-jar-plugin + 3.1.0 + + + + test-jar + + + + + + + + diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/SystemHealth.java b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/SystemHealth.java new file mode 100644 index 00000000..b187fc1d --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/SystemHealth.java @@ -0,0 +1,20 @@ +package com.demo; + +import javax.enterprise.context.ApplicationScoped; +import org.eclipse.microprofile.health.Health; +import org.eclipse.microprofile.health.HealthCheck; +import org.eclipse.microprofile.health.HealthCheckResponse; + +@Health +@ApplicationScoped +public class SystemHealth implements HealthCheck { + @Override + public HealthCheckResponse call() { + if (!System.getProperty("wlp.server.name").startsWith("defaultServer")) { + return HealthCheckResponse.named(HelloWorld.class.getSimpleName()) + .withData("default server", "not available").down().build(); + } + return HealthCheckResponse.named(HelloWorld.class.getSimpleName()).withData("default server", "available").up() + .build(); + } +} diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/pom.xml b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/pom.xml new file mode 100644 index 00000000..12b5903f --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/pom.xml @@ -0,0 +1,178 @@ + + + + 4.0.0 + + boost.it + dev-sample-proj + 1.0-SNAPSHOT + war + + + UTF-8 + UTF-8 + 1.7 + 1.7 + 9000 + + + + + + sonatype-nexus-snapshots + Sonatype Nexus Snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + true + + + false + + + + + + + + org.microshed.boost.boms + mp14-bom + RUNTIME_VERSION + pom + import + + + + + + + + org.microshed.boost.runtimes + openliberty + + + org.microshed.boost.boosters + jaxrs + + + org.microshed.boost.boosters + jsonp + + + org.microshed.boost.boosters + cdi + + + org.microshed.boost.boosters + mp-config + + + org.microshed.boost.boosters + mp-rest-client + + + + + junit + junit + 4.12 + test + + + org.apache.cxf + cxf-rt-rs-client + 3.2.6 + test + + + org.apache.cxf + cxf-rt-rs-extension-providers + 3.2.6 + test + + + org.glassfish + javax.json + 1.0.4 + test + + + + + + + org.apache.maven.plugins + maven-war-plugin + 3.2.2 + + false + pom.xml + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M1 + + + test + default-test + + + **/it/** + + ${project.build.directory}/test-reports/unit + + + + + + + org.microshed.boost + boost-maven-plugin + RUNTIME_VERSION + + + + package + + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0-M1 + + + integration-test + integration-test + + integration-test + + + + **/it/**/*.java + + + + + verify-results + + verify + + + + + ${project.build.directory}/test-reports/it/failsafe-summary.xml + ${project.build.directory}/test-reports/it + + + + + diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/src/main/java/com/demo/HelloWorld.java b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/src/main/java/com/demo/HelloWorld.java new file mode 100644 index 00000000..f8220202 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/src/main/java/com/demo/HelloWorld.java @@ -0,0 +1,8 @@ +package com.demo; + +public class HelloWorld { + + public String helloWorld() { + return "helloWorld"; + } +} diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/src/main/resources/placeHolder.txt b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/resources/basic-dev-project/src/main/resources/placeHolder.txt new file mode 100644 index 00000000..e69de29b diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/BaseDevTest.java b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/BaseDevTest.java new file mode 100644 index 00000000..c1a07065 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/BaseDevTest.java @@ -0,0 +1,211 @@ +/******************************************************************************* + * (c) Copyright IBM Corporation 2019. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package net.wasdev.wlp.test.dev.it; + +import static org.junit.Assert.*; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Scanner; + +import org.apache.maven.shared.utils.io.FileUtils; + +public class BaseDevTest { + + static File tempProj; + static File basicDevProj; + static File logFile; + static File targetDir; + static File pom; + static BufferedWriter writer; + static Process process; + + static int logFileLineCount; + + protected static void setUpBeforeClass(String devModeParams, String logFileName) + throws IOException, InterruptedException, FileNotFoundException { + + logFileLineCount = 0; + + tempProj = Files.createTempDirectory("temp").toFile(); + assertTrue(tempProj.exists()); + + basicDevProj = new File("../resources/basic-dev-project"); + assertTrue(basicDevProj.exists()); + + FileUtils.copyDirectoryStructure(basicDevProj, tempProj); + assertTrue(tempProj.listFiles().length > 0); + + logFile = new File(logFileName + ".txt"); + assertTrue(logFile.createNewFile()); + + pom = new File(tempProj, "pom.xml"); + assertTrue(pom.exists()); + + replaceVersion(); + + startDevMode(devModeParams); + } + + private static void startDevMode(String devModeParams) + throws IOException, InterruptedException, FileNotFoundException { + // run dev mode on project + StringBuilder command = new StringBuilder("mvn io.openliberty.tools:liberty-maven-plugin:3.0.1:dev"); + if (devModeParams != null) { + command.append(" " + devModeParams); + } + ProcessBuilder builder = buildProcess(command.toString()); + + // builder.redirectError(logFile); + builder.redirectErrorStream(true); + builder.redirectOutput(logFile); + process = builder.start(); + assertTrue(process.isAlive()); + + OutputStream stdin = process.getOutputStream(); + + writer = new BufferedWriter(new OutputStreamWriter(stdin)); + } + + protected static void cleanUpAfterClass(String testClassName) throws Exception { + stopDevMode(); + + if (tempProj != null && tempProj.exists()) { + File messagesLog = new File(tempProj, "target/liberty/wlp/usr/servers/defaultServer/logs/messages.log"); + File copyMessagesLog = new File(testClassName + "_messages.log"); + FileUtils.copyFile(messagesLog, copyMessagesLog); + FileUtils.deleteDirectory(tempProj); + } + } + + protected static void stopDevMode() throws IOException, InterruptedException, FileNotFoundException { + markEndOfLogFile(); + + // shut down dev mode + if (writer != null) { + writer.write("exit"); // trigger dev mode to shut down + writer.flush(); + writer.close(); + + // test that dev mode has stopped running. + assertTrue(checkLogForMessage("CWWKE0036I")); + } + } + + protected static void testModifyJavaFile() throws IOException, InterruptedException { + // modify a java file + File srcHelloWorld = new File(tempProj, "src/main/java/com/demo/HelloWorld.java"); + File targetHelloWorld = new File(targetDir, "classes/com/demo/HelloWorld.class"); + assertTrue(srcHelloWorld.exists()); + assertTrue(targetHelloWorld.exists()); + + long lastModified = targetHelloWorld.lastModified(); + String str = "// testing"; + BufferedWriter javaWriter = new BufferedWriter(new FileWriter(srcHelloWorld, true)); + javaWriter.append(' '); + javaWriter.append(str); + + javaWriter.close(); + + Thread.sleep(5000); // wait for compilation + boolean wasModified = targetHelloWorld.lastModified() > lastModified; + assertTrue(wasModified); + } + + private static ProcessBuilder buildProcess(String processCommand) { + ProcessBuilder builder = new ProcessBuilder(); + builder.directory(tempProj); + + String os = System.getProperty("os.name"); + if (os != null && os.toLowerCase().startsWith("windows")) { + builder.command("CMD", "/C", processCommand); + } else { + builder.command("bash", "-c", processCommand); + } + return builder; + } + + private static void replaceVersion() throws IOException { + replaceString("RUNTIME_VERSION", "0.2.2-SNAPSHOT", pom); + } + + protected static void replaceString(String str, String replacement, File file) throws IOException { + Path path = file.toPath(); + Charset charset = StandardCharsets.UTF_8; + + String content = new String(Files.readAllBytes(path), charset); + + content = content.replaceAll(str, replacement); + Files.write(path, content.getBytes(charset)); + } + + protected static boolean checkLogForMessage(String message) throws InterruptedException, FileNotFoundException { + + boolean found = false; + + int timeout = 1; + while (!found && timeout <= 90) { + timeout++; + Thread.sleep(1000); + + found = findMessageInLog(message); + } + + return found; + } + + protected static void markEndOfLogFile() throws FileNotFoundException { + Scanner scanner = new Scanner(logFile); + + int lineCount = 0; + while (scanner.hasNextLine()) { + scanner.nextLine(); + lineCount++; + } + + logFileLineCount = lineCount; + } + + private static boolean findMessageInLog(String message) throws FileNotFoundException { + Scanner scanner = new Scanner(logFile); + + // Skip to last marked eof + skipLines(scanner); + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.contains(message)) { + return true; + } + } + return false; + } + + private static void skipLines(Scanner scanner) { + for (int i = 0; i < logFileLineCount; i++) { + scanner.nextLine(); + } + } +} diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevHotTestingTest.java b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevHotTestingTest.java new file mode 100644 index 00000000..3ed335b8 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevHotTestingTest.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * (c) Copyright IBM Corporation 2019. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package net.wasdev.wlp.test.dev.it; + +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DevHotTestingTest extends BaseDevTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + setUpBeforeClass("-DhotTests=true", "DevHotTestingTest"); + + // check that dev mode has fully started + assertTrue(checkLogForMessage("Tests will run automatically")); + + // verify that the target directory was created + targetDir = new File(tempProj, "target"); + assertTrue(targetDir.exists()); + } + + @AfterClass + public static void cleanUpAfterClass() throws Exception { + BaseDevTest.cleanUpAfterClass("DevHotTestingTest"); + } + + @Test + public void autoTestsInvocationTest() throws Exception { + + markEndOfLogFile(); + + testModifyJavaFile(); + + assertTrue(checkLogForMessage("Unit tests finished.")); + assertTrue(checkLogForMessage("Integration tests finished.")); + } + +} diff --git a/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevTest.java b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevTest.java new file mode 100644 index 00000000..f7bc06c8 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-liberty-dev-mode/src/test/java/net/wasdev/wlp/test/dev/it/DevTest.java @@ -0,0 +1,224 @@ +/******************************************************************************* + * (c) Copyright IBM Corporation 2019. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *******************************************************************************/ +package net.wasdev.wlp.test.dev.it; + +import static org.junit.Assert.*; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.nio.file.Files; +import java.util.Scanner; + +import org.apache.maven.shared.utils.io.FileUtils; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class DevTest extends BaseDevTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + setUpBeforeClass(null, "DevTest"); + + // check that dev mode has fully started + assertTrue(checkLogForMessage("Press the Enter key to run tests on demand.")); + + // verify that the target directory was created + targetDir = new File(tempProj, "target"); + assertTrue(targetDir.exists()); + } + + @AfterClass + public static void cleanUpAfterClass() throws Exception { + BaseDevTest.cleanUpAfterClass("DevTest"); + } + + /** + * Test that a basic java source file change is detected + * + * @throws Exception + */ + @Test + public void basicTest() throws Exception { + testModifyJavaFile(); + } + + /** + * Test that changes to boost properties in the pom.xml are detected + * + * @throws Exception + */ + @Test + public void changePortPropertyTest() throws Exception { + markEndOfLogFile(); + + File targetVariablesXML = new File(targetDir, + "/liberty/wlp/usr/servers/defaultServer/configDropins/defaults/variables.xml"); + assertTrue(targetVariablesXML.exists()); + + replaceString("9000", "9001", pom); + + // check for application started message + assertTrue(checkLogForMessage("CWWKT0016I")); + + boolean found = false; + Scanner scanner = new Scanner(targetVariablesXML); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.contains("")) { + found = true; + } + } + assertTrue("The updated boost_http_port variable was not found in the variables.xml file.", found); + } + + /** + * Test that resource files are properly detected when added + * + * @throws Exception + */ + @Test + public void resourceFileChangeTest() throws Exception { + markEndOfLogFile(); + + // make a resource file change + File resourceDir = new File(tempProj, "src/main/resources"); + assertTrue(resourceDir.exists()); + + File propertiesFile = new File(resourceDir, "microprofile-config.properties"); + assertTrue(propertiesFile.createNewFile()); + + Thread.sleep(2000); // wait for compilation + File targetPropertiesFile = new File(targetDir, "classes/microprofile-config.properties"); + assertTrue(targetPropertiesFile.exists()); + assertTrue(checkLogForMessage("CWWKZ0003I")); + + // delete a resource file + assertTrue(propertiesFile.delete()); + Thread.sleep(2000); + assertFalse(targetPropertiesFile.exists()); + } + + /** + * Test that test files are properly detected when created and modified + * + * @throws Exception + */ + @Test + public void testDirectoryTest() throws Exception { + // create the test directory + File testDir = new File(tempProj, "src/test/java"); + assertTrue(testDir.mkdirs()); + + // creates a java test file + File unitTestSrcFile = new File(testDir, "UnitTest.java"); + String unitTest = "import org.junit.Test;\n" + "import static org.junit.Assert.*;\n" + "\n" + + "public class UnitTest {\n" + "\n" + " @Test\n" + " public void testTrue() {\n" + + " assertTrue(true);\n" + "\n" + " }\n" + "}"; + Files.write(unitTestSrcFile.toPath(), unitTest.getBytes()); + assertTrue(unitTestSrcFile.exists()); + + Thread.sleep(2000); // wait for compilation + File unitTestTargetFile = new File(targetDir, "/test-classes/UnitTest.class"); + assertTrue(unitTestTargetFile.exists()); + long lastModified = unitTestTargetFile.lastModified(); + + // modify the test file + String str = "// testing"; + BufferedWriter javaWriter = new BufferedWriter(new FileWriter(unitTestSrcFile, true)); + javaWriter.append(' '); + javaWriter.append(str); + + javaWriter.close(); + + Thread.sleep(5000); // wait for compilation + assertTrue(unitTestTargetFile.lastModified() > lastModified); + + // delete the test file + assertTrue(unitTestSrcFile.delete()); + Thread.sleep(2000); + assertFalse(unitTestTargetFile.exists()); + + } + + /** + * Test to make sure tests are run when manually invoked + * + * @throws Exception + */ + @Test + public void manualTestsInvocationTest() throws Exception { + markEndOfLogFile(); + + writer.write("\n"); + writer.flush(); + + assertTrue(checkLogForMessage("Unit tests finished.")); + assertTrue(checkLogForMessage("Integration tests finished.")); + } + + /** + * Test to make sure comile failures are resolved when the correct booster + * is added. + * + * @throws Exception + */ + @Test + public void resolveDependencyTest() throws Exception { + markEndOfLogFile(); + + // create the HealthCheck class, expect a compilation error + File systemHealthRes = new File("../resources/SystemHealth.java"); + assertTrue(systemHealthRes.exists()); + File systemHealthSrc = new File(tempProj, "/src/main/java/com/demo/SystemHealth.java"); + File systemHealthTarget = new File(targetDir, "/classes/com/demo/SystemHealth.class"); + + FileUtils.copyFile(systemHealthRes, systemHealthSrc); + assertTrue(systemHealthSrc.exists()); + + assertTrue(checkLogForMessage("Source compilation had errors")); + assertFalse(systemHealthTarget.exists()); + + markEndOfLogFile(); + + // add mpHealth dependency to pom.xml + String mpHealthComment = ""; + String mpHealth = "\n" + " org.microshed.boost.boosters\n" + + " mp-health\n" + " "; + replaceString(mpHealthComment, mpHealth, pom); + + // Wait for boost:package to complete + assertTrue(checkLogForMessage("Server defaultServer package complete")); + + markEndOfLogFile(); + + String str = "// testing"; + BufferedWriter javaWriter = new BufferedWriter(new FileWriter(systemHealthSrc, true)); + javaWriter.append(' '); + javaWriter.append(str); + + javaWriter.close(); + + Thread.sleep(10000); // Wait until compilation is complete + + assertTrue(checkLogForMessage("Source compilation was successful.")); + assertTrue(systemHealthTarget.exists()); + + } + +} diff --git a/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/org/microshed/boost/runtimes/openliberty/LibertyRuntime.java b/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/org/microshed/boost/runtimes/openliberty/LibertyRuntime.java index c85076a3..5c4c3ca7 100644 --- a/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/org/microshed/boost/runtimes/openliberty/LibertyRuntime.java +++ b/boost-maven/boost-runtimes/runtime-openliberty/src/main/java/org/microshed/boost/runtimes/openliberty/LibertyRuntime.java @@ -20,6 +20,7 @@ import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin; import static org.twdata.maven.mojoexecutor.MojoExecutor.version; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -27,6 +28,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.microshed.boost.common.BoostException; @@ -61,6 +63,8 @@ public class LibertyRuntime implements RuntimeI { private String libertyMavenPluginArtifactId = "liberty-maven-plugin"; private String libertyMavenPluginVersion = "3.0.1"; + private Log logger; + public LibertyRuntime() { this.boosterConfigs = null; this.boostProperties = null; @@ -69,6 +73,7 @@ public LibertyRuntime() { this.projectBuildDir = null; this.libertyServerPath = null; this.mavenDepPlugin = null; + this.logger = null; } public LibertyRuntime(RuntimeParams runtimeParams) { @@ -79,6 +84,7 @@ public LibertyRuntime(RuntimeParams runtimeParams) { this.projectBuildDir = project.getBuild().getDirectory(); this.libertyServerPath = projectBuildDir + "/liberty/wlp/usr/servers/" + serverName; this.mavenDepPlugin = runtimeParams.getMavenDepPlugin(); + this.logger = runtimeParams.getLog(); } private Plugin getPlugin() throws MojoExecutionException { @@ -126,8 +132,8 @@ private void packageLiberty(List boosterConfigs) throws M } /** - * Get all booster dependencies and invoke the maven-dependency-plugin to copy - * them to the Liberty server. + * Get all booster dependencies and invoke the maven-dependency-plugin to + * copy them to the Liberty server. * * @throws MojoExecutionException * @@ -167,8 +173,9 @@ private void generateServerConfig(List boosterConfigs) th } /** - * Assumes a non-WAR packaging type (like JAR) has a WAR dependency. - * We assume there's only 1 but don't check, just return the first one. + * Assumes a non-WAR packaging type (like JAR) has a WAR dependency. We + * assume there's only 1 but don't check, just return the first one. + * * @return * @throws BoostException */ @@ -177,7 +184,7 @@ private String getWarName() throws BoostException { String retVal = null; if (project.getPackaging().equals(ConfigConstants.WAR_PKG_TYPE)) { retVal = project.getBuild().getFinalName(); - } else { + } else { // JAR package "release", get WAR from dependency for (Artifact artifact : project.getArtifacts()) { // first WAR @@ -193,7 +200,7 @@ private String getWarName() throws BoostException { throw new BoostException(msg); } } - + return retVal; } @@ -218,7 +225,7 @@ private void generateLibertyServerConfig(List boosterConf String httpsPort = (String) boostProperties.getOrDefault(BoostProperties.ENDPOINT_HTTPS_PORT, "9443"); libertyConfig.addHttpsPort(httpsPort); - + String warName = getWarName(); libertyConfig.addApplication(warName); @@ -261,13 +268,22 @@ private void installMissingFeatures() throws MojoExecutionException { */ private void installApp(String installAppPackagesVal) throws MojoExecutionException { - Element deployPackages = element(name("deployPackages"), installAppPackagesVal); - Element serverNameElement = element(name("serverName"), serverName); + // Since the app is installed as a loose app, there is no need to + // re-install if + // the server is already running. If we do, the app will be deployed to + // dropins + // which is not what we want. + if (!isServerRunning()) { + Element deployPackages = element(name("deployPackages"), installAppPackagesVal); + Element serverNameElement = element(name("serverName"), serverName); - Xpp3Dom configuration = configuration(deployPackages, serverNameElement, getRuntimeArtifactElement()); - configuration.addChild(element(name("appsDirectory"), "apps").toDom()); + Xpp3Dom configuration = configuration(deployPackages, serverNameElement, getRuntimeArtifactElement()); + configuration.addChild(element(name("appsDirectory"), "apps").toDom()); - executeMojo(getPlugin(), goal("deploy"), configuration, env); + executeMojo(getPlugin(), goal("deploy"), configuration, env); + } else { + logger.info("Server is running. Skipping app re-installation."); + } } private Element getRuntimeArtifactElement() { @@ -277,8 +293,8 @@ private Element getRuntimeArtifactElement() { } /** - * Invoke the liberty-maven-plugin to package the server into a runnable Liberty - * JAR + * Invoke the liberty-maven-plugin to package the server into a runnable + * Liberty JAR */ private void createUberJar() throws MojoExecutionException { executeMojo(getPlugin(), goal("package"), @@ -333,4 +349,8 @@ public void doStop() throws BoostException { } } + private boolean isServerRunning() { + return new File(libertyServerPath + "/workarea/.sRunning").exists(); + } + }