Skip to content

Commit

Permalink
Merge pull request #1528 from odrotbohm
Browse files Browse the repository at this point in the history
* pr/1528:
  Polish "Automatically add Spring Modulith event externalization dependency"
  Automatically add Spring Modulith event externalization dependency

Closes gh-1528
  • Loading branch information
mhalbritter committed Jul 3, 2024
2 parents 119e379 + bc87b20 commit 244d69f
Show file tree
Hide file tree
Showing 2 changed files with 42 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,23 @@ 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) {
return switch (broker) {
case "kafka", "amqp" -> broker;
case "artemis", "activemq" -> "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 244d69f

Please sign in to comment.