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 10, 2022
1 parent f60a555 commit b85ff89
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 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 @@ -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,13 +733,34 @@ 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
Expand Down Expand Up @@ -1173,13 +1196,10 @@ 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");
runCompileMojoLogWarning();
}
Expand Down Expand Up @@ -1395,6 +1415,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 +1448,8 @@ protected void doExecute() throws Exception {
}
}



/**
* Use the following priority ordering for skip test flags: <br>
* 1. within Liberty Maven plugin’s configuration in a module <br>
Expand Down Expand Up @@ -1849,6 +1873,7 @@ private void runCompileMojo(String goal, MavenProject mavenProject) throws MojoE
*/
private void runCompileMojoLogWarning() throws MojoExecutionException {
runCompileMojo("compile", project);
updateArtifactPathToOutputDirectory(project);
}

/**
Expand All @@ -1858,6 +1883,7 @@ private void runCompileMojoLogWarning() throws MojoExecutionException {
*/
private void runCompileMojoLogWarning(MavenProject mavenProject) throws MojoExecutionException {
runCompileMojo("compile", mavenProject);
updateArtifactPathToOutputDirectory(mavenProject);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package io.openliberty.tools.maven.server;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -388,4 +390,33 @@ private String getPluginConfiguration(MavenProject proj, String pluginGroupId, S
}
return null;
}

/**
* Call {@link #updateArtifactPathToOutputDirectory(MavenProject,Artifact) updateArtifactPathToOutputDirectory(MavenProject mavenProject, Artifact artifactToUpdate)}
* with <code>artifactToUpdate</code> obtained from <code>mavenProject</code>
*
* @param mavenProject
*/
protected void updateArtifactPathToOutputDirectory(MavenProject mavenProject) {
updateArtifactPathToOutputDirectory(mavenProject, mavenProject.getArtifact());
}

/**
* Call <code>artifactToUpdate.setFile()</code> to build output directory (i.e. ".../target/classes") and
* also creates the directory if not present. Together these will help avoid the core
* Maven artifact resolution trying to resolve the artifact against the local .m2,
* fitting better into dev mode, "all-in-one" use cases.
*
* @param mavenProject
* @param artifactToUpdate
*/
protected void updateArtifactPathToOutputDirectory(MavenProject mavenProject, Artifact artifactToUpdate) {
File outputDir = Paths.get(mavenProject.getBuild().getOutputDirectory()).toFile();
artifactToUpdate.setFile(outputDir);
try {
outputDir.createNewFile();
} catch(IOException ioe) {
log.warn("Failure creating output directory: " + outputDir, ioe);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.openliberty.tools.maven.server;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;

import org.apache.maven.execution.ProjectDependencyGraph;
Expand Down Expand Up @@ -82,12 +84,9 @@ 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) {
purgeLocalRepositoryArtifact();
}

runMojo("org.apache.maven.plugins", "maven-resources-plugin", "resources");
runMojo("org.apache.maven.plugins", "maven-compiler-plugin", "compile");
updateArtifactPathToOutputDirectory(project);
}

if (!looseApplication) {
Expand Down

0 comments on commit b85ff89

Please sign in to comment.