mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-05 17:38:06 +08:00
Add support for configuring a Maven parent relative path
Closes gh-1296
This commit is contained in:
parent
5a4e7fac95
commit
a961c52450
@ -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.
|
||||
@ -64,7 +64,8 @@ public class DefaultMavenBuildCustomizer implements BuildCustomizer<MavenBuild>
|
||||
build.properties().property("project.build.sourceEncoding", "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) {
|
||||
|
@ -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.
|
||||
@ -76,17 +76,19 @@ class DefaultMavenBuildCustomizerTests {
|
||||
assertThat(parent.getGroupId()).isEqualTo("org.springframework.boot");
|
||||
assertThat(parent.getArtifactId()).isEqualTo("spring-boot-starter-parent");
|
||||
assertThat(parent.getVersion()).isEqualTo("2.0.0");
|
||||
assertThat(parent.getRelativePath()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeWithCustomParentAndSpringBootBomShouldAddBom() {
|
||||
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);
|
||||
MavenParent parent = build.getSettings().getParent();
|
||||
assertThat(parent.getGroupId()).isEqualTo("com.foo");
|
||||
assertThat(parent.getArtifactId()).isEqualTo("foo-parent");
|
||||
assertThat(parent.getVersion()).isEqualTo("1.0.0-SNAPSHOT");
|
||||
assertThat(parent.getRelativePath()).isEqualTo("../pom.xml");
|
||||
BomContainer boms = build.boms();
|
||||
assertThat(boms.items()).hasSize(1);
|
||||
assertThat(boms.ids()).contains("spring-boot");
|
||||
@ -97,7 +99,7 @@ class DefaultMavenBuildCustomizerTests {
|
||||
@Test
|
||||
void customizeWithNoSpringBootBomShouldNotAddBom() {
|
||||
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);
|
||||
BomContainer boms = build.boms();
|
||||
assertThat(boms.items()).hasSize(0);
|
||||
|
@ -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.
|
||||
@ -219,12 +219,13 @@ public class InitializrMetadataTestBuilder {
|
||||
}
|
||||
|
||||
public InitializrMetadataTestBuilder setMavenParent(String groupId, String artifactId, String version,
|
||||
boolean includeSpringBootBom) {
|
||||
String relativePath, boolean includeSpringBootBom) {
|
||||
this.builder.withCustomizer((it) -> {
|
||||
ParentPom parent = it.getConfiguration().getEnv().getMaven().getParent();
|
||||
parent.setGroupId(groupId);
|
||||
parent.setArtifactId(artifactId);
|
||||
parent.setVersion(version);
|
||||
parent.setRelativePath(relativePath);
|
||||
parent.setIncludeSpringBootBom(includeSpringBootBom);
|
||||
});
|
||||
return this;
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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>");
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
@ -542,7 +542,7 @@ public class InitializrConfiguration {
|
||||
*/
|
||||
public ParentPom resolveParentPom(String bootVersion) {
|
||||
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;
|
||||
|
||||
/**
|
||||
* Parent relative path.
|
||||
*/
|
||||
private String relativePath = "";
|
||||
|
||||
/**
|
||||
* Add the "spring-boot-dependencies" BOM to the project.
|
||||
*/
|
||||
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.artifactId = artifactId;
|
||||
this.version = version;
|
||||
this.relativePath = relativePath;
|
||||
}
|
||||
|
||||
public ParentPom() {
|
||||
@ -615,6 +621,14 @@ public class InitializrConfiguration {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getRelativePath() {
|
||||
return this.relativePath;
|
||||
}
|
||||
|
||||
public void setRelativePath(String relativePath) {
|
||||
this.relativePath = relativePath;
|
||||
}
|
||||
|
||||
public boolean isIncludeSpringBootBom() {
|
||||
return this.includeSpringBootBom;
|
||||
}
|
||||
|
@ -54,6 +54,7 @@
|
||||
"groupId": null,
|
||||
"artifactId": null,
|
||||
"version": null,
|
||||
"relativePath": "",
|
||||
"includeSpringBootBom": false
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user