Skip to content

Commit

Permalink
Handle umbrella dependency updates for multi module project build files
Browse files Browse the repository at this point in the history
Signed-off-by: Kathryn Kodama <[email protected]>
  • Loading branch information
kathrynkodama committed Mar 21, 2022
1 parent 1144b28 commit 84d08ee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -586,16 +586,21 @@ private boolean restartForLibertyMojoConfigChanged(Xpp3Dom config, Xpp3Dom oldCo
}

@Override
public boolean updateArtifactPaths(ProjectModule projectModule, boolean redeployCheck, ThreadPoolExecutor executor)
public boolean updateArtifactPaths(ProjectModule projectModule, boolean redeployCheck, boolean generateFeatures, ThreadPoolExecutor executor)
throws PluginExecutionException {
try {
MavenProject upstreamProject = getMavenProject(projectModule.getBuildFile());
File buildFile = projectModule.getBuildFile();
if (buildFile == null) {
buildFile = this.buildFile;
}
MavenProject upstreamProject = getMavenProject(buildFile);
MavenProject backupUpstreamProject = upstreamProject;
for (MavenProject p : upstreamMavenProjects) {
if (buildFile != null && p.getFile().getCanonicalPath().equals(buildFile.getCanonicalPath())) {
backupUpstreamProject = p;
}
}


// TODO rebuild the corresponding module if the compiler options have changed
JavaCompilerOptions oldCompilerOptions = getMavenCompilerOptions(backupUpstreamProject);
Expand Down Expand Up @@ -628,17 +633,26 @@ public boolean updateArtifactPaths(ProjectModule projectModule, boolean redeploy
updateArtifactPaths(projectModule.getBuildFile());
}

// check if compile dependencies have changed and redeploy if they have
// check if compile dependencies have changed, regenerate features and redeploy if they have
if (redeployCheck) {
// update upstream Maven projects list
int index = upstreamMavenProjects.indexOf(backupUpstreamProject);
upstreamMavenProjects.set(index, upstreamProject);

List<Dependency> deps = upstreamProject.getDependencies();
List<Dependency> oldDeps = backupUpstreamProject.getDependencies();
if (!dependencyListsEquals(deps,oldDeps)) {
if (!dependencyListsEquals(deps, oldDeps)) {
// detect compile dependency changes
if (!dependencyListsEquals(getCompileDependency(deps),getCompileDependency(oldDeps))) {
if (!dependencyListsEquals(getCompileDependency(deps), getCompileDependency(oldDeps))) {
// optimize generate features
if (generateFeatures) {
log.debug("Detected a change in the compile dependencies for "
+ buildFile + " , regenerating features");
boolean generateFeaturesSuccess = libertyGenerateFeatures(null, true);
if (generateFeaturesSuccess) {
util.getJavaSourceClassPaths().clear();
}
}
runLibertyMojoDeploy();
}
}
Expand Down Expand Up @@ -714,10 +728,10 @@ private void updateChildProjectArtifactPaths(File parentBuildFile, List<String>

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

@Override
protected void updateLooseApp() throws PluginExecutionException {
// Only perform operations if we are a war type application
Expand Down Expand Up @@ -805,7 +819,7 @@ public boolean recompileBuildFile(File buildFile, Set<String> compileArtifactPat
boolean installFeature = false;
boolean redeployApp = false;
boolean runBoostPackage = false;
boolean compileDependenciesChanged = false;
boolean optimizeGenerateFeatures = false;

ProjectBuildingResult build;
try {
Expand Down Expand Up @@ -864,6 +878,11 @@ public boolean recompileBuildFile(File buildFile, Set<String> compileArtifactPat
if (!Objects.equals(config, oldConfig)) {
redeployApp = true;
}
config = ExecuteMojoUtil.getPluginGoalConfig(libertyPlugin, "generate-features", log);
oldConfig = ExecuteMojoUtil.getPluginGoalConfig(backupLibertyPlugin, "generate-features", log);
if (!Objects.equals(config, oldConfig)) {
optimizeGenerateFeatures = true;
}

List<Dependency> deps = project.getDependencies();
List<Dependency> oldDeps = backupProject.getDependencies();
Expand All @@ -877,7 +896,7 @@ public boolean recompileBuildFile(File buildFile, Set<String> compileArtifactPat
// detect compile dependency changes
if (!dependencyListsEquals(getCompileDependency(deps), getCompileDependency(oldDeps))) {
redeployApp = true;
compileDependenciesChanged = true;
optimizeGenerateFeatures = true;
}
}
// update classpath for dependencies changes
Expand All @@ -895,7 +914,7 @@ public boolean recompileBuildFile(File buildFile, Set<String> compileArtifactPat
compileArtifactPaths.addAll(project.getCompileClasspathElements());
testArtifactPaths.addAll(project.getTestClasspathElements());

if (compileDependenciesChanged && generateFeatures) {
if (optimizeGenerateFeatures && generateFeatures) {
log.debug("Detected a change in the compile dependencies, re-generating features");
// always optimize generate features on dependency change
boolean generateFeaturesSuccess = libertyGenerateFeatures(null, true);
Expand All @@ -906,7 +925,6 @@ public boolean recompileBuildFile(File buildFile, Set<String> compileArtifactPat
}
}
if (restartServer) {
// TODO check if features are generated here
// - stop Server
// - create server or runBoostMojo
// - install feature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.apache.maven.project.ProjectBuildingResult;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -117,7 +119,21 @@ private void generateFeatures() throws MojoExecutionException, PluginExecutionEx
return;
} else {
// get all upstream projects
upstreamProjects.addAll(graph.getUpstreamProjects(project, true));
// upstreamProjects.addAll(graph.getUpstreamProjects(project, true));

// when GenerateFeaturesMojo is called from dev mode on a multi module project,
// the upstream project umbrella dependencies may not be up to date. Call
// getMavenProject to rebuild the project with the current Maven session,
// ensuring that the latest umbrella dependencies are loaded
for (MavenProject upstreamProj : graph.getUpstreamProjects(project, true)) {
try {
upstreamProjects.add(getMavenProject(upstreamProj.getFile()));
} catch (ProjectBuildingException e) {
log.debug("Could not resolve the upstream project: " + upstreamProj.getFile()
+ " using the current Maven session. Falling back to last resolved upstream project.");
upstreamProjects.add(upstreamProj); // fail gracefully, use last resolved project
}
}
}

if (containsPreviousLibertyModule(graph)) {
Expand Down Expand Up @@ -646,4 +662,11 @@ public boolean isDebugEnabled() {
}
}

// using the current MavenSession build the project (resolves dependencies)
private MavenProject getMavenProject(File buildFile) throws ProjectBuildingException {
ProjectBuildingResult build = mavenProjectBuilder.build(buildFile,
session.getProjectBuildingRequest().setResolveDependencies(true));
return build.getProject();
}

}

0 comments on commit 84d08ee

Please sign in to comment.