@@ -249,6 +249,9 @@ public static class Builder {
249
249
private OpenRewriteMavenBuildFile mockedBuildFile ;
250
250
private DependencyHelper dependencyHelper = new DependencyHelper ();
251
251
private SbmApplicationProperties sbmApplicationProperties = new SbmApplicationProperties ();
252
+
253
+ private Optional <String > springVersion = Optional .empty ();
254
+
252
255
private JavaParser javaParser ;
253
256
254
257
public Builder (Path projectRoot ) {
@@ -456,10 +459,22 @@ public ProjectContext serializeProjectContext(Path targetDir) {
456
459
public ProjectContext build () {
457
460
verifyValidBuildFileSetup ();
458
461
459
- if (dependencies != null && !dependencies .isEmpty ()) {
460
- String generatedPomXml = renderPomXmlWithGivenDependencies ();
461
- resourcesWithRelativePaths .put (Path .of ("pom.xml" ), generatedPomXml );
462
- }
462
+ String xml = "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
463
+ "<project xmlns=\" http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" >\n " +
464
+ " <modelVersion>4.0.0</modelVersion>\n " +
465
+ "{{springParentPom}}" +
466
+ " <groupId>com.example</groupId>\n " +
467
+ " <artifactId>dummy-root</artifactId>\n " +
468
+ " <version>0.1.0-SNAPSHOT</version>\n " +
469
+ " <packaging>jar</packaging>\n " +
470
+ "{{dependencies}}" +
471
+ "</project>\n " ;
472
+
473
+ xml = xml
474
+ .replace ("{{dependencies}}" , getDependenciesSection ())
475
+ .replace ("{{springParentPom}}" , getSpringParentPomSection ());
476
+
477
+ resourcesWithRelativePaths .put (Path .of ("pom.xml" ), xml );
463
478
464
479
if (!containsAnyPomXml ()) {
465
480
withDummyRootBuildFile ();
@@ -588,22 +603,9 @@ private Parser.Input createParserInput(Path path, String value) {
588
603
589
604
590
605
@ NotNull
591
- private String renderPomXmlWithGivenDependencies () {
592
- String xml = "<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n " +
593
- "<project xmlns=\" http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\" http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" >\n " +
594
- " <modelVersion>4.0.0</modelVersion>\n " +
595
- " <groupId>com.example</groupId>\n " +
596
- " <artifactId>dummy-root</artifactId>\n " +
597
- " <version>0.1.0-SNAPSHOT</version>\n " +
598
- " <packaging>jar</packaging>\n " +
599
- "{{dependencies}}\n " +
600
- "</project>\n " ;
601
-
602
- String dependenciesText = null ;
603
- if (dependencies .isEmpty ()) {
604
- dependenciesText = "" ;
605
- } else {
606
- StringBuilder dependenciesSection = new StringBuilder ();
606
+ private String getDependenciesSection () {
607
+ StringBuilder dependenciesSection = new StringBuilder ();
608
+ if (!dependencies .isEmpty ()) {
607
609
dependenciesSection .append (" " ).append ("<dependencies>" ).append ("\n " );
608
610
dependencyHelper .mapCoordinatesToDependencies (dependencies ).stream ().forEach (dependency -> {
609
611
dependenciesSection .append (" " ).append (" " ).append ("<dependency>" ).append ("\n " );
@@ -613,11 +615,32 @@ private String renderPomXmlWithGivenDependencies() {
613
615
dependenciesSection .append (" " ).append (" " ).append ("</dependency>" ).append ("\n " );
614
616
});
615
617
dependenciesSection .append (" " ).append ("</dependencies>" ).append ("\n " );
616
- dependenciesText = dependenciesSection .toString ();
617
618
}
618
619
619
- String buildFileSource = xml .replace ("{{dependencies}}" , dependenciesText );
620
- return buildFileSource ;
620
+ return dependenciesSection .toString ();
621
+ }
622
+
623
+ @ NotNull
624
+ private String getSpringParentPomSection () {
625
+
626
+ if (this .springVersion .isPresent ()) {
627
+ return """
628
+ <parent>
629
+ <groupId>org.springframework.boot</groupId>
630
+ <artifactId>spring-boot-starter-parent</artifactId>
631
+ <version>%s</version>
632
+ <relativePath/>
633
+ </parent>
634
+ """ .formatted (this .springVersion .get ());
635
+ }
636
+
637
+ return "" ;
638
+ }
639
+
640
+ public Builder withSpringBootParentOf (String springVersion ) {
641
+
642
+ this .springVersion = Optional .of (springVersion );
643
+ return this ;
621
644
}
622
645
}
623
646
0 commit comments