Skip to content

Commit

Permalink
Add <type>zip</type> to Elasticsearch dist in dependencies bom
Browse files Browse the repository at this point in the history
Fixes gh-28725
  • Loading branch information
wilkinsona committed Nov 19, 2021
1 parent c4e2252 commit f2b959b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,28 @@ public Object methodMissing(String name, Object args) {
if (args instanceof Object[] && ((Object[]) args).length == 1) {
Object arg = ((Object[]) args)[0];
if (arg instanceof Closure) {
ExclusionHandler exclusionHandler = new ExclusionHandler();
ConfigureUtil.configure((Closure<?>) arg, exclusionHandler);
return new Module(name, exclusionHandler.exclusions);
ModuleHandler moduleHandler = new ModuleHandler();
ConfigureUtil.configure((Closure<?>) arg, moduleHandler);
return new Module(name, moduleHandler.type, moduleHandler.exclusions);
}
}
throw new InvalidUserDataException("Invalid exclusion configuration for module '" + name + "'");
throw new InvalidUserDataException("Invalid configuration for module '" + name + "'");
}

public class ExclusionHandler {
public class ModuleHandler {

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

private String type;

public void exclude(Map<String, String> exclusion) {
this.exclusions.add(new Exclusion(exclusion.get("group"), exclusion.get("module")));
}

public void setType(String type) {
this.type = type;
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.boot.build.bom;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import groovy.util.Node;
Expand All @@ -34,6 +36,7 @@
import org.springframework.boot.build.DeployedPlugin;
import org.springframework.boot.build.MavenRepositoryPlugin;
import org.springframework.boot.build.bom.Library.Group;
import org.springframework.boot.build.bom.Library.Module;
import org.springframework.boot.build.bom.bomr.UpgradeBom;

/**
Expand Down Expand Up @@ -108,6 +111,7 @@ private void customizePom(MavenPom pom) {
addPropertiesBeforeDependencyManagement(projectNode, properties);
replaceVersionsWithVersionPropertyReferences(dependencyManagement);
addExclusionsToManagedDependencies(dependencyManagement);
addTypesToManagedDependencies(dependencyManagement);
}
else {
projectNode.children().add(properties);
Expand Down Expand Up @@ -160,6 +164,30 @@ private void addExclusionsToManagedDependencies(Node dependencyManagement) {
}
}

private void addTypesToManagedDependencies(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();
Set<String> types = 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::getType)
.filter(Objects::nonNull).collect(Collectors.toSet());
if (types.size() > 1) {
throw new IllegalStateException(
"Multiple types for " + groupId + ":" + artifactId + ": " + types);
}
if (types.size() == 1) {
String type = types.iterator().next();
dependency.appendNode("type", type);
}
}
}
}

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
Expand Up @@ -187,21 +187,36 @@ public static class Module {

private final String name;

private final String type;

private final List<Exclusion> exclusions;

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

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

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

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

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

public String getType() {
return this.type;
}

public List<Exclusion> getExclusions() {
return this.exclusions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,36 @@ void moduleExclusionsAreIncludedInDependencyManagementOfGeneratedPom() throws IO
});
}

@Test
void moduleTypesAreIncludedInDependencyManagementOfGeneratedPom() 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('Elasticsearch', '7.15.2') {");
out.println(" group('org.elasticsearch.distribution.integ-test-zip') {");
out.println(" modules = [");
out.println(" 'elasticsearch' {");
out.println(" type = 'zip'");
out.println(" }");
out.println(" ]");
out.println(" }");
out.println(" }");
out.println("}");
}
generatePom((pom) -> {
assertThat(pom).textAtPath("//properties/elasticsearch.version").isEqualTo("7.15.2");
NodeAssert dependency = pom.nodeAtPath("//dependencyManagement/dependencies/dependency");
assertThat(dependency).textAtPath("groupId").isEqualTo("org.elasticsearch.distribution.integ-test-zip");
assertThat(dependency).textAtPath("artifactId").isEqualTo("elasticsearch");
assertThat(dependency).textAtPath("version").isEqualTo("${elasticsearch.version}");
assertThat(dependency).textAtPath("scope").isNullOrEmpty();
assertThat(dependency).textAtPath("type").isEqualTo("zip");
assertThat(dependency).nodeAtPath("exclusions").isNull();
});
}

@Test
void libraryNamedSpringBootHasNoVersionProperty() throws IOException {
try (PrintWriter out = new PrintWriter(new FileWriter(this.buildFile))) {
Expand Down
4 changes: 3 additions & 1 deletion spring-boot-project/spring-boot-dependencies/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ bom {
}
group("org.elasticsearch.distribution.integ-test-zip") {
modules = [
"elasticsearch"
"elasticsearch" {
type = 'zip'
}
]
}
group("org.elasticsearch.plugin") {
Expand Down

0 comments on commit f2b959b

Please sign in to comment.