Skip to content

Commit

Permalink
Merge branch '2.5.x' into 2.6.x
Browse files Browse the repository at this point in the history
Closes gh-30029
  • Loading branch information
wilkinsona committed Mar 2, 2022
2 parents 3630952 + 69ce392 commit 4ac884e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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 @@ -168,13 +168,18 @@ Map<String, DependencyVersion> getProperties() {
return this.properties;
}

String getArtifactVersionProperty(String groupId, String artifactId) {
String coordinates = groupId + ":" + artifactId;
String getArtifactVersionProperty(String groupId, String artifactId, String classifier) {
String coordinates = groupId + ":" + artifactId + ":" + classifier;
return this.artifactVersionProperties.get(coordinates);
}

private void putArtifactVersionProperty(String groupId, String artifactId, String versionProperty) {
String coordinates = groupId + ":" + artifactId;
putArtifactVersionProperty(groupId, artifactId, null, versionProperty);
}

private void putArtifactVersionProperty(String groupId, String artifactId, String classifier,
String versionProperty) {
String coordinates = groupId + ":" + artifactId + ":" + ((classifier != null) ? classifier : "");
String existing = this.artifactVersionProperties.putIfAbsent(coordinates, versionProperty);
if (existing != null) {
throw new InvalidUserDataException("Cannot put version property for '" + coordinates
Expand All @@ -190,7 +195,7 @@ private void addLibrary(Library library) {
}
for (Group group : library.getGroups()) {
for (Module module : group.getModules()) {
putArtifactVersionProperty(group.getId(), module.getName(), versionProperty);
putArtifactVersionProperty(group.getId(), module.getName(), module.getClassifier(), versionProperty);
this.dependencyHandler.getConstraints().add(JavaPlatformPlugin.API_CONFIGURATION_NAME,
createDependencyNotation(group.getId(), module.getName(), library.getVersion().getVersion()));
}
Expand Down Expand Up @@ -316,7 +321,7 @@ public Object methodMissing(String name, Object args) {
closure.setResolveStrategy(Closure.DELEGATE_FIRST);
closure.setDelegate(moduleHandler);
closure.call(moduleHandler);
return new Module(name, moduleHandler.type, moduleHandler.exclusions);
return new Module(name, moduleHandler.type, moduleHandler.classifier, moduleHandler.exclusions);
}
}
throw new InvalidUserDataException("Invalid configuration for module '" + name + "'");
Expand All @@ -328,6 +333,8 @@ public class ModuleHandler {

private String type;

private String classifier;

public void exclude(Map<String, String> exclusion) {
this.exclusions.add(new Exclusion(exclusion.get("group"), exclusion.get("module")));
}
Expand All @@ -336,6 +343,10 @@ public void setType(String type) {
this.type = type;
}

public void setClassifier(String classifier) {
this.classifier = classifier;
}

}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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 @@ -109,6 +109,7 @@ private void customizePom(MavenPom pom) {
Node dependencyManagement = findChild(projectNode, "dependencyManagement");
if (dependencyManagement != null) {
addPropertiesBeforeDependencyManagement(projectNode, properties);
addClassifiedManagedDependencies(dependencyManagement);
replaceVersionsWithVersionPropertyReferences(dependencyManagement);
addExclusionsToManagedDependencies(dependencyManagement);
addTypesToManagedDependencies(dependencyManagement);
Expand Down Expand Up @@ -136,7 +137,9 @@ private void replaceVersionsWithVersionPropertyReferences(Node dependencyManagem
for (Node dependency : findChildren(dependencies, "dependency")) {
String groupId = findChild(dependency, "groupId").text();
String artifactId = findChild(dependency, "artifactId").text();
String versionProperty = this.bom.getArtifactVersionProperty(groupId, artifactId);
Node classifierNode = findChild(dependency, "classifier");
String classifier = (classifierNode != null) ? classifierNode.text() : "";
String versionProperty = this.bom.getArtifactVersionProperty(groupId, artifactId, classifier);
if (versionProperty != null) {
findChild(dependency, "version").setValue("${" + versionProperty + "}");
}
Expand Down Expand Up @@ -188,6 +191,39 @@ private void addTypesToManagedDependencies(Node dependencyManagement) {
}
}

@SuppressWarnings("unchecked")
private void addClassifiedManagedDependencies(Node dependencyManagement) {
Node dependencies = findChild(dependencyManagement, "dependencies");
if (dependencies != null) {
for (Node dependency : findChildren(dependencies, "dependency")) {
String groupId = findChild(dependency, "groupId").text();
String artifactId = findChild(dependency, "artifactId").text();
String version = findChild(dependency, "version").text();
Set<String> classifiers = this.bom.getLibraries().stream()
.flatMap((library) -> library.getGroups().stream())
.filter((group) -> group.getId().equals(groupId))
.flatMap((group) -> group.getModules().stream())
.filter((module) -> module.getName().equals(artifactId)).map(Module::getClassifier)
.filter(Objects::nonNull).collect(Collectors.toSet());
Node target = dependency;
for (String classifier : classifiers) {
if (classifier.length() > 0) {
if (target == null) {
target = new Node(null, "dependency");
target.appendNode("groupId", groupId);
target.appendNode("artifactId", artifactId);
target.appendNode("version", version);
int index = dependency.parent().children().indexOf(dependency);
dependency.parent().children().add(index + 1, target);
}
target.appendNode("classifier", classifier);
}
target = null;
}
}
}
}

private void addPluginManagement(Node projectNode) {
for (Library library : this.bom.getLibraries()) {
for (Group group : library.getGroups()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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 @@ -189,30 +189,37 @@ public static class Module {

private final String type;

private final String classifier;

private final List<Exclusion> exclusions;

public Module(String name) {
this(name, Collections.emptyList());
}

public Module(String name, String type) {
this(name, type, Collections.emptyList());
this(name, type, null, Collections.emptyList());
}

public Module(String name, List<Exclusion> exclusions) {
this(name, null, exclusions);
this(name, null, null, exclusions);
}

public Module(String name, String type, List<Exclusion> exclusions) {
public Module(String name, String type, String classifier, List<Exclusion> exclusions) {
this.name = name;
this.type = type;
this.classifier = (classifier != null) ? classifier : "";
this.exclusions = exclusions;
}

public String getName() {
return this.name;
}

public String getClassifier() {
return this.classifier;
}

public String getType() {
return this.type;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 the original author or authors.
* Copyright 2012-2022 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 @@ -75,12 +75,14 @@ void libraryModulesAreIncludedInDependencyManagementOfGeneratedPom() throws IOEx
assertThat(dependency).textAtPath("version").isEqualTo("${activemq.version}");
assertThat(dependency).textAtPath("scope").isNullOrEmpty();
assertThat(dependency).textAtPath("type").isNullOrEmpty();
assertThat(dependency).textAtPath("classifier").isNullOrEmpty();
dependency = pom.nodeAtPath("//dependencyManagement/dependencies/dependency[2]");
assertThat(dependency).textAtPath("groupId").isEqualTo("org.apache.activemq");
assertThat(dependency).textAtPath("artifactId").isEqualTo("activemq-blueprint");
assertThat(dependency).textAtPath("version").isEqualTo("${activemq.version}");
assertThat(dependency).textAtPath("scope").isNullOrEmpty();
assertThat(dependency).textAtPath("type").isNullOrEmpty();
assertThat(dependency).textAtPath("classifier").isNullOrEmpty();
});
}

Expand Down Expand Up @@ -135,6 +137,7 @@ void libraryImportsAreIncludedInDependencyManagementOfGeneratedPom() throws Exce
assertThat(dependency).textAtPath("version").isEqualTo("${jackson-bom.version}");
assertThat(dependency).textAtPath("scope").isEqualTo("import");
assertThat(dependency).textAtPath("type").isEqualTo("pom");
assertThat(dependency).textAtPath("classifier").isNullOrEmpty();
});
}

Expand Down Expand Up @@ -164,6 +167,7 @@ void moduleExclusionsAreIncludedInDependencyManagementOfGeneratedPom() throws IO
assertThat(dependency).textAtPath("version").isEqualTo("${mysql.version}");
assertThat(dependency).textAtPath("scope").isNullOrEmpty();
assertThat(dependency).textAtPath("type").isNullOrEmpty();
assertThat(dependency).textAtPath("classifier").isNullOrEmpty();
NodeAssert exclusion = dependency.nodeAtPath("exclusions/exclusion");
assertThat(exclusion).textAtPath("groupId").isEqualTo("com.google.protobuf");
assertThat(exclusion).textAtPath("artifactId").isEqualTo("protobuf-java");
Expand Down Expand Up @@ -196,10 +200,69 @@ void moduleTypesAreIncludedInDependencyManagementOfGeneratedPom() throws IOExcep
assertThat(dependency).textAtPath("version").isEqualTo("${elasticsearch.version}");
assertThat(dependency).textAtPath("scope").isNullOrEmpty();
assertThat(dependency).textAtPath("type").isEqualTo("zip");
assertThat(dependency).textAtPath("classifier").isNullOrEmpty();
assertThat(dependency).nodeAtPath("exclusions").isNull();
});
}

@Test
void moduleClassifiersAreIncludedInDependencyManagementOfGeneratedPom() throws IOException {
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
out.println("plugins {");
out.println(" id 'org.springframework.boot.bom'");
out.println("}");
out.println("bom {");
out.println(" library('Kafka', '2.7.2') {");
out.println(" group('org.apache.kafka') {");
out.println(" modules = [");
out.println(" 'connect-api',");
out.println(" 'generator',");
out.println(" 'generator' {");
out.println(" classifier = 'test'");
out.println(" },");
out.println(" 'kafka-tools',");
out.println(" ]");
out.println(" }");
out.println(" }");
out.println("}");
}
generatePom((pom) -> {
assertThat(pom).textAtPath("//properties/kafka.version").isEqualTo("2.7.2");
NodeAssert connectApi = pom.nodeAtPath("//dependencyManagement/dependencies/dependency[1]");
assertThat(connectApi).textAtPath("groupId").isEqualTo("org.apache.kafka");
assertThat(connectApi).textAtPath("artifactId").isEqualTo("connect-api");
assertThat(connectApi).textAtPath("version").isEqualTo("${kafka.version}");
assertThat(connectApi).textAtPath("scope").isNullOrEmpty();
assertThat(connectApi).textAtPath("type").isNullOrEmpty();
assertThat(connectApi).textAtPath("classifier").isNullOrEmpty();
assertThat(connectApi).nodeAtPath("exclusions").isNull();
NodeAssert generator = pom.nodeAtPath("//dependencyManagement/dependencies/dependency[2]");
assertThat(generator).textAtPath("groupId").isEqualTo("org.apache.kafka");
assertThat(generator).textAtPath("artifactId").isEqualTo("generator");
assertThat(generator).textAtPath("version").isEqualTo("${kafka.version}");
assertThat(generator).textAtPath("scope").isNullOrEmpty();
assertThat(generator).textAtPath("type").isNullOrEmpty();
assertThat(generator).textAtPath("classifier").isNullOrEmpty();
assertThat(generator).nodeAtPath("exclusions").isNull();
NodeAssert generatorTest = pom.nodeAtPath("//dependencyManagement/dependencies/dependency[3]");
assertThat(generatorTest).textAtPath("groupId").isEqualTo("org.apache.kafka");
assertThat(generatorTest).textAtPath("artifactId").isEqualTo("generator");
assertThat(generatorTest).textAtPath("version").isEqualTo("${kafka.version}");
assertThat(generatorTest).textAtPath("scope").isNullOrEmpty();
assertThat(generatorTest).textAtPath("type").isNullOrEmpty();
assertThat(generatorTest).textAtPath("classifier").isEqualTo("test");
assertThat(generatorTest).nodeAtPath("exclusions").isNull();
NodeAssert kafkaTools = pom.nodeAtPath("//dependencyManagement/dependencies/dependency[4]");
assertThat(kafkaTools).textAtPath("groupId").isEqualTo("org.apache.kafka");
assertThat(kafkaTools).textAtPath("artifactId").isEqualTo("kafka-tools");
assertThat(kafkaTools).textAtPath("version").isEqualTo("${kafka.version}");
assertThat(kafkaTools).textAtPath("scope").isNullOrEmpty();
assertThat(kafkaTools).textAtPath("type").isNullOrEmpty();
assertThat(kafkaTools).textAtPath("classifier").isNullOrEmpty();
assertThat(kafkaTools).nodeAtPath("exclusions").isNull();
});
}

@Test
void libraryNamedSpringBootHasNoVersionProperty() throws IOException {
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
Expand Down

0 comments on commit 4ac884e

Please sign in to comment.