Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-7344] track dependencyManagement import location in effective Model for MPH-183 #603

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ public class InputLocation implements Serializable, InputLocationTracker {
private final int columnNumber;
private final InputSource source;
private final Map<Object, InputLocation> locations;
private final InputLocation importedFrom;

public InputLocation(InputSource source) {
this.lineNumber = -1;
this.columnNumber = -1;
this.source = source;
this.locations = Collections.singletonMap(0, this);
this.importedFrom = null;
}

public InputLocation(int lineNumber, int columnNumber) {
Expand All @@ -54,13 +56,23 @@ public InputLocation(int lineNumber, int columnNumber, InputSource source, Objec
this.source = source;
this.locations =
selfLocationKey != null ? Collections.singletonMap(selfLocationKey, this) : Collections.emptyMap();
this.importedFrom = null;
}

public InputLocation(int lineNumber, int columnNumber, InputSource source, Map<Object, InputLocation> locations) {
this.lineNumber = lineNumber;
this.columnNumber = columnNumber;
this.source = source;
this.locations = ImmutableCollections.copy(locations);
this.importedFrom = null;
}

public InputLocation(InputLocation original) {
this.lineNumber = original.lineNumber;
this.columnNumber = original.columnNumber;
this.source = original.source;
this.locations = original.locations;
this.importedFrom = original.importedFrom;
}

public int getLineNumber() {
Expand All @@ -83,6 +95,17 @@ public Map<Object, InputLocation> getLocations() {
return locations;
}

/**
* Gets the parent InputLocation where this InputLocation may have been imported from.
* Can return {@code null}.
*
* @return InputLocation
* @since 4.0.0
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need an @since here to remember

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"that caused this model to be read."?
I need to dig more into code to propose another description, but this one is not clear

public InputLocation getImportedFrom() {
return importedFrom;
}

/**
* Merges the {@code source} location into the {@code target} location.
*
Expand Down Expand Up @@ -152,4 +175,26 @@ public static InputLocation merge(InputLocation target, InputLocation source, Co

return new InputLocation(-1, -1, InputSource.merge(source.getSource(), target.getSource()), locations);
} // -- InputLocation merge( InputLocation, InputLocation, java.util.Collection )

/**
* Class StringFormatter.
*
* @version $Revision$ $Date$
*/
public interface StringFormatter {

// -----------/
// - Methods -/
// -----------/

/**
* Method toString.
*/
String toString(InputLocation location);
}

@Override
public String toString() {
return String.format("%s @ %d:%d", source.getLocation(), lineNumber, columnNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@

public interface InputLocationTracker {
InputLocation getLocation(Object field);

/**
* Gets the parent InputLocation where this InputLocation may have been imported from.
* Can return {@code null}.
*
* @return InputLocation
* @since 4.0.0
*/
InputLocation getImportedFrom();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same @since

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,24 @@ public class InputSource implements Serializable {
private final String modelId;
private final String location;
private final List<InputSource> inputs;
private final InputLocation importedFrom;

public InputSource(String modelId, String location) {
this(modelId, location, null);
}

public InputSource(String modelId, String location, InputLocation importedFrom) {
this.modelId = modelId;
this.location = location;
this.inputs = null;
this.importedFrom = importedFrom;
}

public InputSource(Collection<InputSource> inputs) {
this.modelId = null;
this.location = null;
this.inputs = ImmutableCollections.copy(inputs);
this.importedFrom = null;
}

/**
Expand All @@ -64,6 +71,17 @@ public String getModelId() {
return this.modelId;
}

/**
* Gets the parent InputLocation where this InputLocation may have been imported from.
* Can return {@code null}.
*
* @return InputLocation
* @since 4.0.0
*/
public InputLocation getImportedFrom() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same @since

return importedFrom;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
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
@@ -0,0 +1,124 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.internal.impl.model;

import org.apache.maven.api.model.Dependency;
import org.apache.maven.api.model.DependencyManagement;
import org.apache.maven.api.model.InputLocation;
import org.apache.maven.api.model.InputSource;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class DefaultDependencyManagementImporterTest {
@Test
void testUpdateWithImportedFrom_dependencyLocationAndBomLocationAreNull_dependencyReturned() {
final Dependency dependency = Dependency.newBuilder().build();
final DependencyManagement depMgmt = DependencyManagement.newBuilder().build();
final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, depMgmt);

assertThat(dependency).isEqualTo(result);
}

@Test
void testUpdateWithImportedFrom_dependencyManagementAndDependencyHaveSameSource_dependencyImportedFromSameSource() {
final InputSource source = new InputSource("SINGLE_SOURCE", "");
final Dependency dependency = Dependency.newBuilder()
.location("", new InputLocation(1, 1, source))
.build();
final DependencyManagement bom = DependencyManagement.newBuilder()
.location("", new InputLocation(1, 1, source))
.build();

final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, bom);

assertThat(result).isNotNull();
assertThat(result.getImportedFrom().toString())
.isEqualTo(bom.getLocation("").toString());
}

@Test
public void testUpdateWithImportedFrom_singleLevel_importedFromSet() {
// Arrange
final InputSource dependencySource = new InputSource("DEPENDENCY", "DEPENDENCY");
final InputSource bomSource = new InputSource("BOM", "BOM");
final Dependency dependency = Dependency.newBuilder()
.location("", new InputLocation(1, 1, dependencySource))
.build();
final DependencyManagement bom = DependencyManagement.newBuilder()
.location("", new InputLocation(2, 2, bomSource))
.build();

// Act
final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, bom);

// Assert
assertThat(result).isNotNull();
assertThat(result.getImportedFrom().toString())
.isEqualTo(bom.getLocation("").toString());
}

@Test
public void testUpdateWithImportedFrom_multiLevel_importedFromSetChanged() {
// Arrange
final InputSource bomSource = new InputSource("BOM", "BOM");
final InputSource intermediateSource =
new InputSource("INTERMEDIATE", "INTERMEDIATE", new InputLocation(bomSource));
final InputSource dependencySource =
new InputSource("DEPENDENCY", "DEPENDENCY", new InputLocation(intermediateSource));
final InputLocation bomLocation = new InputLocation(2, 2, bomSource);
final Dependency dependency = Dependency.newBuilder()
.location("", new InputLocation(1, 1, dependencySource))
.importedFrom(bomLocation)
.build();
final DependencyManagement bom =
DependencyManagement.newBuilder().location("", bomLocation).build();

// Act
final Dependency result = DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, bom);

// Assert
assertThat(result.getImportedFrom().toString())
.isEqualTo(bom.getLocation("").toString());
}

@Test
public void testUpdateWithImportedFrom_multiLevelAlreadyFoundInDifferentSource_importedFromSetMaintained() {
// Arrange
final InputSource bomSource = new InputSource("BOM", "BOM");
final InputSource intermediateSource =
new InputSource("INTERMEDIATE", "INTERMEDIATE", new InputLocation(bomSource));
final InputSource dependencySource =
new InputSource("DEPENDENCY", "DEPENDENCY", new InputLocation(intermediateSource));
final Dependency dependency = Dependency.newBuilder()
.location("", new InputLocation(1, 1, dependencySource))
.build();
final DependencyManagement differentSource = DependencyManagement.newBuilder()
.location("", new InputLocation(2, 2, new InputSource("BOM2", "BOM2")))
.build();

// Act
final Dependency result =
DefaultDependencyManagementImporter.updateWithImportedFrom(dependency, differentSource);

// Assert
assertThat(result.getImportedFrom().toString())
.isEqualTo(differentSource.getLocation("").toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,8 @@ private void importDependencyManagement(

importIds.add(importing);

List<org.apache.maven.api.model.DependencyManagement> importMgmts = null;
// Model v4
List<org.apache.maven.api.model.DependencyManagement> importMgmts = new ArrayList<>();

for (Iterator<Dependency> it = depMgmt.getDependencies().iterator(); it.hasNext(); ) {
Dependency dependency = it.next();
Expand All @@ -1734,13 +1735,19 @@ private void importDependencyManagement(

it.remove();

// Model v3
DependencyManagement importMgmt = loadDependencyManagement(model, request, problems, dependency, importIds);
if (importMgmt == null) {
continue;
}

if (importMgmt != null) {
if (importMgmts == null) {
importMgmts = new ArrayList<>();
}

if (request.isLocationTracking()) {
// Keep track of why this DependencyManagement was imported.
// And map model v3 to model v4 -> importMgmt(v3).getDelegate() returns a v4 object
importMgmts.add(
org.apache.maven.api.model.DependencyManagement.newBuilder(importMgmt.getDelegate(), true)
.build());
} else {
importMgmts.add(importMgmt.getDelegate());
}
}
Expand Down
Loading