Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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,13 @@ public Map<Object, InputLocation> getLocations() {
return locations;
}

/**
* Gets the input location that caused this model to be read.
*/
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 @@ -169,4 +188,9 @@ public interface StringFormatter {
*/
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,6 @@

public interface InputLocationTracker {
InputLocation getLocation(Object field);

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,27 @@ 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 = modelId;
this.location = location;
this.inputs = null;
this.importedFrom = null;
}

private 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 +74,14 @@ public String getModelId() {
return this.modelId;
}

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;
}

public InputSource importedFrom(InputLocation importedFrom) {
return new InputSource(modelId, location, importedFrom);
Copy link
Member

Choose a reason for hiding this comment

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

really create a new object and forget about it? strange (i did not really dig in depth, just giving initial feedback)

Copy link
Contributor

Choose a reason for hiding this comment

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

This is due to the immutable model, but I think we can simply remove this and make the new constructor public. We don't seem to use it (anymore) upon further inspection...

}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,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 @@ -1683,13 +1684,20 @@ 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)
.importedFrom(dependency.getDelegate().getLocation(""))
Copy link
Member

Choose a reason for hiding this comment

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

importedFrom should not be assigned to DependencyManagement but to each Dependency, isn't it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@hboutemy you're right. It should only map it from model v3 to model v4. We're going to remove the line .importedFrom(dependency.getDelegate().getLocation(""))

.build());
} else {
importMgmts.add(importMgmt.getDelegate());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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.model.building.ModelBuildingRequest;
import org.apache.maven.model.building.ModelProblem;
Expand Down Expand Up @@ -73,6 +75,11 @@ public Model importManagement(
+ toString(present) + ". Add a 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 @@ -136,4 +143,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.model.composition;

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").importedFrom(new InputLocation(bomSource));
final InputSource dependencySource =
new InputSource("DEPENDENCY", "DEPENDENCY").importedFrom(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").importedFrom(new InputLocation(bomSource));
final InputSource dependencySource =
new InputSource("DEPENDENCY", "DEPENDENCY").importedFrom(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 @@ -59,6 +59,11 @@ public final class InputLocation implements java.io.Serializable, Cloneable, Inp
*/
private InputLocation location;

/**
* Field importedFrom.
*/
private InputLocation importedFrom;

// ----------------/
// - Constructors -/
// ----------------/
Expand All @@ -73,6 +78,7 @@ public InputLocation(org.apache.maven.api.model.InputLocation location) {
.collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue() == location ? this : new InputLocation(e.getValue())));
this.importedFrom = location.getImportedFrom() != null ? new InputLocation(location.getImportedFrom()) : null;
}

public InputLocation(int lineNumber, int columnNumber) {
Expand Down Expand Up @@ -217,6 +223,24 @@ public InputSource getSource() {
return this.source;
} // -- InputSource getSource()

/**
* Get the imported from location.
*
* @return InputLocation
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

*/
public InputLocation getImportedFrom() {
return importedFrom;
}

/**
* Set the imported from location.
*
* @param importedFrom
*/
public void setImportedFrom(InputLocation importedFrom) {
this.importedFrom = importedFrom;
}

/**
* Method merge.
*
Expand Down
Loading