Skip to content

Commit 78b912c

Browse files
committed
Move code to new implementation
1 parent b27c457 commit 78b912c

File tree

6 files changed

+57
-49
lines changed

6 files changed

+57
-49
lines changed

maven-api-impl/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ under the License.
123123
<artifactId>hamcrest</artifactId>
124124
<scope>test</scope>
125125
</dependency>
126+
<dependency>
127+
<groupId>org.assertj</groupId>
128+
<artifactId>assertj-core</artifactId>
129+
</dependency>
126130
<dependency>
127131
<groupId>org.apache.maven</groupId>
128132
<artifactId>maven-di</artifactId>

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultDependencyManagementImporter.java

+45
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.apache.maven.api.model.Dependency;
3333
import org.apache.maven.api.model.DependencyManagement;
3434
import org.apache.maven.api.model.Exclusion;
35+
import org.apache.maven.api.model.InputLocation;
36+
import org.apache.maven.api.model.InputSource;
3537
import org.apache.maven.api.model.Model;
3638
import org.apache.maven.api.services.BuilderProblem.Severity;
3739
import org.apache.maven.api.services.ModelBuilderRequest;
@@ -81,6 +83,10 @@ public Model importManagement(
8183
+ toString(present) + ". Add the conflicting managed dependency directly "
8284
+ "to the dependencyManagement section of the POM.");
8385
}
86+
if (present == null && request.isLocationTracking()) {
87+
Dependency updatedDependency = updateWithImportedFrom(dependency, source);
88+
dependencies.put(key, updatedDependency);
89+
}
8490
}
8591
}
8692

@@ -144,4 +150,43 @@ private boolean equals(Exclusion e1, Exclusion e2) {
144150
return Objects.equals(e1.getGroupId(), e2.getGroupId())
145151
&& Objects.equals(e1.getArtifactId(), e2.getArtifactId());
146152
}
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+
}
147192
}

maven-model-builder/src/test/java/org/apache/maven/model/composition/DefaultDependencyManagementImporterTest.java maven-api-impl/src/test/java/org/apache/maven/internal/impl/model/DefaultDependencyManagementImporterTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
package org.apache.maven.model.composition;
19+
package org.apache.maven.internal.impl.model;
2020

2121
import org.apache.maven.api.model.Dependency;
2222
import org.apache.maven.api.model.DependencyManagement;

maven-model-builder/src/main/java/org/apache/maven/model/composition/DefaultDependencyManagementImporter.java

-46
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.apache.maven.api.model.Dependency;
2727
import org.apache.maven.api.model.DependencyManagement;
2828
import org.apache.maven.api.model.Exclusion;
29-
import org.apache.maven.api.model.InputLocation;
30-
import org.apache.maven.api.model.InputSource;
3129
import org.apache.maven.api.model.Model;
3230
import org.apache.maven.model.building.ModelBuildingRequest;
3331
import org.apache.maven.model.building.ModelProblem;
@@ -75,11 +73,6 @@ public Model importManagement(
7573
+ toString(present) + ". Add the conflicting managed dependency directly "
7674
+ "to the dependencyManagement section of the POM."));
7775
}
78-
79-
if (present == null && request.isLocationTracking()) {
80-
Dependency updatedDependency = updateWithImportedFrom(dependency, source);
81-
dependencies.put(key, updatedDependency);
82-
}
8376
}
8477
}
8578

@@ -143,43 +136,4 @@ private boolean equals(Exclusion e1, Exclusion e2) {
143136
return Objects.equals(e1.getGroupId(), e2.getGroupId())
144137
&& Objects.equals(e1.getArtifactId(), e2.getArtifactId());
145138
}
146-
147-
static Dependency updateWithImportedFrom(Dependency dependency, DependencyManagement bom) {
148-
// We are only interested in the InputSource, so the location of the <dependency> element is sufficient
149-
InputLocation dependencyLocation = dependency.getLocation("");
150-
InputLocation bomLocation = bom.getLocation("");
151-
152-
if (dependencyLocation == null || bomLocation == null) {
153-
return dependency;
154-
}
155-
156-
InputSource dependencySource = dependencyLocation.getSource();
157-
InputSource bomSource = bomLocation.getSource();
158-
159-
// If the dependency and BOM have the same source, it means we found the root where the dependency is declared.
160-
if (dependencySource == null
161-
|| bomSource == null
162-
|| Objects.equals(dependencySource.getModelId(), bomSource.getModelId())) {
163-
return Dependency.newBuilder(dependency, true)
164-
.importedFrom(bomLocation)
165-
.build();
166-
}
167-
168-
while (dependencySource.getImportedFrom() != null) {
169-
InputLocation importedFrom = dependencySource.getImportedFrom();
170-
171-
// Stop if the BOM is already in the list, no update necessary
172-
if (Objects.equals(importedFrom.getSource().getModelId(), bomSource.getModelId())) {
173-
return dependency;
174-
}
175-
176-
dependencySource = importedFrom.getSource();
177-
}
178-
179-
// We modify the input location that is used for the whole file.
180-
// This is likely correct because the POM hierarchy applies to the whole POM, not just one dependency.
181-
return Dependency.newBuilder(dependency, true)
182-
.importedFrom(new InputLocation(bomLocation))
183-
.build();
184-
}
185139
}

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ under the License.
161161
<maven.site.path>ref/4-LATEST</maven.site.path>
162162
<project.build.outputTimestamp>2024-05-22T14:07:09Z</project.build.outputTimestamp>
163163
<!-- various versions -->
164+
<assertjVersion>3.26.0</assertjVersion>
164165
<asmVersion>9.7</asmVersion>
165166
<byteBuddyVersion>1.14.18</byteBuddyVersion>
166167
<cipherVersion>2.0</cipherVersion>
@@ -606,6 +607,11 @@ under the License.
606607
<version>${hamcrestVersion}</version>
607608
<scope>test</scope>
608609
</dependency>
610+
<dependency>
611+
<groupId>org.assertj</groupId>
612+
<artifactId>assertj-core</artifactId>
613+
<version>${assertjVersion}</version>
614+
</dependency>
609615
<dependency>
610616
<groupId>org.codehaus.plexus</groupId>
611617
<artifactId>plexus-testing</artifactId>

src/mdo/model.vm

+1-2
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ public class ${class.name}
485485
}
486486

487487
@Nonnull
488-
public Builder importedFrom( InputLocation importedFrom )
489-
{
488+
public Builder importedFrom(InputLocation importedFrom) {
490489
this.importedFrom = importedFrom;
491490
return this;
492491
}

0 commit comments

Comments
 (0)