Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add Spring Modulith event externalization dependency #1528

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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