-
Notifications
You must be signed in to change notification settings - Fork 906
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1228 from odrotbohm
* pr/1228: Polish "Add support for Spring Modulith" Add support for Spring Modulith Closes gh-1228
- Loading branch information
Showing
6 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
.../spring/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.start.site.extension.dependency.springmodulith; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import io.spring.initializr.generator.buildsystem.Build; | ||
import io.spring.initializr.generator.buildsystem.Dependency; | ||
import io.spring.initializr.generator.buildsystem.Dependency.Builder; | ||
import io.spring.initializr.generator.buildsystem.DependencyContainer; | ||
import io.spring.initializr.generator.buildsystem.DependencyScope; | ||
import io.spring.initializr.generator.spring.build.BuildCustomizer; | ||
|
||
/** | ||
* Registers Spring Modulith dependencies to the build file of the project to be created, | ||
* in particular registering integration dependencies depending on others registered for | ||
* inclusion as well (observability and persistence). | ||
* | ||
* @author Oliver Drotbohm | ||
* @author Stephane Nicoll | ||
*/ | ||
class SpringModulithBuildCustomizer implements BuildCustomizer<Build> { | ||
|
||
private static final Collection<String> OBSERVABILITY_DEPENDENCIES = List.of("actuator", "datadog", "graphite", | ||
"influx", "new-relic", "prometheus", "wavefront", "zipkin"); | ||
|
||
@Override | ||
public void customize(Build build) { | ||
DependencyContainer dependencies = build.dependencies(); | ||
if (dependencies.has("actuator")) { | ||
dependencies.add("modulith-actuator", modulithDependency("actuator").scope(DependencyScope.RUNTIME)); | ||
} | ||
if (OBSERVABILITY_DEPENDENCIES.stream().anyMatch(dependencies::has)) { | ||
dependencies.add("modulith-observability", | ||
modulithDependency("observability").scope(DependencyScope.RUNTIME)); | ||
} | ||
addEventPublicationRegistryBackend(build); | ||
dependencies.add("modulith-starter-test", | ||
modulithDependency("starter-test").scope(DependencyScope.TEST_COMPILE)); | ||
} | ||
|
||
private void addEventPublicationRegistryBackend(Build build) { | ||
DependencyContainer dependencies = build.dependencies(); | ||
if (dependencies.has("data-mongodb")) { | ||
dependencies.add("modulith-starter-mongodb", modulithDependency("starter-mongodb")); | ||
} | ||
if (dependencies.has("data-jdbc")) { | ||
dependencies.add("modulith-starter-jdbc", modulithDependency("starter-jdbc")); | ||
} | ||
if (dependencies.has("data-jpa")) { | ||
dependencies.add("modulith-starter-jpa", modulithDependency("starter-jpa")); | ||
} | ||
} | ||
|
||
private Builder<?> modulithDependency(String name) { | ||
return Dependency.withCoordinates("org.springframework.modulith", "spring-modulith-" + name); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ite/extension/dependency/springmodulith/SpringModulithProjectGenerationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.start.site.extension.dependency.springmodulith; | ||
|
||
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; | ||
import io.spring.initializr.generator.project.ProjectGenerationConfiguration; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* Registers {@link SpringModulithBuildCustomizer} with the context to allow selecting | ||
* Spring Modulith dependencies. | ||
* | ||
* @author Oliver Drotbohm | ||
*/ | ||
@ProjectGenerationConfiguration | ||
@ConditionalOnRequestedDependency("modulith") | ||
class SpringModulithProjectGenerationConfiguration { | ||
|
||
@Bean | ||
SpringModulithBuildCustomizer springModulithBuildCustomizer() { | ||
return new SpringModulithBuildCustomizer(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
.../src/main/java/io/spring/start/site/extension/dependency/springmodulith/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Extensions for generation of projects that depend on Spring Modulith. | ||
*/ | ||
package io.spring.start.site.extension.dependency.springmodulith; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ng/start/site/extension/dependency/springmodulith/SpringModulithBuildCustomizerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2012-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.spring.start.site.extension.dependency.springmodulith; | ||
|
||
import java.util.Arrays; | ||
|
||
import io.spring.initializr.generator.buildsystem.Build; | ||
import io.spring.initializr.generator.buildsystem.maven.MavenBuild; | ||
import io.spring.initializr.generator.version.Version; | ||
import io.spring.initializr.metadata.InitializrMetadata; | ||
import io.spring.initializr.metadata.support.MetadataBuildItemResolver; | ||
import io.spring.start.site.extension.AbstractExtensionTests; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link SpringModulithBuildCustomizer}. | ||
* | ||
* @author Oliver Drotbohm | ||
*/ | ||
class SpringModulithBuildCustomizerTests extends AbstractExtensionTests { | ||
|
||
private final SpringModulithBuildCustomizer customizer = new SpringModulithBuildCustomizer(); | ||
|
||
@Test | ||
void registersTestStarterWhenModulithIsSelected() { | ||
Build build = createBuild("modulith"); | ||
this.customizer.customize(build); | ||
assertThat(build.dependencies().ids()).contains("modulith-starter-test"); | ||
} | ||
|
||
@Test | ||
void registersActuatorStarterIfActuatorsIsPresent() { | ||
Build build = createBuild("modulith", "actuator"); | ||
this.customizer.customize(build); | ||
assertThat(build.dependencies().ids()).contains("modulith-actuator"); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource( | ||
strings = { "actuator", "datadog", "graphite", "influx", "new-relic", "prometheus", "wavefront", "zipkin" }) | ||
void registersObservabilityStarterIfObservabilityDependencyIsPresent(String dependency) { | ||
Build build = createBuild("modulith"); | ||
build.dependencies().add(dependency); | ||
this.customizer.customize(build); | ||
assertThat(build.dependencies().ids()).contains("modulith-observability"); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { "jdbc", "jpa", "mongodb" }) | ||
void presenceOfSpringDataModuleAddsModuleEventStarter(String store) { | ||
Build build = createBuild("modulith"); | ||
build.dependencies().add("data-" + store); | ||
this.customizer.customize(build); | ||
assertThat(build.dependencies().ids()).contains("modulith-starter-" + store); | ||
} | ||
|
||
private Build createBuild(String... dependencies) { | ||
InitializrMetadata metadata = getMetadata(); | ||
MavenBuild build = new MavenBuild(new MetadataBuildItemResolver(metadata, getDefaultPlatformVersion(metadata))); | ||
Arrays.stream(dependencies).forEach(build.dependencies()::add); | ||
return build; | ||
} | ||
|
||
private Version getDefaultPlatformVersion(InitializrMetadata metadata) { | ||
return Version.parse(metadata.getBootVersions().getDefault().getId()); | ||
} | ||
|
||
} |