Skip to content

Commit

Permalink
Add target/classes File to upstream multi-module Artifact missing a F…
Browse files Browse the repository at this point in the history
…ile because compilation wasn't done

Signed-off-by: Scott Kurz <[email protected]>
  • Loading branch information
scottkurz committed Aug 4, 2022
1 parent 98c7615 commit e534881
Showing 1 changed file with 80 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -40,6 +41,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ProjectDependencyGraph;
Expand Down Expand Up @@ -68,9 +70,9 @@
import io.openliberty.tools.common.plugins.util.JavaCompilerOptions;
import io.openliberty.tools.common.plugins.util.PluginExecutionException;
import io.openliberty.tools.common.plugins.util.PluginScenarioException;
import io.openliberty.tools.common.plugins.util.ProjectModule;
import io.openliberty.tools.common.plugins.util.ServerFeatureUtil;
import io.openliberty.tools.common.plugins.util.ServerStatusUtil;
import io.openliberty.tools.common.plugins.util.ProjectModule;
import io.openliberty.tools.maven.BasicSupport;
import io.openliberty.tools.maven.applications.DeployMojoSupport;
import io.openliberty.tools.maven.applications.LooseWarApplication;
Expand Down Expand Up @@ -260,7 +262,7 @@ protected void setContainer(boolean container) {
}

protected List<File> getResourceDirectories(MavenProject project, File outputDir) {
// Let's just add resources directories unconditionally, the dev util already checks the directories actually exist
// Let's just add resources directories unconditionally, the dev util already checks the directories actually exist
// before adding them to the watch list. If we avoid checking here we allow for creating them later on.
List<File> resourceDirs = new ArrayList<File>();
for (Resource resource : project.getResources()) {
Expand Down Expand Up @@ -604,7 +606,7 @@ public boolean updateArtifactPaths(ProjectModule projectModule, boolean redeploy
backupUpstreamProject = p;
}
}


// TODO rebuild the corresponding module if the compiler options have changed
JavaCompilerOptions oldCompilerOptions = getMavenCompilerOptions(backupUpstreamProject);
Expand Down Expand Up @@ -731,48 +733,69 @@ private void updateChildProjectArtifactPaths(File parentBuildFile, List<String>
}
}
}

private MavenProject getMavenProject(File buildFile) throws ProjectBuildingException {
ProjectBuildingResult build = mavenProjectBuilder.build(buildFile,
session.getProjectBuildingRequest().setResolveDependencies(true));
return build.getProject();
MavenProject builtProject = build.getProject();
updateUpstreamProjectsArtifactPathToOutputDirectory(builtProject);
return builtProject;
}

/**
* From the project we're running dev mode from, get the artifact representing each of the upstream modules
* and make sure the artifact File is associated to the build output (target/classes, not the .m2 repo).
*
* Because of the way we dynamically build new model objects we need to do this from a given model's perspective.
*
* @param startingProject
*/
private void updateUpstreamProjectsArtifactPathToOutputDirectory(MavenProject startingProject){
Map<String,Artifact> artifactMap = startingProject.getArtifactMap();
for (MavenProject p : upstreamMavenProjects) {
Artifact projArtifact = artifactMap.get(p.getGroupId() + ":" + p.getArtifactId());
if (projArtifact != null) {
File outputDir = Paths.get(p.getBuild().getOutputDirectory()).toFile();
projArtifact.setFile(outputDir);
}
}
}

