|
32 | 32 | import org.apache.maven.api.model.Dependency;
|
33 | 33 | import org.apache.maven.api.model.DependencyManagement;
|
34 | 34 | import org.apache.maven.api.model.Exclusion;
|
| 35 | +import org.apache.maven.api.model.InputLocation; |
| 36 | +import org.apache.maven.api.model.InputSource; |
35 | 37 | import org.apache.maven.api.model.Model;
|
36 | 38 | import org.apache.maven.api.services.BuilderProblem.Severity;
|
37 | 39 | import org.apache.maven.api.services.ModelBuilderRequest;
|
@@ -81,6 +83,10 @@ public Model importManagement(
|
81 | 83 | + toString(present) + ". Add the conflicting managed dependency directly "
|
82 | 84 | + "to the dependencyManagement section of the POM.");
|
83 | 85 | }
|
| 86 | + if (present == null && request.isLocationTracking()) { |
| 87 | + Dependency updatedDependency = updateWithImportedFrom(dependency, source); |
| 88 | + dependencies.put(key, updatedDependency); |
| 89 | + } |
84 | 90 | }
|
85 | 91 | }
|
86 | 92 |
|
@@ -144,4 +150,43 @@ private boolean equals(Exclusion e1, Exclusion e2) {
|
144 | 150 | return Objects.equals(e1.getGroupId(), e2.getGroupId())
|
145 | 151 | && Objects.equals(e1.getArtifactId(), e2.getArtifactId());
|
146 | 152 | }
|
| 153 | + |
| 154 | + static Dependency updateWithImportedFrom(Dependency dependency, DependencyManagement bom) { |
| 155 | + // We are only interested in the InputSource, so the location of the <dependency> element is sufficient |
| 156 | + InputLocation dependencyLocation = dependency.getLocation(""); |
| 157 | + InputLocation bomLocation = bom.getLocation(""); |
| 158 | + |
| 159 | + if (dependencyLocation == null || bomLocation == null) { |
| 160 | + return dependency; |
| 161 | + } |
| 162 | + |
| 163 | + InputSource dependencySource = dependencyLocation.getSource(); |
| 164 | + InputSource bomSource = bomLocation.getSource(); |
| 165 | + |
| 166 | + // If the dependency and BOM have the same source, it means we found the root where the dependency is declared. |
| 167 | + if (dependencySource == null |
| 168 | + || bomSource == null |
| 169 | + || Objects.equals(dependencySource.getModelId(), bomSource.getModelId())) { |
| 170 | + return Dependency.newBuilder(dependency, true) |
| 171 | + .importedFrom(bomLocation) |
| 172 | + .build(); |
| 173 | + } |
| 174 | + |
| 175 | + while (dependencySource.getImportedFrom() != null) { |
| 176 | + InputLocation importedFrom = dependencySource.getImportedFrom(); |
| 177 | + |
| 178 | + // Stop if the BOM is already in the list, no update necessary |
| 179 | + if (Objects.equals(importedFrom.getSource().getModelId(), bomSource.getModelId())) { |
| 180 | + return dependency; |
| 181 | + } |
| 182 | + |
| 183 | + dependencySource = importedFrom.getSource(); |
| 184 | + } |
| 185 | + |
| 186 | + // We modify the input location that is used for the whole file. |
| 187 | + // This is likely correct because the POM hierarchy applies to the whole POM, not just one dependency. |
| 188 | + return Dependency.newBuilder(dependency, true) |
| 189 | + .importedFrom(new InputLocation(bomLocation)) |
| 190 | + .build(); |
| 191 | + } |
147 | 192 | }
|
0 commit comments