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-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (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 artifactId the artifactID of the parent
* @param version the version of the parent
* @return this for method chaining
* @see #parent(String, String, String, String)
*/
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();
}

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");
* 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, "artifactId", parent.getArtifactId());
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>");
}

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");
* you may not use this file except in compliance with the License.
@@ -29,10 +29,13 @@ public class MavenParent {
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.artifactId = artifactId;
this.version = version;
this.relativePath = relativePath;
}
/**
@@ -59,4 +62,12 @@ public class MavenParent {
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");
* 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/artifactId").isEqualTo("spring-boot-starter-parent");
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();
});
}