From f2b959b0a5fce7b904ac7d39f640d0ed5a01e3be Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 18 Nov 2021 18:29:27 +0000 Subject: [PATCH] Add zip to Elasticsearch dist in dependencies bom Fixes gh-28725 --- .../boot/build/bom/BomExtension.java | 16 ++++++---- .../boot/build/bom/BomPlugin.java | 28 +++++++++++++++++ .../boot/build/bom/Library.java | 15 ++++++++++ .../build/bom/BomPluginIntegrationTests.java | 30 +++++++++++++++++++ .../spring-boot-dependencies/build.gradle | 4 ++- 5 files changed, 87 insertions(+), 6 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/BomExtension.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/BomExtension.java index 3979a0014414..7ee1c22dc15e 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/BomExtension.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/BomExtension.java @@ -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 exclusions = new ArrayList<>(); + private String type; + public void exclude(Map exclusion) { this.exclusions.add(new Exclusion(exclusion.get("group"), exclusion.get("module"))); } + public void setType(String type) { + this.type = type; + } + } } diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java index 4bf1d5b2ee81..d5b1c0206d11 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/BomPlugin.java @@ -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; @@ -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; /** @@ -108,6 +111,7 @@ private void customizePom(MavenPom pom) { addPropertiesBeforeDependencyManagement(projectNode, properties); replaceVersionsWithVersionPropertyReferences(dependencyManagement); addExclusionsToManagedDependencies(dependencyManagement); + addTypesToManagedDependencies(dependencyManagement); } else { projectNode.children().add(properties); @@ -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 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()) { diff --git a/buildSrc/src/main/java/org/springframework/boot/build/bom/Library.java b/buildSrc/src/main/java/org/springframework/boot/build/bom/Library.java index abb0fb371f2c..31942fb583b8 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/bom/Library.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/bom/Library.java @@ -187,14 +187,25 @@ public static class Module { private final String name; + private final String type; + private final List 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 exclusions) { + this(name, null, exclusions); + } + + public Module(String name, String type, List exclusions) { this.name = name; + this.type = type; this.exclusions = exclusions; } @@ -202,6 +213,10 @@ public String getName() { return this.name; } + public String getType() { + return this.type; + } + public List getExclusions() { return this.exclusions; } diff --git a/buildSrc/src/test/java/org/springframework/boot/build/bom/BomPluginIntegrationTests.java b/buildSrc/src/test/java/org/springframework/boot/build/bom/BomPluginIntegrationTests.java index 0a93828427a2..f7cbea278991 100644 --- a/buildSrc/src/test/java/org/springframework/boot/build/bom/BomPluginIntegrationTests.java +++ b/buildSrc/src/test/java/org/springframework/boot/build/bom/BomPluginIntegrationTests.java @@ -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))) { diff --git a/spring-boot-project/spring-boot-dependencies/build.gradle b/spring-boot-project/spring-boot-dependencies/build.gradle index 63853706a78d..067eb2cc8b03 100644 --- a/spring-boot-project/spring-boot-dependencies/build.gradle +++ b/spring-boot-project/spring-boot-dependencies/build.gradle @@ -290,7 +290,9 @@ bom { } group("org.elasticsearch.distribution.integ-test-zip") { modules = [ - "elasticsearch" + "elasticsearch" { + type = 'zip' + } ] } group("org.elasticsearch.plugin") {