@Override
protected void updateLooseApp() throws PluginExecutionException {
// Only perform operations if we are a war type application
if (project.getPackaging().equals("war")) {
// Check if we are using an exploded loose app
if (LooseWarApplication.isExploded(project)) {
if (!isExplodedLooseWarApp) {
// The project was previously running with a "non-exploded" loose app.
// Update this flag and redeploy as an exploded loose app.
isExplodedLooseWarApp = true;
// Validate maven-war-plugin version
Plugin warPlugin = getPlugin("org.apache.maven.plugins", "maven-war-plugin");
if (!validatePluginVersion(warPlugin.getVersion(), "3.3.1")) {
log.warn("Exploded WAR functionality is enabled. Please use maven-war-plugin version 3.3.1 or greater for best results.");
}
redeployApp();
} else {
try {
runExplodedMojo();
} catch (MojoExecutionException e) {
log.error("Failed to run war:exploded goal", e);
}
}
} else {
if (isExplodedLooseWarApp) {
// Dev mode was previously running with an exploded loose war app. The app
// must have been updated to remove any exploded war capabilities
// (filtering, overlay, etc). Update this flag and redeploy.
isExplodedLooseWarApp = false;
redeployApp();
}
}
}
// Only perform operations if we are a war type application
if (project.getPackaging().equals("war")) {
// Check if we are using an exploded loose app
if (LooseWarApplication.isExploded(project)) {
if (!isExplodedLooseWarApp) {
// The project was previously running with a "non-exploded" loose app.
// Update this flag and redeploy as an exploded loose app.
isExplodedLooseWarApp = true;
// Validate maven-war-plugin version
Plugin warPlugin = getPlugin("org.apache.maven.plugins", "maven-war-plugin");
if (!validatePluginVersion(warPlugin.getVersion(), "3.3.1")) {
log.warn("Exploded WAR functionality is enabled. Please use maven-war-plugin version 3.3.1 or greater for best results.");
}
redeployApp();
} else {
try {
runExplodedMojo();
} catch (MojoExecutionException e) {
log.error("Failed to run war:exploded goal", e);
}
}
} else {
if (isExplodedLooseWarApp) {
// Dev mode was previously running with an exploded loose war app. The app
// must have been updated to remove any exploded war capabilities
// (filtering, overlay, etc). Update this flag and redeploy.
isExplodedLooseWarApp = false;
redeployApp();
}
}
}
}

@Override
Expand All @@ -789,7 +812,7 @@ protected void resourceDirectoryCreated() throws IOException {

@Override
protected void resourceModifiedOrCreated(File fileChanged, File resourceParent, File outputDirectory) throws IOException {
if (project.getPackaging().equals("war") && LooseWarApplication.isExploded(project)) {
if (project.getPackaging().equals("war") && LooseWarApplication.isExploded(project)) {
try {
runMojo("org.apache.maven.plugins", "maven-resources-plugin", "resources");
runExplodedMojo();
Expand Down Expand Up @@ -1173,14 +1196,12 @@ protected void doExecute() throws Exception {
if (isEar) {
runMojo("org.apache.maven.plugins", "maven-ear-plugin", "generate-application-xml");
runMojo("org.apache.maven.plugins", "maven-resources-plugin", "resources");

installEmptyEarIfNotFound(project);
} else if (project.getPackaging().equals("pom")) {
log.debug("Skipping compile/resources on module with pom packaging type");
} else {
purgeLocalRepositoryArtifact();

runMojo("org.apache.maven.plugins", "maven-resources-plugin", "resources");
updateArtifactPathToOutputDirectory(project);
runCompileMojoLogWarning();
}
return;
Expand Down Expand Up @@ -1320,10 +1341,10 @@ protected void doExecute() throws Exception {

// Validate maven-war-plugin version
if (isExplodedLooseWarApp) {
Plugin warPlugin = getPlugin("org.apache.maven.plugins", "maven-war-plugin");
if (!validatePluginVersion(warPlugin.getVersion(), "3.3.2")) {
log.warn("Exploded WAR functionality is enabled. Please use maven-war-plugin version 3.3.2 or greater for best results.");
}
Plugin warPlugin = getPlugin("org.apache.maven.plugins", "maven-war-plugin");
if (!validatePluginVersion(warPlugin.getVersion(), "3.3.2")) {
log.warn("Exploded WAR functionality is enabled. Please use maven-war-plugin version 3.3.2 or greater for best results.");
}
}
}

Expand Down Expand Up @@ -1395,6 +1416,8 @@ protected void doExecute() throws Exception {
// pom.xml
File pom = project.getFile();

updateArtifactPathToOutputDirectory(project);

// collect artifacts canonical paths in order to build classpath
Set<String> compileArtifactPaths = new HashSet<String>(project.getCompileClasspathElements());
Set<String> testArtifactPaths = new HashSet<String>(project.getTestClasspathElements());
Expand Down Expand Up @@ -1426,6 +1449,17 @@ protected void doExecute() throws Exception {
}
}

/**
* Call Artifact.setFile() to build output directory (i.e. ".../target/classes")
* to ensure the local .m2 repository location is not used.
*
* @param project
*/
private static void updateArtifactPathToOutputDirectory(MavenProject mavenProject) {
File outputDir = Paths.get(mavenProject.getBuild().getOutputDirectory()).toFile();
mavenProject.getArtifact().setFile(outputDir);
}

/**
* Use the following priority ordering for skip test flags: <br>
* 1. within Liberty Maven plugin’s configuration in a module <br>
Expand Down

0 comments on commit e534881

Please sign in to comment.