From 0089e12f419e977ed929f6d2f1c25f2a1a642f52 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 2 Jul 2024 22:08:56 +0200 Subject: [PATCH] Automatically add Spring Modulith event externalization dependency. If a message broker *and* Spring Modulith are declared as project dependencies, the event externalization of Spring Modulith for configured broker are now declared automatically as well. Also, the Spring Modulith Events API dependency is declared in compile scope to allow event types to be marked for externalization. --- .../SpringModulithBuildCustomizer.java | 30 +++++++++++++++++++ .../SpringModulithBuildCustomizerTests.java | 18 +++++++++++ 2 files changed, 48 insertions(+) diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizer.java index a979040ff79..f01b1a44f93 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizer.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizer.java @@ -41,6 +41,8 @@ class SpringModulithBuildCustomizer implements BuildCustomizer { private static final Collection PERSISTENCE = List.of("jdbc", "jpa", "mongodb"); + private static final Collection BROKERS = List.of("activemq", "amqp", "artemis", "kafka"); + @Override public void customize(Build build) { DependencyContainer dependencies = build.dependencies(); @@ -52,6 +54,9 @@ public void customize(Build build) { modulithDependency("observability").scope(DependencyScope.RUNTIME)); } addEventPublicationRegistryBackend(build); + if (addEventExternalizationDependency(build)) { + dependencies.add("modulith-events-api", modulithDependency("events-api")); + } dependencies.add("modulith-starter-test", modulithDependency("starter-test").scope(DependencyScope.TEST_COMPILE)); } @@ -75,4 +80,29 @@ private Builder modulithDependency(String name) { return Dependency.withCoordinates("org.springframework.modulith", "spring-modulith-" + name); } + private boolean addEventExternalizationDependency(Build build) { + DependencyContainer dependencies = build.dependencies(); + return BROKERS.stream() + .filter(dependencies::has) + .map(this::getModulithBrokerKey) + .peek((it) -> dependencies.add("modulith-events-" + it, + modulithDependency("events-" + it).scope(DependencyScope.RUNTIME))) + .findAny() + .isPresent(); + } + + private String getModulithBrokerKey(String broker) { + + switch (broker) { + case "kafka": + case "amqp": + return broker; + case "artemis": + case "activemq": + return "jms"; + default: + throw new IllegalArgumentException("Unsupported broker!"); + } + } + } diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizerTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizerTests.java index 957b56547bc..81ac376dfbe 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizerTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizerTests.java @@ -74,6 +74,24 @@ void presenceOfSpringDataModuleAddsModuleEventStarter(String store) { assertThat(build.dependencies().ids()).doesNotContain("modulith-starter-core"); } + @ParameterizedTest + @ValueSource(strings = { "amqp", "kafka" }) + void addsExternalizationDependency(String broker) { + Build build = createBuild("modulith", broker); + this.customizer.customize(build); + assertThat(build.dependencies().ids()).contains("modulith-events-" + broker); + assertThat(build.dependencies().ids()).contains("modulith-events-api"); + } + + @ParameterizedTest + @ValueSource(strings = { "activemq", "artemis" }) + void addsJmsExternalizationDependency(String broker) { + Build build = createBuild("modulith", broker); + this.customizer.customize(build); + assertThat(build.dependencies().ids()).contains("modulith-events-jms"); + assertThat(build.dependencies().ids()).contains("modulith-events-api"); + } + private Build createBuild(String... dependencies) { InitializrMetadata metadata = getMetadata(); MavenBuild build = new MavenBuild(new MetadataBuildItemResolver(metadata, getDefaultPlatformVersion(metadata)));