Add support for configuring a Maven parent relative path

Closes gh-1296
This commit is contained in:
Stephane Nicoll
2022-01-22 11:20:06 +01:00
parent 5a4e7fac95
commit a961c52450
9 changed files with 99 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -64,7 +64,8 @@ public class DefaultMavenBuildCustomizer implements BuildCustomizer<MavenBuild>
build.properties().property("project.build.sourceEncoding", "UTF-8") build.properties().property("project.build.sourceEncoding", "UTF-8")
.property("project.reporting.outputEncoding", "UTF-8"); .property("project.reporting.outputEncoding", "UTF-8");
} }
build.settings().parent(parentPom.getGroupId(), parentPom.getArtifactId(), parentPom.getVersion()); build.settings().parent(parentPom.getGroupId(), parentPom.getArtifactId(), parentPom.getVersion(),
parentPom.getRelativePath());
} }
private boolean hasBom(MavenBuild build, BillOfMaterials bom) { private boolean hasBom(MavenBuild build, BillOfMaterials bom) {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -76,17 +76,19 @@ class DefaultMavenBuildCustomizerTests {
assertThat(parent.getGroupId()).isEqualTo("org.springframework.boot"); assertThat(parent.getGroupId()).isEqualTo("org.springframework.boot");
assertThat(parent.getArtifactId()).isEqualTo("spring-boot-starter-parent"); assertThat(parent.getArtifactId()).isEqualTo("spring-boot-starter-parent");
assertThat(parent.getVersion()).isEqualTo("2.0.0"); assertThat(parent.getVersion()).isEqualTo("2.0.0");
assertThat(parent.getRelativePath()).isEmpty();
} }
@Test @Test
void customizeWithCustomParentAndSpringBootBomShouldAddBom() { void customizeWithCustomParentAndSpringBootBomShouldAddBom() {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", true).build(); .setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", "../pom.xml", true).build();
MavenBuild build = customizeBuild(metadata); MavenBuild build = customizeBuild(metadata);
MavenParent parent = build.getSettings().getParent(); MavenParent parent = build.getSettings().getParent();
assertThat(parent.getGroupId()).isEqualTo("com.foo"); assertThat(parent.getGroupId()).isEqualTo("com.foo");
assertThat(parent.getArtifactId()).isEqualTo("foo-parent"); assertThat(parent.getArtifactId()).isEqualTo("foo-parent");
assertThat(parent.getVersion()).isEqualTo("1.0.0-SNAPSHOT"); assertThat(parent.getVersion()).isEqualTo("1.0.0-SNAPSHOT");
assertThat(parent.getRelativePath()).isEqualTo("../pom.xml");
BomContainer boms = build.boms(); BomContainer boms = build.boms();
assertThat(boms.items()).hasSize(1); assertThat(boms.items()).hasSize(1);
assertThat(boms.ids()).contains("spring-boot"); assertThat(boms.ids()).contains("spring-boot");
@@ -97,7 +99,7 @@ class DefaultMavenBuildCustomizerTests {
@Test @Test
void customizeWithNoSpringBootBomShouldNotAddBom() { void customizeWithNoSpringBootBomShouldNotAddBom() {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", false).build(); .setMavenParent("com.foo", "foo-parent", "1.0.0-SNAPSHOT", null, false).build();
MavenBuild build = customizeBuild(metadata); MavenBuild build = customizeBuild(metadata);
BomContainer boms = build.boms(); BomContainer boms = build.boms();
assertThat(boms.items()).hasSize(0); assertThat(boms.items()).hasSize(0);

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -219,12 +219,13 @@ public class InitializrMetadataTestBuilder {
} }
public InitializrMetadataTestBuilder setMavenParent(String groupId, String artifactId, String version, public InitializrMetadataTestBuilder setMavenParent(String groupId, String artifactId, String version,
boolean includeSpringBootBom) { String relativePath, boolean includeSpringBootBom) {
this.builder.withCustomizer((it) -> { this.builder.withCustomizer((it) -> {
ParentPom parent = it.getConfiguration().getEnv().getMaven().getParent(); ParentPom parent = it.getConfiguration().getEnv().getMaven().getParent();
parent.setGroupId(groupId); parent.setGroupId(groupId);
parent.setArtifactId(artifactId); parent.setArtifactId(artifactId);
parent.setVersion(version); parent.setVersion(version);
parent.setRelativePath(relativePath);
parent.setIncludeSpringBootBom(includeSpringBootBom); parent.setIncludeSpringBootBom(includeSpringBootBom);
}); });
return this; return this;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -203,14 +203,30 @@ public class MavenBuildSettings extends BuildSettings {
} }
/** /**
* Set the coordinates of the parent. * Set the coordinates of the parent, to be resolved against the repository.
* @param groupId the groupID of the parent * @param groupId the groupID of the parent
* @param artifactId the artifactID of the parent * @param artifactId the artifactID of the parent
* @param version the version of the parent * @param version the version of the parent
* @return this for method chaining * @return this for method chaining
* @see #parent(String, String, String, String)
*/ */
public Builder parent(String groupId, String artifactId, String version) { public Builder parent(String groupId, String artifactId, String version) {
this.parent = new MavenParent(groupId, artifactId, version); return parent(groupId, artifactId, version, "");
}
/**
* Set the coordinates of the parent and its relative path. The relative path can
* be set to {@code null} to let Maven search the parent using local file search,
* for instance {@code pom.xml} in the parent directory. It can also be set to an
* empty string to specify that it should be resolved against the repository.
* @param groupId the groupID of the parent
* @param artifactId the artifactID of the parent
* @param version the version of the parent
* @param relativePath the relative path
* @return this for method chaining
*/
public Builder parent(String groupId, String artifactId, String version, String relativePath) {
this.parent = new MavenParent(groupId, artifactId, version, relativePath);
return self(); return self();
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -120,7 +120,15 @@ public class MavenBuildWriter {
writeSingleElement(writer, "groupId", parent.getGroupId()); writeSingleElement(writer, "groupId", parent.getGroupId());
writeSingleElement(writer, "artifactId", parent.getArtifactId()); writeSingleElement(writer, "artifactId", parent.getArtifactId());
writeSingleElement(writer, "version", parent.getVersion()); writeSingleElement(writer, "version", parent.getVersion());
writer.println("<relativePath/> <!-- lookup parent from repository -->"); String relativePath = parent.getRelativePath();
if (relativePath != null) {
if (StringUtils.hasText(relativePath)) {
writeSingleElement(writer, "relativePath", relativePath);
}
else {
writer.println("<relativePath/> <!-- lookup parent from repository -->");
}
}
}); });
writer.println("</parent>"); writer.println("</parent>");
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -29,10 +29,13 @@ public class MavenParent {
private final String version; private final String version;
MavenParent(String groupId, String artifactId, String version) { private final String relativePath;
MavenParent(String groupId, String artifactId, String version, String relativePath) {
this.groupId = groupId; this.groupId = groupId;
this.artifactId = artifactId; this.artifactId = artifactId;
this.version = version; this.version = version;
this.relativePath = relativePath;
} }
/** /**
@@ -59,4 +62,12 @@ public class MavenParent {
return this.version; return this.version;
} }
/**
* Return the relative path of this parent.
* @return the relative path of this parent or {@code null}.
*/
public String getRelativePath() {
return this.relativePath;
}
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -78,6 +78,33 @@ class MavenBuildWriterTests {
assertThat(pom).textAtPath("/project/parent/groupId").isEqualTo("org.springframework.boot"); assertThat(pom).textAtPath("/project/parent/groupId").isEqualTo("org.springframework.boot");
assertThat(pom).textAtPath("/project/parent/artifactId").isEqualTo("spring-boot-starter-parent"); assertThat(pom).textAtPath("/project/parent/artifactId").isEqualTo("spring-boot-starter-parent");
assertThat(pom).textAtPath("/project/parent/version").isEqualTo("2.1.0.RELEASE"); assertThat(pom).textAtPath("/project/parent/version").isEqualTo("2.1.0.RELEASE");
assertThat(pom).textAtPath("/project/parent/relativePath").isEmpty();
});
}
@Test
void pomWithParentAndRelativePath() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo").parent("org.springframework.boot",
"spring-boot-starter-parent", "2.1.0.RELEASE", "../parent/pom.xml");
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/parent/groupId").isEqualTo("org.springframework.boot");
assertThat(pom).textAtPath("/project/parent/artifactId").isEqualTo("spring-boot-starter-parent");
assertThat(pom).textAtPath("/project/parent/version").isEqualTo("2.1.0.RELEASE");
assertThat(pom).textAtPath("/project/parent/relativePath").isEqualTo("../parent/pom.xml");
});
}
@Test
void pomWithParentAndNullRelativePath() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo").parent("org.springframework.boot",
"spring-boot-starter-parent", "2.1.0.RELEASE", null);
generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/parent/groupId").isEqualTo("org.springframework.boot");
assertThat(pom).textAtPath("/project/parent/artifactId").isEqualTo("spring-boot-starter-parent");
assertThat(pom).textAtPath("/project/parent/version").isEqualTo("2.1.0.RELEASE");
assertThat(pom).nodeAtPath("/project/parent/relativePath").isNull();
}); });
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -542,7 +542,7 @@ public class InitializrConfiguration {
*/ */
public ParentPom resolveParentPom(String bootVersion) { public ParentPom resolveParentPom(String bootVersion) {
return (StringUtils.hasText(this.parent.groupId) ? this.parent return (StringUtils.hasText(this.parent.groupId) ? this.parent
: new ParentPom(DEFAULT_PARENT_GROUP_ID, DEFAULT_PARENT_ARTIFACT_ID, bootVersion)); : new ParentPom(DEFAULT_PARENT_GROUP_ID, DEFAULT_PARENT_ARTIFACT_ID, bootVersion, ""));
} }
/** /**
@@ -577,15 +577,21 @@ public class InitializrConfiguration {
*/ */
private String version; private String version;
/**
* Parent relative path.
*/
private String relativePath = "";
/** /**
* Add the "spring-boot-dependencies" BOM to the project. * Add the "spring-boot-dependencies" BOM to the project.
*/ */
private boolean includeSpringBootBom; private boolean includeSpringBootBom;
public ParentPom(String groupId, String artifactId, String version) { public ParentPom(String groupId, String artifactId, String version, String relativePath) {
this.groupId = groupId; this.groupId = groupId;
this.artifactId = artifactId; this.artifactId = artifactId;
this.version = version; this.version = version;
this.relativePath = relativePath;
} }
public ParentPom() { public ParentPom() {
@@ -615,6 +621,14 @@ public class InitializrConfiguration {
this.version = version; this.version = version;
} }
public String getRelativePath() {
return this.relativePath;
}
public void setRelativePath(String relativePath) {
this.relativePath = relativePath;
}
public boolean isIncludeSpringBootBom() { public boolean isIncludeSpringBootBom() {
return this.includeSpringBootBom; return this.includeSpringBootBom;
} }

View File

@@ -54,6 +54,7 @@
"groupId": null, "groupId": null,
"artifactId": null, "artifactId": null,
"version": null, "version": null,
"relativePath": "",
"includeSpringBootBom": false "includeSpringBootBom": false
} }
}, },