Skip to content

Commit 16b4882

Browse files
committed
feat(build): auto-add web starter when sample code is enabled
1 parent abc0af4 commit 16b4882

3 files changed

Lines changed: 77 additions & 13 deletions

File tree

src/main/java/io/github/blueprintplatform/codegen/adapter/out/profile/springboot/maven/java/build/MavenPomBuildConfigurationAdapter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ private List<BuildDependency> buildPomDependencies(ProjectBlueprint bp) {
6363
deps.add(MavenPomBuildModel.CORE_STARTER);
6464
deps.addAll(buildDependencyMapper.from(bp.getDependencies()));
6565

66+
if (isSampleEnabled(bp) && !isWebSelected(bp)) {
67+
deps.add(MavenPomBuildModel.WEB_STARTER);
68+
}
69+
6670
if (isJpaSelected(bp)) {
6771
deps.add(MavenPomBuildModel.H2_DB);
6872
}
@@ -76,6 +80,18 @@ private List<BuildDependency> buildPomDependencies(ProjectBlueprint bp) {
7680
return List.copyOf(deps);
7781
}
7882

83+
private boolean isSampleEnabled(ProjectBlueprint bp) {
84+
var arch = bp.getArchitecture();
85+
return arch != null && arch.sampleCodeOptions() != null && arch.sampleCodeOptions().isEnabled();
86+
}
87+
88+
private boolean isWebSelected(ProjectBlueprint bp) {
89+
var deps = bp.getDependencies();
90+
return deps != null
91+
&& !deps.isEmpty()
92+
&& deps.asList().stream().anyMatch(MavenPomBuildModel.WEB_STARTER_FEATURE.matches());
93+
}
94+
7995
private boolean isJpaSelected(ProjectBlueprint bp) {
8096
var deps = bp.getDependencies();
8197
return deps != null

src/main/java/io/github/blueprintplatform/codegen/adapter/out/profile/springboot/maven/java/build/MavenPomBuildModel.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ final class MavenPomBuildModel {
1313
static final String KEY_PROJECT_NAME = "projectName";
1414
static final String KEY_PROJECT_DESCRIPTION = "projectDescription";
1515
static final String KEY_POM_PROPERTIES = "pomProperties";
16+
1617
static final String ARCH_UNIT_VERSION_KEY = "archunit.version";
1718
static final String ARCH_UNIT_VERSION = "1.4.1";
1819
static final BuildDependency H2_DB = BuildDependency.of("com.h2database", "h2", null, null);
@@ -22,10 +23,18 @@ final class MavenPomBuildModel {
2223
private static final String SPRING_BOOT_GROUP_ID = "org.springframework.boot";
2324
static final DependencyFeature JPA_STARTER =
2425
new DependencyFeature("jpa", SPRING_BOOT_GROUP_ID, "spring-boot-starter-data-jpa");
26+
27+
static final DependencyFeature WEB_STARTER_FEATURE =
28+
new DependencyFeature("web", SPRING_BOOT_GROUP_ID, "spring-boot-starter-web");
29+
2530
static final BuildDependency CORE_STARTER =
2631
BuildDependency.of(SPRING_BOOT_GROUP_ID, "spring-boot-starter");
32+
2733
static final BuildDependency TEST_STARTER =
2834
BuildDependency.of(SPRING_BOOT_GROUP_ID, "spring-boot-starter-test", null, "test");
2935

36+
static final BuildDependency WEB_STARTER =
37+
BuildDependency.of(SPRING_BOOT_GROUP_ID, "spring-boot-starter-web");
38+
3039
private MavenPomBuildModel() {}
3140
}

src/test/java/io/github/blueprintplatform/codegen/adapter/out/profile/springboot/maven/java/build/MavenPomBuildConfigurationAdapterTest.java

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,30 @@ class MavenPomBuildConfigurationAdapterTest {
5252
private static final String BASE_PATH = "springboot/maven/java/";
5353

5454
private static ProjectBlueprint blueprint(
55-
ArchitectureGovernance governance, Dependencies dependencies) {
55+
ArchitectureGovernance governance, Dependencies dependencies, SampleCodeOptions sample) {
56+
5657
ProjectMetadata metadata =
57-
new ProjectMetadata(
58-
new ProjectIdentity(new GroupId("com.acme"), new ArtifactId("demo-app")),
59-
new ProjectName("Demo App"),
60-
new ProjectDescription("Sample Project"),
61-
new PackageName("com.acme.demo"));
58+
new ProjectMetadata(
59+
new ProjectIdentity(new GroupId("com.acme"), new ArtifactId("demo-app")),
60+
new ProjectName("Demo App"),
61+
new ProjectDescription("Sample Project"),
62+
new PackageName("com.acme.demo"));
6263

6364
TechStack techStack = new TechStack(Framework.SPRING_BOOT, BuildTool.MAVEN, Language.JAVA);
6465
PlatformTarget target = new SpringBootJvmTarget(JavaVersion.JAVA_21, SpringBootVersion.V3_5);
6566
PlatformSpec platform = new PlatformSpec(techStack, target);
6667

6768
ArchitectureSpec architecture =
68-
new ArchitectureSpec(ProjectLayout.STANDARD, governance, SampleCodeOptions.none());
69+
new ArchitectureSpec(ProjectLayout.STANDARD, governance, sample);
6970

7071
return ProjectBlueprint.of(metadata, platform, architecture, dependencies);
7172
}
7273

7374
private static Dependency dep(String groupId, String artifactId) {
7475
return new Dependency(
75-
new DependencyCoordinates(new GroupId(groupId), new ArtifactId(artifactId)),
76-
new DependencyVersion("1.0.0"),
77-
DependencyScope.RUNTIME);
76+
new DependencyCoordinates(new GroupId(groupId), new ArtifactId(artifactId)),
77+
new DependencyVersion("1.0.0"),
78+
DependencyScope.RUNTIME);
7879
}
7980

8081
@Test
@@ -106,7 +107,8 @@ void generate_shouldBuildModel_withoutJpa_withoutGovernance() {
106107

107108
ProjectBlueprint bp =
108109
blueprint(
109-
ArchitectureGovernance.none(), Dependencies.of(List.of(dep("org.acme", "custom-dep"))));
110+
ArchitectureGovernance.none(),
111+
Dependencies.of(List.of(dep("org.acme", "custom-dep"))), SampleCodeOptions.none());
110112

111113
renderer.nextFile =
112114
new GeneratedTextResource(Path.of("pom.xml"), "<project/>", StandardCharsets.UTF_8);
@@ -139,6 +141,42 @@ void generate_shouldBuildModel_withoutJpa_withoutGovernance() {
139141
assertThat(deps.get(2)).isEqualTo(MavenPomBuildModel.TEST_STARTER);
140142
}
141143

144+
@Test
145+
@DisplayName("generate() should add WEB starter when sample code is enabled and web is not selected")
146+
void generate_shouldAddWeb_whenSampleEnabled_andWebNotSelected() {
147+
CapturingTemplateRenderer renderer = new CapturingTemplateRenderer();
148+
149+
List<BuildDependency> mapped =
150+
List.of(BuildDependency.of("org.acme", "custom-dep", "1.0.0", "runtime"));
151+
RecordingBuildDependencyMapper mapper = new RecordingBuildDependencyMapper(mapped);
152+
153+
ArtifactSpec artifactSpec =
154+
new ArtifactSpec(BASE_PATH, List.of(new TemplateSpec("pom.ftl", "pom.xml")));
155+
MavenPomBuildConfigurationAdapter adapter =
156+
new MavenPomBuildConfigurationAdapter(renderer, artifactSpec, mapper);
157+
158+
ProjectBlueprint bp =
159+
blueprint(
160+
ArchitectureGovernance.none(),
161+
Dependencies.of(List.of(dep("org.acme", "custom-dep"))),
162+
SampleCodeOptions.basic());
163+
164+
renderer.nextFile =
165+
new GeneratedTextResource(Path.of("pom.xml"), "<project/>", StandardCharsets.UTF_8);
166+
167+
adapter.generate(bp);
168+
169+
@SuppressWarnings("unchecked")
170+
List<BuildDependency> deps =
171+
(List<BuildDependency>) renderer.capturedModel.get(MavenPomBuildModel.KEY_DEPENDENCIES);
172+
173+
assertThat(deps).hasSize(4);
174+
assertThat(deps.get(0)).isEqualTo(MavenPomBuildModel.CORE_STARTER);
175+
assertThat(deps.get(1)).isSameAs(mapped.getFirst());
176+
assertThat(deps.get(2)).isEqualTo(MavenPomBuildModel.WEB_STARTER);
177+
assertThat(deps.get(3)).isEqualTo(MavenPomBuildModel.TEST_STARTER);
178+
}
179+
142180
@Test
143181
@DisplayName("generate() should add H2 dependency when JPA starter is selected")
144182
void generate_shouldAddH2_whenJpaSelected() {
@@ -164,7 +202,7 @@ void generate_shouldAddH2_whenJpaSelected() {
164202
ProjectBlueprint bp =
165203
blueprint(
166204
ArchitectureGovernance.none(),
167-
Dependencies.of(List.of(dep("org.acme", "custom-dep"), jpaStarter)));
205+
Dependencies.of(List.of(dep("org.acme", "custom-dep"), jpaStarter)), SampleCodeOptions.none());
168206

169207
renderer.nextFile =
170208
new GeneratedTextResource(Path.of("pom.xml"), "<project/>", StandardCharsets.UTF_8);
@@ -213,7 +251,7 @@ void generate_shouldAddArchUnit_whenGovernanceEnabled() {
213251
ProjectBlueprint bp =
214252
blueprint(
215253
ArchitectureGovernance.basic(),
216-
Dependencies.of(List.of(dep("org.acme", "custom-dep"), jpaStarter)));
254+
Dependencies.of(List.of(dep("org.acme", "custom-dep"), jpaStarter)), SampleCodeOptions.none());
217255

218256
renderer.nextFile =
219257
new GeneratedTextResource(Path.of("pom.xml"), "<project/>", StandardCharsets.UTF_8);
@@ -239,4 +277,5 @@ void generate_shouldAddArchUnit_whenGovernanceEnabled() {
239277
assertThat(deps.get(3)).isEqualTo(MavenPomBuildModel.ARCH_UNIT_TEST);
240278
assertThat(deps.get(4)).isEqualTo(MavenPomBuildModel.TEST_STARTER);
241279
}
280+
242281
}

0 commit comments

Comments
 (0)