mirror of
https://gitee.com/dcren/initializr.git
synced 2025-11-24 08:33:18 +08:00
Use self-closed tag for empty attributes in Maven pom
Closes gh-1244
This commit is contained in:
@@ -49,6 +49,7 @@ import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link MavenBuild} writer for {@code pom.xml}.
|
||||
@@ -557,9 +558,14 @@ public class MavenBuildWriter {
|
||||
private void writeSingleElement(IndentingWriter writer, String name, Object value) {
|
||||
if (value != null) {
|
||||
CharSequence text = (value instanceof CharSequence) ? (CharSequence) value : value.toString();
|
||||
writer.print(String.format("<%s>", name));
|
||||
writer.print(encodeText(text));
|
||||
writer.println(String.format("</%s>", name));
|
||||
if (!StringUtils.hasLength(text)) {
|
||||
writer.println(String.format("<%s/>", name));
|
||||
}
|
||||
else {
|
||||
writer.print(String.format("<%s>", name));
|
||||
writer.print(encodeText(text));
|
||||
writer.println(String.format("</%s>", name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -217,6 +217,19 @@ class MavenBuildWriterTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void pomWithPropertiesAndEmptyValue() {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.settings().coordinates("com.example.demo", "demo");
|
||||
build.properties().property("alpha", "");
|
||||
generatePom(build, (pom) -> {
|
||||
assertThat(pom).nodeAtPath("/project/properties/alpha").isNotNull();
|
||||
assertThat(pom).textAtPath("/project/properties/alpha").isEmpty();
|
||||
});
|
||||
String pom = writePom(new MavenBuildWriter(), build);
|
||||
assertThat(pom).containsSubsequence("<properties>", "<alpha/>", "</properties>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void pomWithVersionProperties() {
|
||||
MavenBuild build = new MavenBuild();
|
||||
|
||||
Reference in New Issue
Block a user