Skip to content

Commit

Permalink
Move code to new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Jul 9, 2024
1 parent b27c457 commit 78b912c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 49 deletions.
4 changes: 4 additions & 0 deletions maven-api-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ under the License.
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-di</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.DependencyManagement;
import org.apache.maven.api.model.Exclusion;
import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.InputSource;
import org.apache.maven.api.model.Model;
import org.apache.maven.api.services.BuilderProblem.Severity;
import org.apache.maven.api.services.ModelBuilderRequest;
Expand Down Expand Up @@ -81,6 +83,10 @@ public Model importManagement(
+ toString(present) + ". Add the conflicting managed dependency directly "
+ "to the dependencyManagement section of the POM.");
}
if (present == null && request.isLocationTracking()) {
Dependency updatedDependency = updateWithImportedFrom(dependency, source);
dependencies.put(key, updatedDependency);
}
}
}

Expand Down Expand Up @@ -144,4 +150,43 @@ private boolean equals(Exclusion e1, Exclusion e2) {
return Objects.equals(e1.getGroupId(), e2.getGroupId())
&& Objects.equals(e1.getArtifactId(), e2.getArtifactId());
}

static Dependency updateWithImportedFrom(Dependency dependency, DependencyManagement bom) {
// We are only interested in the InputSource, so the location of the <dependency> element is sufficient
InputLocation dependencyLocation = dependency.getLocation("");
InputLocation bomLocation = bom.getLocation("");

if (dependencyLocation == null || bomLocation == null) {
return dependency;
}

InputSource dependencySource = dependencyLocation.getSource();
InputSource bomSource = bomLocation.getSource();

// If the dependency and BOM have the same source, it means we found the root where the dependency is declared.
if (dependencySource == null
|| bomSource == null
|| Objects.equals(dependencySource.getModelId(), bomSource.getModelId())) {
return Dependency.newBuilder(dependency, true)
.importedFrom(bomLocation)
.build();
}

while (dependencySource.getImportedFrom() != null) {
InputLocation importedFrom = dependencySource.getImportedFrom();

// Stop if the BOM is already in the list, no update necessary
if (Objects.equals(importedFrom.getSource().getModelId(), bomSource.getModelId())) {
return dependency;
}

dependencySource = importedFrom.getSource();
}

// We modify the input location that is used for the whole file.
// This is likely correct because the POM hierarchy applies to the whole POM, not just one dependency.
return Dependency.newBuilder(dependency, true)
.importedFrom(new InputLocation(bomLocation))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.model.composition;
package org.apache.maven.internal.impl.model;

import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.DependencyManagement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.DependencyManagement;
import org.apache.maven.api.model.Exclusion;
import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.InputSource;
import org.apache.maven.api.model.Model;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblem;
Expand Down Expand Up @@ -75,11 +73,6 @@ public Model importManagement(
+ toString(present) + ". Add the conflicting managed dependency directly "
+ "to the dependencyManagement section of the POM."));
}

if (present == null && request.isLocationTracking()) {
Dependency updatedDependency = updateWithImportedFrom(dependency, source);
dependencies.put(key, updatedDependency);
}
}
}

Expand Down Expand Up @@ -143,43 +136,4 @@ private boolean equals(Exclusion e1, Exclusion e2) {
return Objects.equals(e1.getGroupId(), e2.getGroupId())
&& Objects.equals(e1.getArtifactId(), e2.getArtifactId());
}

static Dependency updateWithImportedFrom(Dependency dependency, DependencyManagement bom) {
// We are only interested in the InputSource, so the location of the <dependency> element is sufficient
InputLocation dependencyLocation = dependency.getLocation("");
InputLocation bomLocation = bom.getLocation("");

if (dependencyLocation == null || bomLocation == null) {
return dependency;
}

InputSource dependencySource = dependencyLocation.getSource();
InputSource bomSource = bomLocation.getSource();

// If the dependency and BOM have the same source, it means we found the root where the dependency is declared.
if (dependencySource == null
|| bomSource == null
|| Objects.equals(dependencySource.getModelId(), bomSource.getModelId())) {
return Dependency.newBuilder(dependency, true)
.importedFrom(bomLocation)
.build();
}

while (dependencySource.getImportedFrom() != null) {
InputLocation importedFrom = dependencySource.getImportedFrom();

// Stop if the BOM is already in the list, no update necessary
if (Objects.equals(importedFrom.getSource().getModelId(), bomSource.getModelId())) {
return dependency;
}

dependencySource = importedFrom.getSource();
}

// We modify the input location that is used for the whole file.
// This is likely correct because the POM hierarchy applies to the whole POM, not just one dependency.
return Dependency.newBuilder(dependency, true)
.importedFrom(new InputLocation(bomLocation))
.build();
}
}
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ under the License.
<maven.site.path>ref/4-LATEST</maven.site.path>
<project.build.outputTimestamp>2024-05-22T14:07:09Z</project.build.outputTimestamp>
<!-- various versions -->
<assertjVersion>3.26.0</assertjVersion>
<asmVersion>9.7</asmVersion>
<byteBuddyVersion>1.14.18</byteBuddyVersion>
<cipherVersion>2.0</cipherVersion>
Expand Down Expand Up @@ -606,6 +607,11 @@ under the License.
<version>${hamcrestVersion}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertjVersion}</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-testing</artifactId>
Expand Down
3 changes: 1 addition & 2 deletions src/mdo/model.vm
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@ public class ${class.name}
}

@Nonnull
public Builder importedFrom( InputLocation importedFrom )
{
public Builder importedFrom(InputLocation importedFrom) {
this.importedFrom = importedFrom;
return this;
}
Expand Down

0 comments on commit 78b912c

Please sign in to comment.