Skip to content

Commit c57596c

Browse files
committed
Enhance PomBuilder
- Add method to add compile scope deps in PomBuilder - Add method create root pom with parent - Fix method name
1 parent ca71660 commit c57596c

File tree

6 files changed

+76
-32
lines changed

6 files changed

+76
-32
lines changed

components/sbm-core/src/test/java/org/springframework/sbm/build/api/Module_contains_Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Module_contains_Test {
3030
@Test
3131
void singleModuleProject() {
3232
String rootPom = PomBuilder
33-
.buiildPom("com.example:parent:1.0")
33+
.buildPom("com.example:parent:1.0")
3434
.type("jar")
3535
.withModules("module1", "module2")
3636
.build();
@@ -66,19 +66,19 @@ public class SomeClassTest {}
6666
@Test
6767
void multiModuleProject() {
6868
String rootPom = PomBuilder
69-
.buiildPom("com.example:parent:1.0")
69+
.buildPom("com.example:parent:1.0")
7070
.type("pom")
7171
.withModules("module1", "module2")
7272
.build();
7373

7474
String module1Pom = PomBuilder
75-
.buiildPom("com.example:parent:1.0", "module1")
75+
.buildPom("com.example:parent:1.0", "module1")
7676
.unscopedDependencies("com.example:module2:1.0")
7777
.build();
7878

79-
String module2Pom = PomBuilder.buiildPom("com.example:parent:1.0", "module2").build();
79+
String module2Pom = PomBuilder.buildPom("com.example:parent:1.0", "module2").build();
8080

81-
String moduleInModule1Pom = PomBuilder.buiildPom("com.example:parent:1.0", "module-in-module1").build();
81+
String moduleInModule1Pom = PomBuilder.buildPom("com.example:parent:1.0", "module-in-module1").build();
8282

8383

8484
String javaClass = """

components/sbm-core/src/test/java/org/springframework/sbm/build/impl/RewriteMavenParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void noExecutionContextGiven() {
4646

4747
@Test
4848
void customExecutionContextGiven() {
49-
String pom = PomBuilder.buiildPom("com.example:project:1.0").build();
49+
String pom = PomBuilder.buildPom("com.example:project:1.0").build();
5050
ExecutionContext ctx = new RewriteExecutionContext();
5151
sut.parse(ctx, pom);
5252
// first time when initializing the parser

components/sbm-core/src/test/java/org/springframework/sbm/build/util/PomBuilder.java

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.sbm.build.util;
1818

1919
import org.openrewrite.maven.tree.Scope;
20-
import org.springframework.sbm.build.api.Dependency;
2120
import org.springframework.sbm.project.parser.DependencyHelper;
2221

2322
import java.util.*;
@@ -34,20 +33,39 @@ public class PomBuilder {
3433
private Map<Scope, org.openrewrite.maven.tree.Dependency> dependencies = new LinkedHashMap<Scope, org.openrewrite.maven.tree.Dependency>();
3534

3635
private DependencyHelper dependencyHelper = new DependencyHelper();
36+
private String parentPom;
3737

38-
public static PomBuilder buiildPom(String coordinate) {
38+
public static PomBuilder buildPom(String coordinate) {
3939
PomBuilder pomBuilder = new PomBuilder();
4040
pomBuilder.coordinate = coordinate;
4141
return pomBuilder;
4242
}
4343

44-
public static PomBuilder buiildPom(String parent, String artifactId) {
44+
public static PomBuilder buildPom(String parentCoordinate, String artifactId) {
4545
PomBuilder pomBuilder = new PomBuilder();
46-
pomBuilder.parent = parent;
46+
pomBuilder.parent = parentCoordinate;
4747
pomBuilder.artifactId = artifactId;
4848
return pomBuilder;
4949
}
5050

51+
/**
52+
* Build a parent pom file with a parent, e.g. spring-boot-starter-parent
53+
*
54+
* @param parentCoordinate
55+
* @param coordinate
56+
*/
57+
public static PomBuilder buildParentPom(String parentCoordinate, String coordinate) {
58+
PomBuilder pomBuilder = new PomBuilder();
59+
pomBuilder.parentPom = parentCoordinate;
60+
pomBuilder.coordinate = coordinate;
61+
return pomBuilder;
62+
}
63+
64+
/**
65+
* Add modules to a pom.
66+
*
67+
* @param moduleArtifactNames one or more module artifactIds
68+
*/
5169
public PomBuilder withModules(String... moduleArtifactNames) {
5270
this.modules = Arrays.asList(moduleArtifactNames);
5371
if(this.modules.stream().anyMatch(m -> m.contains(":"))) throw new RuntimeException("Found ':' in artifact name but artifact names of modules must not be provided as coordinate.");
@@ -62,6 +80,10 @@ public String build() {
6280
<modelVersion>4.0.0</modelVersion>
6381
""");
6482

83+
if(parentPom != null && parent != null) {
84+
throw new IllegalStateException("parentPom and parent were set.");
85+
}
86+
6587
if (parent != null) {
6688
String[] coord = parent.split(":");
6789
sb.append(" <parent>").append("\n");
@@ -70,7 +92,14 @@ public String build() {
7092
sb.append(" <version>").append(coord[2]).append("</version>").append("\n");
7193
sb.append(" </parent>").append("\n");
7294
sb.append(" <artifactId>").append(artifactId).append("</artifactId>").append("\n");
73-
} else {
95+
} else if (parentPom != null) {
96+
String[] coord = parentPom.split(":");
97+
sb.append(" <parent>").append("\n");
98+
sb.append(" <groupId>").append(coord[0]).append("</groupId>").append("\n");
99+
sb.append(" <artifactId>").append(coord[1]).append("</artifactId>").append("\n");
100+
sb.append(" <version>").append(coord[2]).append("</version>").append("\n");
101+
sb.append(" </parent>").append("\n");
102+
} if (parent == null){
74103
String[] coord = coordinate.split(":");
75104
sb.append(" <groupId>").append(coord[0]).append("</groupId>").append("\n");
76105
sb.append(" <artifactId>").append(coord[1]).append("</artifactId>").append("\n");
@@ -139,14 +168,16 @@ private void renderDependency(StringBuilder dependenciesSection, Scope scope, or
139168
.append(dependency.getArtifactId())
140169
.append("</artifactId>")
141170
.append("\n");
142-
dependenciesSection
143-
.append(" ")
144-
.append(" ")
145-
.append(" ")
146-
.append("<version>")
147-
.append(dependency.getVersion())
148-
.append("</version>")
149-
.append("\n");
171+
if(dependency.getVersion() != null) {
172+
dependenciesSection
173+
.append(" ")
174+
.append(" ")
175+
.append(" ")
176+
.append("<version>")
177+
.append(dependency.getVersion())
178+
.append("</version>")
179+
.append("\n");
180+
}
150181
if(scope != Scope.None) {
151182
dependenciesSection
152183
.append(" ")
@@ -176,6 +207,13 @@ public PomBuilder unscopedDependencies(String... coordinates) {
176207
return this;
177208
}
178209

210+
public PomBuilder compileScopeDependencies(String... coordinates) {
211+
dependencyHelper.mapCoordinatesToDependencies(Arrays.asList(coordinates))
212+
.stream()
213+
.forEach(c -> this.dependencies.put(Scope.Compile, c));
214+
return this;
215+
}
216+
179217
public PomBuilder testScopeDependencies(String... coordinates) {
180218
dependencyHelper.mapCoordinatesToDependencies(Arrays.asList(coordinates))
181219
.stream()

components/sbm-core/src/test/java/org/springframework/sbm/project/buildfile/OpenRewriteMavenBuildFileTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@
1818
import org.intellij.lang.annotations.Language;
1919
import org.jetbrains.annotations.NotNull;
2020
import org.junit.jupiter.api.*;
21-
import org.junit.jupiter.api.io.TempDir;
2221
import org.mockito.ArgumentCaptor;
2322
import org.mockito.Mockito;
2423
import org.springframework.context.ApplicationEventPublisher;
2524
import org.springframework.sbm.build.api.BuildFile;
2625
import org.springframework.sbm.build.api.DependenciesChangedEvent;
2726
import org.springframework.sbm.build.api.Dependency;
28-
import org.springframework.sbm.build.api.Module;
2927
import org.springframework.sbm.build.api.Plugin;
3028
import org.springframework.sbm.build.util.PomBuilder;
3129
import org.springframework.sbm.engine.context.ProjectContext;
@@ -907,7 +905,7 @@ private BuildFile getBuildFileByPackagingType(ProjectContext context, String ear
907905
@Nested
908906
class GetDependenciesMultiModuleTest {
909907
String parentPom = PomBuilder
910-
.buiildPom("com.example:parent:1.0")
908+
.buildPom("com.example:parent:1.0")
911909
.withProperties(Map.of(
912910
"jakarta.version", "3.0.2",
913911
"validation.groupId", "jakarta.validation",
@@ -918,13 +916,13 @@ class GetDependenciesMultiModuleTest {
918916
.build();
919917

920918
String module1Pom = PomBuilder
921-
.buiildPom("com.example:parent:1.0", "module1")
919+
.buildPom("com.example:parent:1.0", "module1")
922920
.unscopedDependencies("com.example:module2:${project.version}")
923921
.testScopeDependencies("javax.annotation:${annotationApi.artifactId}:1.3.2")
924922
.build();
925923

926924
String module2Pom = PomBuilder
927-
.buiildPom("com.example:parent:1.0", "module2")
925+
.buildPom("com.example:parent:1.0", "module2")
928926
.unscopedDependencies("${validation.groupId}:jakarta.validation-api:${jakarta.version}")
929927
.build();
930928

components/sbm-recipes-boot-upgrade/src/main/java/org/springframework/sbm/boot/upgrade_27_30/report/SpringBootUpgradeReportSection.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
import lombok.Setter;
2525
import org.jetbrains.annotations.NotNull;
2626
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.sbm.build.api.BuildFile;
2728
import org.springframework.sbm.engine.context.ProjectContext;
2829
import org.springframework.sbm.engine.recipe.Condition;
29-
import org.stringtemplate.v4.ST;
3030

3131
import javax.validation.constraints.NotEmpty;
3232
import java.io.IOException;
@@ -51,14 +51,24 @@ public class SpringBootUpgradeReportSection {
5151

5252
/**
5353
* Helper acting as {@link Condition} and data provide for a {@link SpringBootUpgradeReportSection}.
54+
* @deprecated Use {@link AbstractHelper} instead
5455
*/
56+
@Deprecated(forRemoval = true)
5557
public interface Helper<T> extends Condition {
5658
/**
5759
* @return {@code Map<String, T>} the model data for the template.
5860
*/
5961
Map<String, T> getData();
6062
}
6163

64+
public static abstract class AbstractHelper<T> implements Helper<T> {
65+
66+
@Override
67+
public String getDescription() {
68+
return "";
69+
}
70+
}
71+
6272
public static final String CHANGE_HEADER = "What Changed";
6373
public static final String AFFECTED = "Why is the application affected";
6474
public static final String REMEDIATION = "Remediation";
@@ -99,7 +109,7 @@ public boolean shouldRender(ProjectContext context) {
99109
private Set<String> contributors;
100110

101111
@JsonIgnore
102-
private Helper helper;
112+
private Helper<Object> helper;
103113
@JsonIgnore
104114
@Autowired
105115
private SpringBootUpgradeReportFreemarkerSupport freemarkerSupport;

components/sbm-recipes-boot-upgrade/src/test/java/org/springframework/sbm/boot/upgrade_27_30/RemoveImageBannerTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.springframework.sbm.boot.upgrade_27_30.report.SpringBootUpgradeReportActionDeserializer;
20-
import org.springframework.sbm.build.impl.MavenBuildFileUtil;
2120
import org.springframework.sbm.build.util.PomBuilder;
2221
import org.springframework.sbm.engine.context.ProjectContext;
23-
import org.springframework.sbm.engine.recipe.OpenRewriteDeclarativeRecipeAdapterTest;
2422
import org.springframework.sbm.engine.recipe.Recipe;
2523
import org.springframework.sbm.project.resource.TestProjectContext;
2624
import org.springframework.sbm.test.RecipeTestSupport;
@@ -45,12 +43,12 @@ void testGlobExpression() {
4543
@Test
4644
void applyRemoveImageBannerRecipeShouldRemoveAllImageBannerAtDefaultLocation() {
4745
String parentPom = PomBuilder
48-
.buiildPom("com.example:parent:1.0")
46+
.buildPom("com.example:parent:1.0")
4947
.withModules("moduleA", "moduleB", "moduleC")
5048
.build();
51-
String moduleA = PomBuilder.buiildPom("com.example:parent:1.0", "moduleA").build();
52-
String moduleB = PomBuilder.buiildPom("com.example:parent:1.0", "moduleB").build();
53-
String moduleC = PomBuilder.buiildPom("com.example:parent:1.0", "moduleC").build();
49+
String moduleA = PomBuilder.buildPom("com.example:parent:1.0", "moduleA").build();
50+
String moduleB = PomBuilder.buildPom("com.example:parent:1.0", "moduleB").build();
51+
String moduleC = PomBuilder.buildPom("com.example:parent:1.0", "moduleC").build();
5452

5553
ProjectContext context = TestProjectContext.buildProjectContext()
5654
.withMavenBuildFileSource("pom.xml", parentPom)

0 commit comments

Comments
 (0)