slachiewicz commented on code in PR #761:
URL:
https://github.com/apache/maven-invoker-plugin/pull/761#discussion_r4055086924
##########
src/main/java/org/apache/maven/plugins/invoker/InstallMojo.java:
##########
@@ -211,6 +219,121 @@ private void resolveProjectPoms(MavenProject project,
Map<String, Artifact> reso
resolveProjectPoms(project.getParent(), resolvedArtifacts);
}
+ /**
+ * Resolve the BOMs imported by the project and its parents.
+ * <p>
+ * Imported BOMs are flattened into the effective model, so they are
invisible to the dependency resolution and
+ * would be missing from the local repository used by the integration
tests. Only the original models still carry
+ * the import declarations, hence the walk over the original models of the
project and of its parents.
+ */
+ private void resolveImportedBoms(Map<String, Artifact> resolvedArtifacts)
+ throws ArtifactResolutionException, MojoExecutionException {
+
+ Set<String> visitedBoms = new HashSet<>();
+ for (MavenProject currentProject = project;
+ currentProject != null;
+ currentProject = currentProject.getParent()) {
+ Model originalModel = currentProject.getOriginalModel();
+ if (originalModel == null) {
+ continue;
+ }
+ resolveImportedBoms(
+ originalModel.getDependencyManagement(),
+ createInterpolator(currentProject.getModel()),
+ currentProject.getRemoteProjectRepositories(),
+ resolvedArtifacts,
+ visitedBoms);
+ }
+ }
+
+ private void resolveImportedBoms(
+ DependencyManagement dependencyManagement,
+ Interpolator interpolator,
+ List<RemoteRepository> remoteRepositories,
+ Map<String, Artifact> resolvedArtifacts,
+ Set<String> visitedBoms)
+ throws ArtifactResolutionException, MojoExecutionException {
+
+ if (dependencyManagement == null) {
+ return;
+ }
+
+ for (org.apache.maven.model.Dependency dependency :
dependencyManagement.getDependencies()) {
+ if (!"pom".equals(dependency.getType()) ||
!"import".equals(dependency.getScope())) {
+ continue;
+ }
+
+ String groupId = interpolate(interpolator,
dependency.getGroupId());
+ String artifactId = interpolate(interpolator,
dependency.getArtifactId());
+ String version = interpolate(interpolator,
dependency.getVersion());
+ String bomId = groupId + ":" + artifactId + ":" + version;
+
+ if (bomId.contains("${")) {
+ getLog().warn("Skipping imported BOM with unresolvable
coordinates: " + bomId);
+ continue;
+ }
+
+ if (!visitedBoms.add(bomId)) {
+ continue;
+ }
+
+ Artifact bomArtifact;
+ try {
+ bomArtifact = resolveArtifact(
+ new DefaultArtifact(groupId, artifactId, "", "pom",
version), remoteRepositories);
+ } catch (ArtifactResolutionException e) {
+ throw new MojoExecutionException("Failed to resolve imported
BOM: " + bomId, e);
+ }
+
+ getLog().debug("Resolved imported BOM " + bomId + " to " +
bomArtifact.getFile());
+ resolvePomWithParents(bomArtifact, resolvedArtifacts,
remoteRepositories);
+
+ // a BOM can import other BOMs in turn
+ Model bomModel = PomUtils.loadPom(bomArtifact.getFile());
+ resolveImportedBoms(
+ bomModel.getDependencyManagement(),
+ createInterpolator(inheritCoordinates(bomModel)),
+ remoteRepositories,
+ resolvedArtifacts,
+ visitedBoms);
+ }
+ }
+
+ /**
+ * Complete the coordinates a raw model inherits from its parent, so that
<code>${project.version}</code> and
+ * <code>${project.groupId}</code> can be interpolated.
+ */
+ private Model inheritCoordinates(Model model) {
+ Parent parent = model.getParent();
+ if (parent != null) {
+ if (model.getGroupId() == null) {
+ model.setGroupId(parent.getGroupId());
+ }
+ if (model.getVersion() == null) {
+ model.setVersion(parent.getVersion());
+ }
+ }
+ return model;
+ }
+
+ private Interpolator createInterpolator(Model model) {
Review Comment:
Core's `ModelInterpolator` runs after inheritance assembly, so on the raw
model it cannot see parent-defined properties — the
raw-declarations-plus-effective-values pairing here is the same order core
uses. For BOMs that import BOMs I switched to `ProjectBuilder` in 74eb3f1,
which drops the hand-rolled `inheritCoordinates` and resolves inherited
properties; the IT now covers that case.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]