Skip to content

Commit

Permalink
Automatically add Spring Modulith event externalization dependency
Browse files Browse the repository at this point in the history
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.

See gh-1528
  • Loading branch information
odrotbohm authored and mhalbritter committed Jul 3, 2024
1 parent 119e379 commit d9bc915
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class SpringModulithBuildCustomizer implements BuildCustomizer<Build> {

private static final Collection<String> PERSISTENCE = List.of("jdbc", "jpa", "mongodb");

private static final Collection<String> BROKERS = List.of("activemq", "amqp", "artemis", "kafka");

@Override
public void customize(Build build) {
DependencyContainer dependencies = build.dependencies();
Expand All @@ -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));
}
Expand All @@ -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!");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down

0 comments on commit d9bc915

Please sign in to comment.