Skip to content

Commit

Permalink
Iterate to convergence
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed May 1, 2022
1 parent ffb1a34 commit a448f7d
Showing 1 changed file with 40 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,41 +179,52 @@ public void execute() throws MojoExecutionException {
applyOverrides(overrides, bundledPlugins, false, shadow, getLog());

if (useUpperBounds) {
/*
* Do upper bounds analysis. Upper bounds analysis consumes the model directly and
* not the resolution of that model, so it is fine to invoke it at this point with
* the model having been updated and the resolution having been cleared.
*/
DependencyNode node;
try {
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setProject(shadow);
ArtifactFilter filter = null; // Evaluate all scopes
node = dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter);
} catch (DependencyCollectorBuilderException e) {
throw new MojoExecutionException("Failed to analyze dependency tree for useUpperBounds", e);
}
RequireUpperBoundDepsVisitor visitor = new RequireUpperBoundDepsVisitor();
node.accept(visitor);
Map<String, String> upperBounds = visitor.upperBounds();
boolean converged = false;
int i = 0;

if (!upperBounds.isEmpty()) {
// Second pass: apply the results of the upper bounds analysis.
while (!converged) {
if (i++ > 10) {
throw new MojoExecutionException("Failed to iterate to convergence during upper bounds analysis");
}

/*
* applyOverrides depends on resolution, so resolve again between the first pass
* and the second.
* Do upper bounds analysis. Upper bounds analysis consumes the model directly and
* not the resolution of that model, so it is fine to invoke it at this point with
* the model having been updated and the resolution having been cleared.
*/
Set<Artifact> resolved = resolveDependencies(shadow);
shadow.setArtifacts(resolved);

// We intentionally want to use a lower version of this dependency.
String servletHack = upperBounds.get("javax.servlet:servlet-api");
if (servletHack != null && !servletHack.equals("0")) {
upperBounds.remove("javax.servlet:servlet-api");
DependencyNode node;
try {
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setProject(shadow);
ArtifactFilter filter = null; // Evaluate all scopes
node = dependencyCollectorBuilder.collectDependencyGraph(buildingRequest, filter);
} catch (DependencyCollectorBuilderException e) {
throw new MojoExecutionException("Failed to analyze dependency tree for useUpperBounds", e);
}
RequireUpperBoundDepsVisitor visitor = new RequireUpperBoundDepsVisitor();
node.accept(visitor);
Map<String, String> upperBounds = visitor.upperBounds();

if (upperBounds.isEmpty()) {
converged = true;
} else {
// Second pass: apply the results of the upper bounds analysis.

applyOverrides(upperBounds, Collections.emptyMap(), true, shadow, getLog());
/*
* applyOverrides depends on resolution, so resolve again between the first pass
* and the second.
*/
Set<Artifact> resolved = resolveDependencies(shadow);
shadow.setArtifacts(resolved);

// We intentionally want to use a lower version of this dependency.
String servletHack = upperBounds.get("javax.servlet:servlet-api");
if (servletHack != null && !servletHack.equals("0")) {
upperBounds.remove("javax.servlet:servlet-api");
}

applyOverrides(upperBounds, Collections.emptyMap(), true, shadow, getLog());
}
}
}

Expand Down

0 comments on commit a448f7d

Please sign in to comment.