Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge locally installed upstream modules when using loose app #1207

Merged
merged 4 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -127,6 +127,10 @@ protected static void startProcess(String params, boolean isDevMode) throws IOEx
}

protected static void startProcess(String params, boolean isDevMode, String mavenPluginCommand) throws IOException, InterruptedException, FileNotFoundException {
startProcess(params, isDevMode, mavenPluginCommand, true);
}

protected static void startProcess(String params, boolean isDevMode, String mavenPluginCommand, boolean verifyServerStart) throws IOException, InterruptedException, FileNotFoundException {
// run dev mode on project
String goal;
if(isDevMode) {
Expand All @@ -153,20 +157,22 @@ protected static void startProcess(String params, boolean isDevMode, String mave

writer = new BufferedWriter(new OutputStreamWriter(stdin));

// check that the server has started
Thread.sleep(5000);
assertTrue(getLogTail(), verifyLogMessageExists("CWWKF0011I", 120000));
if (isDevMode) {
assertTrue(verifyLogMessageExists("Liberty is running in dev mode.", 60000));
}
if (verifyServerStart) {
// check that the server has started
Thread.sleep(5000);
assertTrue(getLogTail(), verifyLogMessageExists("CWWKF0011I", 120000));
if (isDevMode) {
assertTrue(verifyLogMessageExists("Liberty is running in dev mode.", 60000));
}

// verify that the target directory was created
if (customLibertyModule == null) {
targetDir = new File(tempProj, "target");
} else {
targetDir = new File(new File(tempProj, customLibertyModule), "target");
// verify that the target directory was created
if (customLibertyModule == null) {
targetDir = new File(tempProj, "target");
} else {
targetDir = new File(new File(tempProj, customLibertyModule), "target");
}
assertTrue(targetDir.exists());
}
assertTrue(targetDir.exists());
}

protected static String getLogTail() throws IOException {
Expand Down Expand Up @@ -207,7 +213,11 @@ protected static void cleanUpAfterClass() throws Exception {
}

protected static void cleanUpAfterClass(boolean isDevMode) throws Exception {
stopProcess(isDevMode);
cleanUpAfterClass(isDevMode, true);
}

protected static void cleanUpAfterClass(boolean isDevMode, boolean checkForShutdownMessage) throws Exception {
stopProcess(isDevMode, checkForShutdownMessage);

if (tempProj != null && tempProj.exists()) {
FileUtils.deleteDirectory(tempProj);
Expand All @@ -225,7 +235,7 @@ protected static void clearLogFile() throws Exception {
}
}

private static void stopProcess(boolean isDevMode) throws IOException, InterruptedException, FileNotFoundException, IllegalThreadStateException {
private static void stopProcess(boolean isDevMode, boolean checkForShutdownMessage) throws IOException, InterruptedException, FileNotFoundException, IllegalThreadStateException {
// shut down dev mode
if (writer != null) {
if(isDevMode) {
Expand All @@ -240,8 +250,10 @@ private static void stopProcess(boolean isDevMode) throws IOException, Interrupt
process.waitFor(120, TimeUnit.SECONDS);
process.exitValue();

// test that dev mode has stopped running
assertTrue(verifyLogMessageExists("CWWKE0036I", 20000));
// test that the server has shut down
if (checkForShutdownMessage) {
assertTrue(verifyLogMessageExists("CWWKE0036I", 20000));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ protected static File getTargetFileForModule(String srcFilePath, String targetFi
return targetClass;
}

protected static void runCommand(String commandLineString) throws Exception {
StringBuilder command = new StringBuilder(commandLineString);
ProcessBuilder builder = buildProcess(command.toString());

builder.redirectOutput(logFile);
builder.redirectError(logFile);
if (customPomModule != null) {
builder.directory(new File(tempProj, customPomModule));
}
Process process = builder.start();
assertTrue(process.isAlive());
process.waitFor(120, TimeUnit.SECONDS);
assertEquals(0, process.exitValue());
}

protected static void modifyFileForModule(String srcFilePath, String str, String replacement) throws IOException {
File srcClass = new File(tempProj, srcFilePath);
assertTrue(srcClass.exists());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
* (c) Copyright IBM Corporation 2021.
*
* 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.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.maven.shared.utils.io.FileUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class MultiModuleM2InstalledTest extends BaseMultiModuleTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
setUpMultiModule("typeA", "ear", null);
}

@Test
public void purgeLocallyInstalledModule_DevMode_Test() throws Exception {

// install everything to m2
runCommand("mvn install");

// ensure class was compiled
File targetClass = new File(tempProj, "jar/target/classes/io/openliberty/guides/multimodules/lib/Converter.class");
assertTrue(targetClass.exists());

// delete the source file
File srcClass = new File(tempProj, "jar/src/main/java/io/openliberty/guides/multimodules/lib/Converter.java");
assertTrue(srcClass.delete());

// clean targets
runCommand("mvn clean");

// dev mode should purge the jar module from m2, so that the war module will show a failure due to the missing dependency.
// i.e. it should not find the jar dependency from m2
startProcess(null, true, "mvn io.openliberty.tools:liberty-maven-plugin:"+System.getProperty("mavenPluginVersion")+":", false);

// TODO when https://github.com/OpenLiberty/ci.maven/issues/1203 is fixed, check for compilation error instead
assertTrue(getLogTail(logFile), verifyLogMessageExists("Could not resolve dependencies for project", 5000));

assertFalse(getLogTail(logFile), targetClass.exists());
}

@AfterClass
public static void cleanUpAfterClass() throws Exception {
// use a run-style cleanup (e.g. kill the process), and skip checking for server
// shutdown since dev mode should not have fully started
BaseDevTest.cleanUpAfterClass(false, false);
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
* (c) Copyright IBM Corporation 2021.
*
* 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.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.maven.shared.utils.io.FileUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class MultiModuleRunM2InstalledTest extends BaseMultiModuleTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
setUpMultiModule("typeA", "ear", null);
}

@Test
public void purgeLocallyInstalledModule_LibertyRun_Test() throws Exception {

// install everything to m2
runCommand("mvn install");

// ensure class was compiled
File targetClass = new File(tempProj, "jar/target/classes/io/openliberty/guides/multimodules/lib/Converter.class");
assertTrue(targetClass.exists());

// delete the source file
File srcClass = new File(tempProj, "jar/src/main/java/io/openliberty/guides/multimodules/lib/Converter.java");
assertTrue(srcClass.delete());

// clean targets
runCommand("mvn clean");

// run goal should purge the jar module from m2, so that the war module will show a failure due to the missing dependency.
// i.e. it should not find the jar dependency from m2
startProcess(null, false, "mvn io.openliberty.tools:liberty-maven-plugin:"+System.getProperty("mavenPluginVersion")+":", false);

// TODO when https://github.com/OpenLiberty/ci.maven/issues/1203 is fixed, check for compilation error instead
assertTrue(getLogTail(logFile), verifyLogMessageExists("Could not resolve dependencies for project", 5000));

assertFalse(getLogTail(logFile), targetClass.exists());
}

@AfterClass
public static void cleanUpAfterClass() throws Exception {
// use a run-style cleanup (e.g. kill the process), and skip checking for server
// shutdown since server should not have fully started
BaseDevTest.cleanUpAfterClass(false, false);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,8 @@ protected void doExecute() throws Exception {
} else if (project.getPackaging().equals("pom")) {
log.debug("Skipping compile/resources on module with pom packaging type");
} else {
purgeLocalRepositoryModule(project);

runMojo("org.apache.maven.plugins", "maven-resources-plugin", "resources");
runCompileMojoLogWarning();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ protected void doExecute() throws Exception {
} else if (projectPackaging.equals("pom")) {
log.debug("Skipping compile/resources on module with pom packaging type");
} else {
if (hasDownstreamProjects && looseApplication) {
purgeLocalRepositoryModule(project);
}

runMojo("org.apache.maven.plugins", "maven-resources-plugin", "resources");
runMojo("org.apache.maven.plugins", "maven-compiler-plugin", "compile");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,20 @@ private void installEmptyEAR(MavenProject earProject) throws MojoExecutionExcept
}
}

protected void purgeLocalRepositoryModule(MavenProject project) throws MojoExecutionException {
Plugin plugin = getPlugin("org.apache.maven.plugins", "maven-dependency-plugin");
ericglau marked this conversation as resolved.
Show resolved Hide resolved
String goal = "purge-local-repository";
Xpp3Dom config = ExecuteMojoUtil.getPluginGoalConfig(plugin, goal, log);
config = Xpp3Dom.mergeXpp3Dom(configuration(
element(name("reResolve"), "false"),
element(name("actTransitively"), "false"),
element(name("manualIncludes"), project.getGroupId()+":"+project.getArtifactId())
), config);
log.info("Running maven-dependency-plugin:" + goal);
log.debug("configuration:\n" + config);
executeMojo(plugin, goal(goal), config, executionEnvironment(project, session, pluginManager));
ericglau marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Returns whether potentialTopModule is a multi module project that has potentialSubModule as one of its sub-modules.
*/
Expand Down