Skip to content

Commit

Permalink
Introduce a specific type for modelling an exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Jul 28, 2020
1 parent 412070d commit 985c3b6
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -103,7 +103,7 @@ void addImplicitManagedVersion(String group, String name, String version) {
this.versions.put(createKey(group, name), version);
}

void addExplicitManagedVersion(String group, String name, String version, List<String>
void addExplicitManagedVersion(String group, String name, String version, List<Exclusion>
exclusions) {
String key = createKey(group, name);
this.explicitVersions.put(key, version);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -90,7 +90,7 @@ void addImplicitManagedVersion(Configuration configuration, String group, String
* @param exclusions the dependency's exclusions
*/
public void addManagedVersion(Configuration configuration, String group, String name, String version,
List<String> exclusions) {
List<Exclusion> exclusions) {
dependencyManagementForConfiguration(configuration)
.addExplicitManagedVersion(group, name, version, exclusions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2014-2020 the original author or authors.
*
* Licensed 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
*
* https://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 io.spring.gradle.dependencymanagement.internal;

/**
* An exclusion of an artifact by group ID and artifact ID.
*
* @author Andy Wilkinson
*/
public class Exclusion {

private final String groupId;

private final String artifactId;

/**
* Creates a new {@code Exclusion} using the given {@code groupId} and {@code artifactId}.
*
* @param groupId group ID
* @param artifactId artifact ID
*/
public Exclusion(String groupId, String artifactId) {
this.groupId = groupId;
this.artifactId = artifactId;
}

/**
* Returns the group ID that is excluded.
* @return the group ID
*/
public String getGroupId() {
return groupId;
}

/**
* Returns the artifact ID that is excluded.
* @return the artifact ID
*/
public String getArtifactId() {
return artifactId;
}

@Override
public int hashCode() {
int result = 1;
result = 31 * result + artifactId.hashCode();
result = 31 * result + groupId.hashCode();
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Exclusion other = (Exclusion) obj;
if (!artifactId.equals(other.artifactId)) {
return false;
}
if (!groupId.equals(other.groupId)) {
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -142,7 +142,7 @@ else if (dependencyResult instanceof UnresolvedDependencyResult) {
private Set<DependencyCandidate> determineIncludedComponents(ResolvedComponentResult root,
Map<String, Exclusions> pomExclusionsById) {
LinkedList<Node> queue = new LinkedList<Node>();
queue.add(new Node(root, getId(root), new HashSet<String>()));
queue.add(new Node(root, getId(root), new HashSet<Exclusion>()));
Set<ResolvedComponentResult> seen = new HashSet<ResolvedComponentResult>();
Set<DependencyCandidate> includedComponents = new HashSet<DependencyCandidate>();
while (!queue.isEmpty()) {
Expand Down Expand Up @@ -194,9 +194,9 @@ private DependencyCandidate toDependencyCandidate(
.getGroup(), attemptedModuleSelector.getModule());
}

private Set<String> getChildExclusions(Node parent, String childId,
private Set<Exclusion> getChildExclusions(Node parent, String childId,
Map<String, Exclusions> pomExclusionsById) {
Set<String> childExclusions = new HashSet<String>(parent.exclusions);
Set<Exclusion> childExclusions = new HashSet<Exclusion>(parent.exclusions);
addAllIfPossible(childExclusions,
this.dependencyManagementContainer.getExclusions(this.configuration)
.exclusionsForDependency(childId));
Expand All @@ -207,7 +207,7 @@ private Set<String> getChildExclusions(Node parent, String childId,
return childExclusions;
}

private void addAllIfPossible(Set<String> current, Set<String> addition) {
private void addAllIfPossible(Set<Exclusion> current, Set<Exclusion> addition) {
if (addition != null) {
current.addAll(addition);
}
Expand All @@ -225,16 +225,25 @@ private static final class Node {

private final String id;

private final Set<String> exclusions;
private final Set<Exclusion> exclusions;

private Node(ResolvedComponentResult component, String id, Set<String> exclusions) {
private Node(ResolvedComponentResult component, String id, Set<Exclusion> exclusions) {
this.component = component;
this.id = id;
this.exclusions = exclusions;
}

private boolean excluded(String id) {
return this.exclusions != null && this.exclusions.contains(id);
if (this.exclusions == null || this.exclusions.isEmpty()) {
return false;
}
String[] components = id.split(":");
for (Exclusion exclusion: this.exclusions) {
if (exclusion.getGroupId().equals(components[0]) && exclusion.getArtifactId().equals(components[1])) {
return true;
}
}
return false;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -94,7 +94,7 @@ private Exclusions collectExclusions(Pom pom) {
.contains(dependency.getScope())) {
String dependencyId = dependency.getCoordinates().getGroupId() + ":" +
dependency.getCoordinates().getArtifactId();
exclusions.add(dependencyId, new HashSet<String>(dependency.getExclusions()));
exclusions.add(dependencyId, new HashSet<Exclusion>(dependency.getExclusions()));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,24 +29,24 @@
*/
class Exclusions {

private final Map<String, Set<String>> exclusionsByDependency = new HashMap<String, Set<String>>();
private final Map<String, Set<Exclusion>> exclusionsByDependency = new HashMap<String, Set<Exclusion>>();

void add(String dependency, Collection<String> exclusionsForDependency) {
Set<String> exclusions = this.exclusionsByDependency.get(dependency);
void add(String dependency, Collection<Exclusion> exclusionsForDependency) {
Set<Exclusion> exclusions = this.exclusionsByDependency.get(dependency);
if (exclusions == null) {
exclusions = new HashSet<String>();
exclusions = new HashSet<Exclusion>();
this.exclusionsByDependency.put(dependency, exclusions);
}
exclusions.addAll(exclusionsForDependency);
}

void addAll(Exclusions exclusions) {
for (Map.Entry<String, Set<String>> entry : exclusions.exclusionsByDependency.entrySet()) {
for (Map.Entry<String, Set<Exclusion>> entry : exclusions.exclusionsByDependency.entrySet()) {
add(entry.getKey(), entry.getValue());
}
}

Set<String> exclusionsForDependency(String dependency) {
Set<Exclusion> exclusionsForDependency(String dependency) {
return this.exclusionsByDependency.get(dependency);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -211,11 +211,10 @@ private void addManagedDependency(Node managedDependencies, Dependency managedDe
managedDependency.getType());
if (!managedDependency.getExclusions().isEmpty()) {
Node exclusionsNode = dependencyNode.appendNode(NODE_NAME_EXCLUSIONS);
for (String exclusion : managedDependency.getExclusions()) {
String[] exclusionComponents = exclusion.split(":");
for (Exclusion exclusion : managedDependency.getExclusions()) {
Node exclusionNode = exclusionsNode.appendNode(NODE_NAME_EXCLUSION);
exclusionNode.appendNode(NODE_NAME_GROUP_ID, exclusionComponents[0]);
exclusionNode.appendNode(NODE_NAME_ARTIFACT_ID, exclusionComponents[1]);
exclusionNode.appendNode(NODE_NAME_GROUP_ID, exclusion.getGroupId());
exclusionNode.appendNode(NODE_NAME_ARTIFACT_ID, exclusion.getArtifactId());
}
}
if (classifier != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2018 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import org.gradle.api.InvalidUserDataException;

import io.spring.gradle.dependencymanagement.dsl.DependencyHandler;
import io.spring.gradle.dependencymanagement.internal.Exclusion;

/**
* Standard implementation of {@link DependencyHandler}.
Expand All @@ -31,7 +32,7 @@
*/
final class StandardDependencyHandler implements DependencyHandler {

private final List<String> exclusions = new ArrayList<String>();
private final List<Exclusion> exclusions = new ArrayList<Exclusion>();

@Override
public void exclude(String exclusion) {
Expand All @@ -40,7 +41,7 @@ public void exclude(String exclusion) {
throw new InvalidUserDataException("Exclusion '" + exclusion + "' is malformed. The required"
+ " form is 'group:name'");
}
this.exclusions.add(exclusion);
this.exclusions.add(new Exclusion(components[0], components[1]));
}

@Override
Expand All @@ -50,10 +51,10 @@ public void exclude(Map<String, String> exclusion) {
if (!hasText(group) || !hasText(name)) {
throw new InvalidUserDataException("An exclusion requires both a group and a name");
}
this.exclusions.add(exclusion.get("group") + ":" + exclusion.get("name"));
this.exclusions.add(new Exclusion(group, name));
}

List<String> getExclusions() {
List<Exclusion> getExclusions() {
return this.exclusions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.gradle.api.specs.Specs;

import io.spring.gradle.dependencymanagement.internal.DependencyManagementConfigurationContainer;
import io.spring.gradle.dependencymanagement.internal.Exclusion;
import io.spring.gradle.dependencymanagement.internal.pom.Coordinates;
import io.spring.gradle.dependencymanagement.internal.pom.Dependency;
import io.spring.gradle.dependencymanagement.internal.pom.Pom;
Expand All @@ -43,7 +44,6 @@
import io.spring.gradle.dependencymanagement.internal.properties.CompositePropertySource;
import io.spring.gradle.dependencymanagement.internal.properties.MapPropertySource;
import io.spring.gradle.dependencymanagement.internal.properties.PropertySource;
import io.spring.gradle.dependencymanagement.org.apache.maven.model.Exclusion;
import io.spring.gradle.dependencymanagement.org.apache.maven.model.Model;

/**
Expand Down Expand Up @@ -150,10 +150,10 @@ private List<Dependency> getManagedDependencies(Model model) {
}

private Dependency createDependency(io.spring.gradle.dependencymanagement.org.apache.maven.model.Dependency dependency) {
Set<String> exclusions = new LinkedHashSet<String>();
Set<Exclusion> exclusions = new LinkedHashSet<Exclusion>();
if (dependency.getExclusions() != null) {
for (Exclusion exclusion: dependency.getExclusions()) {
exclusions.add(exclusion.getGroupId() + ":" + exclusion.getArtifactId());
for (io.spring.gradle.dependencymanagement.org.apache.maven.model.Exclusion exclusion: dependency.getExclusions()) {
exclusions.add(new Exclusion(exclusion.getGroupId(), exclusion.getArtifactId()));
}
}
return new Dependency(new Coordinates(dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,8 @@
import java.util.Collections;
import java.util.Set;

import io.spring.gradle.dependencymanagement.internal.Exclusion;

/**
* A dependency in a Maven {@link Pom}.
*
Expand All @@ -36,15 +38,15 @@ public final class Dependency {

private final String scope;

private final Set<String> exclusions;
private final Set<Exclusion> exclusions;

/**
* Creates a new dependency.
*
* @param coordinates the coordinates
* @param exclusions the exclusions int the form {@code groupId:artifactId}
*/
public Dependency(Coordinates coordinates, Set<String> exclusions) {
public Dependency(Coordinates coordinates, Set<Exclusion> exclusions) {
this(coordinates, false, null, null, null, exclusions);
}

Expand All @@ -59,13 +61,13 @@ public Dependency(Coordinates coordinates, Set<String> exclusions) {
* @param exclusions the exclusions int the form {@code groupId:artifactId}
*/
public Dependency(Coordinates coordinates, boolean optional, String type,
String classifier, String scope, Set<String> exclusions) {
String classifier, String scope, Set<Exclusion> exclusions) {
this.coordinates = coordinates;
this.optional = optional;
this.type = type == null ? "jar" : type;
this.classifier = classifier;
this.scope = scope;
this.exclusions = exclusions == null ? Collections.<String>emptySet() : exclusions;
this.exclusions = exclusions == null ? Collections.<Exclusion>emptySet() : exclusions;
}

/**
Expand All @@ -82,7 +84,7 @@ public Coordinates getCoordinates() {
*
* @return the exclusions
*/
public Set<String> getExclusions() {
public Set<Exclusion> getExclusions() {
return this.exclusions;
}

Expand Down
Loading

0 comments on commit 985c3b6

Please sign in to comment.