Skip to content

Commit a961c52

Browse files
committed
Add support for configuring a Maven parent relative path
Closes gh-1296
1 parent 5a4e7fa commit a961c52

File tree

9 files changed

+99
-18
lines changed

9 files changed

+99
-18
lines changed

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/build/maven/DefaultMavenBuildCustomizer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -64,7 +64,8 @@ public void customize(MavenBuild build) {
6464
build.properties().property("project.build.sourceEncoding", "UTF-8")
6565
.property("project.reporting.outputEncoding", "UTF-8");
6666
}
67-
build.settings().parent(parentPom.getGroupId(), parentPom.getArtifactId(), parentPom.getVersion());
67+
build.settings().parent(parentPom.getGroupId(), parentPom.getArtifactId(), parentPom.getVersion(),
68+
parentPom.getRelativePath());
6869
}
6970

7071
private boolean hasBom(MavenBuild build, BillOfMaterials bom) {

initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/build/maven/DefaultMavenBuildCustomizerTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,17 +76,19 @@ void customizeWhenNoParentShouldUseSpringBootParent() {
7676
assertThat(parent.getGroupId()).isEqualTo("org.springframework.boot");
7777
assertThat(parent.getArtifactId()).isEqualTo("spring-boot-starter-parent");
7878
assertThat(parent.getVersion()).isEqualTo("2.0.0");
79+
assertThat(parent.getRelativePath()).isEmpty();
7980
}
8081

8182
@Test
8283
void customizeWithCustomParentAndSpringBootBomShouldAddBom() {
8384
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
84-
.setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", true).build();
85+
.setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", "../pom.xml", true).build();
8586
MavenBuild build = customizeBuild(metadata);
8687
MavenParent parent = build.getSettings().getParent();
8788
assertThat(parent.getGroupId()).isEqualTo("com.foo");
8889
assertThat(parent.getArtifactId()).isEqualTo("foo-parent");
8990
assertThat(parent.getVersion()).isEqualTo("1.0.0-SNAPSHOT");
91+
assertThat(parent.getRelativePath()).isEqualTo("../pom.xml");
9092
BomContainer boms = build.boms();
9193
assertThat(boms.items()).hasSize(1);
9294
assertThat(boms.ids()).contains("spring-boot");
@@ -97,7 +99,7 @@ void customizeWithCustomParentAndSpringBootBomShouldAddBom() {
9799
@Test
98100
void customizeWithNoSpringBootBomShouldNotAddBom() {
99101
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
100-
.setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", false).build();
102+
.setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", null, false).build();
101103
MavenBuild build = customizeBuild(metadata);
102104
BomContainer boms = build.boms();
103105
assertThat(boms.items()).hasSize(0);

initializr-generator-test/src/main/java/io/spring/initializr/generator/test/InitializrMetadataTestBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -219,12 +219,13 @@ public InitializrMetadataTestBuilder setKotlinEnv(String defaultKotlinVersion, K
219219
}
220220

221221
public InitializrMetadataTestBuilder setMavenParent(String groupId, String artifactId, String version,
222-
boolean includeSpringBootBom) {
222+
String relativePath, boolean includeSpringBootBom) {
223223
this.builder.withCustomizer((it) -> {
224224
ParentPom parent = it.getConfiguration().getEnv().getMaven().getParent();
225225
parent.setGroupId(groupId);
226226
parent.setArtifactId(artifactId);
227227
parent.setVersion(version);
228+
parent.setRelativePath(relativePath);
228229
parent.setIncludeSpringBootBom(includeSpringBootBom);
229230
});
230231
return this;

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildSettings.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -203,14 +203,30 @@ public Builder coordinates(String groupId, String artifactId) {
203203
}
204204

205205
/**
206-
* Set the coordinates of the parent.
206+
* Set the coordinates of the parent, to be resolved against the repository.
207207
* @param groupId the groupID of the parent
208208
* @param artifactId the artifactID of the parent
209209
* @param version the version of the parent
210210
* @return this for method chaining
211+
* @see #parent(String, String, String, String)
211212
*/
212213
public Builder parent(String groupId, String artifactId, String version) {
213-
this.parent = new MavenParent(groupId, artifactId, version);
214+
return parent(groupId, artifactId, version, "");
215+
}
216+
217+
/**
218+
* Set the coordinates of the parent and its relative path. The relative path can
219+
* be set to {@code null} to let Maven search the parent using local file search,
220+
* for instance {@code pom.xml} in the parent directory. It can also be set to an
221+
* empty string to specify that it should be resolved against the repository.
222+
* @param groupId the groupID of the parent
223+
* @param artifactId the artifactID of the parent
224+
* @param version the version of the parent
225+
* @param relativePath the relative path
226+
* @return this for method chaining
227+
*/
228+
public Builder parent(String groupId, String artifactId, String version, String relativePath) {
229+
this.parent = new MavenParent(groupId, artifactId, version, relativePath);
214230
return self();
215231
}
216232

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -120,7 +120,15 @@ private void writeParent(IndentingWriter writer, MavenBuild build) {
120120
writeSingleElement(writer, "groupId", parent.getGroupId());
121121
writeSingleElement(writer, "artifactId", parent.getArtifactId());
122122
writeSingleElement(writer, "version", parent.getVersion());
123-
writer.println("<relativePath/> <!-- lookup parent from repository -->");
123+
String relativePath = parent.getRelativePath();
124+
if (relativePath != null) {
125+
if (StringUtils.hasText(relativePath)) {
126+
writeSingleElement(writer, "relativePath", relativePath);
127+
}
128+
else {
129+
writer.println("<relativePath/> <!-- lookup parent from repository -->");
130+
}
131+
}
124132
});
125133
writer.println("</parent>");
126134
}

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenParent.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,10 +29,13 @@ public class MavenParent {
2929

3030
private final String version;
3131

32-
MavenParent(String groupId, String artifactId, String version) {
32+
private final String relativePath;
33+
34+
MavenParent(String groupId, String artifactId, String version, String relativePath) {
3335
this.groupId = groupId;
3436
this.artifactId = artifactId;
3537
this.version = version;
38+
this.relativePath = relativePath;
3639
}
3740

3841
/**
@@ -59,4 +62,12 @@ public String getVersion() {
5962
return this.version;
6063
}
6164

65+
/**
66+
* Return the relative path of this parent.
67+
* @return the relative path of this parent or {@code null}.
68+
*/
69+
public String getRelativePath() {
70+
return this.relativePath;
71+
}
72+
6273
}

initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -78,6 +78,33 @@ void pomWithParent() {
7878
assertThat(pom).textAtPath("/project/parent/groupId").isEqualTo("org.springframework.boot");
7979
assertThat(pom).textAtPath("/project/parent/artifactId").isEqualTo("spring-boot-starter-parent");
8080
assertThat(pom).textAtPath("/project/parent/version").isEqualTo("2.1.0.RELEASE");
81+
assertThat(pom).textAtPath("/project/parent/relativePath").isEmpty();
82+
});
83+
}
84+
85+
@Test
86+
void pomWithParentAndRelativePath() {
87+
MavenBuild build = new MavenBuild();
88+
build.settings().coordinates("com.example.demo", "demo").parent("org.springframework.boot",
89+
"spring-boot-starter-parent", "2.1.0.RELEASE", "../parent/pom.xml");
90+
generatePom(build, (pom) -> {
91+
assertThat(pom).textAtPath("/project/parent/groupId").isEqualTo("org.springframework.boot");
92+
assertThat(pom).textAtPath("/project/parent/artifactId").isEqualTo("spring-boot-starter-parent");
93+
assertThat(pom).textAtPath("/project/parent/version").isEqualTo("2.1.0.RELEASE");
94+
assertThat(pom).textAtPath("/project/parent/relativePath").isEqualTo("../parent/pom.xml");
95+
});
96+
}
97+
98+
@Test
99+
void pomWithParentAndNullRelativePath() {
100+
MavenBuild build = new MavenBuild();
101+
build.settings().coordinates("com.example.demo", "demo").parent("org.springframework.boot",
102+
"spring-boot-starter-parent", "2.1.0.RELEASE", null);
103+
generatePom(build, (pom) -> {
104+
assertThat(pom).textAtPath("/project/parent/groupId").isEqualTo("org.springframework.boot");
105+
assertThat(pom).textAtPath("/project/parent/artifactId").isEqualTo("spring-boot-starter-parent");
106+
assertThat(pom).textAtPath("/project/parent/version").isEqualTo("2.1.0.RELEASE");
107+
assertThat(pom).nodeAtPath("/project/parent/relativePath").isNull();
81108
});
82109
}
83110

initializr-metadata/src/main/java/io/spring/initializr/metadata/InitializrConfiguration.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -542,7 +542,7 @@ private void merge(Maven other) {
542542
*/
543543
public ParentPom resolveParentPom(String bootVersion) {
544544
return (StringUtils.hasText(this.parent.groupId) ? this.parent
545-
: new ParentPom(DEFAULT_PARENT_GROUP_ID, DEFAULT_PARENT_ARTIFACT_ID, bootVersion));
545+
: new ParentPom(DEFAULT_PARENT_GROUP_ID, DEFAULT_PARENT_ARTIFACT_ID, bootVersion, ""));
546546
}
547547

548548
/**
@@ -577,15 +577,21 @@ public static class ParentPom {
577577
*/
578578
private String version;
579579

580+
/**
581+
* Parent relative path.
582+
*/
583+
private String relativePath = "";
584+
580585
/**
581586
* Add the "spring-boot-dependencies" BOM to the project.
582587
*/
583588
private boolean includeSpringBootBom;
584589

585-
public ParentPom(String groupId, String artifactId, String version) {
590+
public ParentPom(String groupId, String artifactId, String version, String relativePath) {
586591
this.groupId = groupId;
587592
this.artifactId = artifactId;
588593
this.version = version;
594+
this.relativePath = relativePath;
589595
}
590596

591597
public ParentPom() {
@@ -615,6 +621,14 @@ public void setVersion(String version) {
615621
this.version = version;
616622
}
617623

624+
public String getRelativePath() {
625+
return this.relativePath;
626+
}
627+
628+
public void setRelativePath(String relativePath) {
629+
this.relativePath = relativePath;
630+
}
631+
618632
public boolean isIncludeSpringBootBom() {
619633
return this.includeSpringBootBom;
620634
}

initializr-web/src/test/resources/metadata/config/test-default.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"groupId": null,
5555
"artifactId": null,
5656
"version": null,
57+
"relativePath": "",
5758
"includeSpringBootBom": false
5859
}
5960
},

0 commit comments

Comments
 (0)