From 10deca608dc730bfb10ad257e4ec765240b265bb Mon Sep 17 00:00:00 2001 From: Joachim Pasquali Date: Thu, 7 Nov 2019 20:48:08 -0500 Subject: [PATCH 1/2] Add distributionManagement support for Maven pom See gh-1038 --- .../buildsystem/maven/MavenBuild.java | 10 + .../buildsystem/maven/MavenBuildWriter.java | 71 +++ .../maven/MavenDistributionManagement.java | 448 ++++++++++++++++++ .../maven/MavenBuildWriterTests.java | 73 +++ .../MavenDistributionManagementTests.java | 118 +++++ 5 files changed, 720 insertions(+) create mode 100644 initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java create mode 100644 initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java index 9ddf9f52..4c11a1e0 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java @@ -37,6 +37,8 @@ public class MavenBuild extends Build { private final MavenPluginContainer plugins = new MavenPluginContainer(); + private final MavenDistributionManagement.Builder distributionManagement = new MavenDistributionManagement.Builder(); + public MavenBuild(BuildItemResolver buildItemResolver) { super(buildItemResolver); } @@ -55,6 +57,14 @@ public class MavenBuild extends Build { return this.settings.build(); } + public MavenDistributionManagement.Builder distributionManagement() { + return this.distributionManagement; + } + + public MavenDistributionManagement getDistributionManagement() { + return this.distributionManagement.build(); + } + /** * Return the {@linkplain MavenResource resource container} to use to configure main * resources. diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java index d5d72511..7c6efc6a 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java @@ -18,6 +18,7 @@ package io.spring.initializr.generator.buildsystem.maven; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Locale; @@ -35,6 +36,10 @@ import io.spring.initializr.generator.buildsystem.DependencyContainer; import io.spring.initializr.generator.buildsystem.DependencyScope; import io.spring.initializr.generator.buildsystem.MavenRepository; import io.spring.initializr.generator.buildsystem.PropertyContainer; +import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.DeploymentRepository; +import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.Relocation; +import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.RepositoryPolicy; +import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.Site; import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Configuration; import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Execution; import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Setting; @@ -51,6 +56,7 @@ import org.springframework.util.ObjectUtils; * @author Stephane Nicoll * @author Olga Maciaszek-Sharma * @author Jafer Khan Shamshad + * @author Joachim Pasquali */ public class MavenBuildWriter { @@ -74,6 +80,7 @@ public class MavenBuildWriter { writeDependencyManagement(writer, build); writeBuild(writer, build); writeRepositories(writer, build); + writeDistributionManagement(writer, build); }); } @@ -425,6 +432,60 @@ public class MavenBuildWriter { }); } + private void writeDistributionManagement(IndentingWriter writer, MavenBuild build) { + MavenDistributionManagement distributionManagement = build.getDistributionManagement(); + if (!distributionManagement.isEmpty()) { + writeElement(writer, "distributionManagement", () -> { + writeSingleElement(writer, "downloadUrl", distributionManagement.getDownloadUrl()); + this.writeDeploymentRepository(writer, "repository", distributionManagement.getRepository()); + this.writeDeploymentRepository(writer, "snapshotRepository", + distributionManagement.getSnapshotRepository()); + if (!distributionManagement.getRelocation().isEmpty()) { + Relocation relocation = distributionManagement.getRelocation(); + writeElement(writer, "relocation", () -> { + writeSingleElement(writer, "groupId", relocation.getGroupId()); + writeSingleElement(writer, "artifactId", relocation.getArtifactId()); + writeSingleElement(writer, "version", relocation.getVersion()); + writeSingleElement(writer, "message", relocation.getMessage()); + }); + } + if (!distributionManagement.getSite().isEmpty()) { + Site site = distributionManagement.getSite(); + writeElementWithAttributes(writer, "site", () -> { + writeSingleElement(writer, "id", site.getId()); + writeSingleElement(writer, "name", site.getName()); + writeSingleElement(writer, "url", site.getUrl()); + }, Collections.singletonMap("child.site.url.inherit.append.path", + site.getChildSiteUrlInheritAppendPath())); + } + }); + } + } + + private void writeDeploymentRepository(IndentingWriter writer, String name, DeploymentRepository repository) { + if (!repository.isEmpty()) { + writeElement(writer, name, () -> { + writeSingleElement(writer, "id", repository.getId()); + writeSingleElement(writer, "name", repository.getName()); + writeSingleElement(writer, "url", repository.getUrl()); + writeSingleElement(writer, "layout", repository.getLayout()); + writeSingleElement(writer, "uniqueVersion", repository.getUniqueVersion().toString()); + this.writeRepositoryPolicy(writer, "releases", repository.getReleases()); + this.writeRepositoryPolicy(writer, "snapshots", repository.getSnapshots()); + }); + } + } + + private void writeRepositoryPolicy(IndentingWriter writer, String name, RepositoryPolicy policy) { + if (!policy.isEmpty()) { + writeElement(writer, name, () -> { + writeSingleElement(writer, "enabled", policy.isEnabled().toString()); + writeSingleElement(writer, "updatePolicy", policy.getUpdatePolicy()); + writeSingleElement(writer, "checksumPolicy", policy.getChecksumPolicy()); + }); + } + } + private void writeSingleElement(IndentingWriter writer, String name, String text) { if (text != null) { writer.print(String.format("<%s>", name)); @@ -433,6 +494,16 @@ public class MavenBuildWriter { } } + private void writeElementWithAttributes(IndentingWriter writer, String name, Runnable withContent, + Map attributeMap) { + writer.print(String.format("<%s", name)); + attributeMap.entrySet().stream().filter((entry) -> entry.getValue() != null).forEach( + (entry) -> writer.print(String.format(" %s=\"%s\"", entry.getKey(), entry.getValue().toString()))); + writer.println(">"); + writer.indented(withContent); + writer.println(String.format("", name)); + } + private void writeElement(IndentingWriter writer, String name, Runnable withContent) { writer.println(String.format("<%s>", name)); writer.indented(withContent); diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java new file mode 100644 index 00000000..6ab8bd9f --- /dev/null +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java @@ -0,0 +1,448 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +import java.util.Optional; +import java.util.function.Consumer; + +/** + * Maven DistributionManagement as defined in the Maven XSD: https://maven.apache.org/xsd/maven-4.0.0.xsd. + * For further documentation see + * + * https://maven.apache.org/pom.html#Distribution_Management + * + * @author Joachim Pasquali + */ +public class MavenDistributionManagement { + + private final String downloadUrl; + + private final DeploymentRepository repository; + + private final DeploymentRepository snapshotRepository; + + private final Site site; + + private final Relocation relocation; + + protected MavenDistributionManagement(final Builder builder) { + this.downloadUrl = builder.downloadUrl; + this.repository = builder.repository.build(); + this.snapshotRepository = builder.snapshotRepository.build(); + this.site = builder.site.build(); + this.relocation = builder.relocation.build(); + } + + public boolean isEmpty() { + return this.downloadUrl == null && this.repository.isEmpty() && this.snapshotRepository.isEmpty() + && this.site.isEmpty() && this.relocation.isEmpty(); + } + + public String getDownloadUrl() { + return this.downloadUrl; + } + + public DeploymentRepository getRepository() { + return this.repository; + } + + public DeploymentRepository getSnapshotRepository() { + return this.snapshotRepository; + } + + public Site getSite() { + return this.site; + } + + public Relocation getRelocation() { + return this.relocation; + } + + public static class Builder { + + private String downloadUrl; + + private DeploymentRepositoryBuilder repository = new DeploymentRepositoryBuilder(); + + private DeploymentRepositoryBuilder snapshotRepository = new DeploymentRepositoryBuilder(); + + private SiteBuilder site = new SiteBuilder(); + + private RelocationBuilder relocation = new RelocationBuilder(); + + public Builder downloadUrl(final String downloadUrl) { + this.downloadUrl = downloadUrl; + return this; + } + + public Builder repository(Consumer repository) { + repository.accept(this.repository); + return this; + } + + public Builder snapshotRepository(Consumer snapshotRepository) { + snapshotRepository.accept(this.snapshotRepository); + return this; + } + + public Builder site(Consumer site) { + site.accept(this.site); + return this; + } + + public Builder relocation(Consumer relocation) { + relocation.accept(this.relocation); + return this; + } + + public MavenDistributionManagement build() { + return new MavenDistributionManagement(this); + } + + } + + public static class DeploymentRepositoryBuilder { + + private Boolean uniqueVersion; + + private RepositoryPolicyBuilder releases = new RepositoryPolicyBuilder(); + + private RepositoryPolicyBuilder snapshots = new RepositoryPolicyBuilder(); + + private String id; + + private String name; + + private String url; + + private String layout; + + public DeploymentRepositoryBuilder uniqueVersion(Boolean uniqueVersion) { + this.uniqueVersion = uniqueVersion; + return this; + } + + public DeploymentRepositoryBuilder releases(Consumer releases) { + releases.accept(this.releases); + return this; + } + + public DeploymentRepositoryBuilder snapshots(Consumer snapshots) { + snapshots.accept(this.snapshots); + return this; + } + + public DeploymentRepositoryBuilder id(String id) { + this.id = id; + return this; + } + + public DeploymentRepositoryBuilder name(String name) { + this.name = name; + return this; + } + + public DeploymentRepositoryBuilder url(String url) { + this.url = url; + return this; + } + + public DeploymentRepositoryBuilder layout(String layout) { + this.layout = layout; + return this; + } + + public DeploymentRepository build() { + return new DeploymentRepository(this); + } + + } + + public static class RepositoryPolicyBuilder { + + private Boolean enabled; + + private String updatePolicy; + + private String checksumPolicy; + + public RepositoryPolicyBuilder enabled(Boolean enabled) { + this.enabled = enabled; + return this; + } + + public RepositoryPolicyBuilder updatePolicy(String updatePolicy) { + this.updatePolicy = updatePolicy; + return this; + } + + public RepositoryPolicyBuilder checksumPolicy(String checksumPolicy) { + this.checksumPolicy = checksumPolicy; + return this; + } + + public RepositoryPolicy build() { + return new RepositoryPolicy(this); + } + + } + + public static class SiteBuilder { + + private String id; + + private String url; + + private String name; + + private Boolean childSiteUrlInheritAppendPath; + + public SiteBuilder childSiteUrlInheritAppendPath(Boolean childSiteUrlInheritAppendPath) { + this.childSiteUrlInheritAppendPath = childSiteUrlInheritAppendPath; + return this; + } + + public SiteBuilder id(String id) { + this.id = id; + return this; + } + + public SiteBuilder url(String url) { + this.url = url; + return this; + } + + public SiteBuilder name(String name) { + this.name = name; + return this; + } + + public Site build() { + return new Site(this); + } + + } + + public static class RelocationBuilder { + + private String groupId; + + private String artifactId; + + private String version; + + private String message; + + public RelocationBuilder groupId(String groupId) { + this.groupId = groupId; + return this; + } + + public RelocationBuilder artifactId(String artifactId) { + this.artifactId = artifactId; + return this; + } + + public RelocationBuilder version(String version) { + this.version = version; + return this; + } + + public RelocationBuilder message(String message) { + this.message = message; + return this; + } + + public Relocation build() { + return new Relocation(this); + } + + } + + public static class DeploymentRepository { + + private final Boolean uniqueVersion; + + private final RepositoryPolicy releases; + + private final RepositoryPolicy snapshots; + + private final String id; + + private final String name; + + private final String url; + + private final String layout; + + protected DeploymentRepository(DeploymentRepositoryBuilder builder) { + this.uniqueVersion = builder.uniqueVersion; + this.releases = builder.releases.build(); + this.snapshots = builder.snapshots.build(); + this.id = builder.id; + this.name = builder.name; + this.url = builder.url; + this.layout = builder.layout; + } + + public boolean isEmpty() { + return this.uniqueVersion == null && this.id == null && this.name == null && this.url == null + && this.layout == null && this.releases.isEmpty() && this.snapshots.isEmpty(); + } + + public Boolean getUniqueVersion() { + return Optional.ofNullable(this.uniqueVersion).orElse(Boolean.TRUE); + } + + public RepositoryPolicy getReleases() { + return this.releases; + } + + public RepositoryPolicy getSnapshots() { + return this.snapshots; + } + + public String getId() { + return this.id; + } + + public String getName() { + return this.name; + } + + public String getUrl() { + return this.url; + } + + public String getLayout() { + return this.layout; + } + + } + + public static class RepositoryPolicy { + + private final Boolean enabled; + + private final String updatePolicy; + + private final String checksumPolicy; + + protected RepositoryPolicy(RepositoryPolicyBuilder builder) { + this.enabled = builder.enabled; + this.updatePolicy = builder.updatePolicy; + this.checksumPolicy = builder.checksumPolicy; + } + + public boolean isEmpty() { + return this.enabled == null && this.updatePolicy == null && this.checksumPolicy == null; + } + + public Boolean isEnabled() { + return Optional.ofNullable(this.enabled).orElse(Boolean.TRUE); + } + + public String getUpdatePolicy() { + return this.updatePolicy; + } + + public String getChecksumPolicy() { + return this.checksumPolicy; + } + + } + + public static class Site { + + private final String id; + + private final String url; + + private final String name; + + private final Boolean childSiteUrlInheritAppendPath; + + public Site(SiteBuilder builder) { + this.id = builder.id; + this.url = builder.url; + this.name = builder.name; + this.childSiteUrlInheritAppendPath = builder.childSiteUrlInheritAppendPath; + } + + public boolean isEmpty() { + return this.id == null && this.url == null && this.name == null; + } + + public String getId() { + return this.id; + } + + public String getUrl() { + return this.url; + } + + public String getName() { + return this.name; + } + + public Boolean getChildSiteUrlInheritAppendPath() { + return this.childSiteUrlInheritAppendPath; + } + + } + + public static class Relocation { + + private final String groupId; + + private final String artifactId; + + private final String version; + + private final String message; + + public Relocation(RelocationBuilder builder) { + this.groupId = builder.groupId; + this.artifactId = builder.artifactId; + this.version = builder.version; + this.message = builder.message; + } + + public boolean isEmpty() { + return this.groupId == null && this.artifactId == null && this.version == null && this.message == null; + } + + public String getGroupId() { + return this.groupId; + } + + public String getArtifactId() { + return this.artifactId; + } + + public String getVersion() { + return this.version; + } + + public String getMessage() { + return this.message; + } + + } + +} diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java index 6d1c2534..427b8c39 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java @@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Stephane Nicoll * @author Olga Maciaszek-Sharma * @author Jafer Khan Shamshad + * @author Joachim Pasquali */ class MavenBuildWriterTests { @@ -657,6 +658,78 @@ class MavenBuildWriterTests { generatePom(build, (pom) -> assertThat(pom).textAtPath("/project/version").isEqualTo("1.2.4.RELEASE")); } + @Test + void pomWithDistributionManagement() { + MavenBuild build = new MavenBuild(); + + build.distributionManagement().downloadUrl("downloadUrl") + .relocation((relocation) -> relocation.groupId("groupId").artifactId("my.artifact").version("version") + .message("message")) + .repository((repository) -> repository.id("id").layout("layout").name("name") + .releases((releases) -> releases.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) + .snapshots( + (snapshots) -> snapshots.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) + .url("url")) + .site((site) -> site.id("id").name("name").url("url").childSiteUrlInheritAppendPath(Boolean.TRUE)) + .snapshotRepository((snapshotRepository) -> snapshotRepository.id("id").layout("layout").name("name") + .releases((releases) -> releases.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) + .snapshots( + (snapshots) -> snapshots.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) + .url("url")); + + generatePom(build, (pom) -> { + assertThat(pom).textAtPath("/project/distributionManagement/downloadUrl").isEqualTo("downloadUrl"); + assertThat(pom).textAtPath("/project/distributionManagement/relocation/groupId").isEqualTo("groupId"); + assertThat(pom).textAtPath("/project/distributionManagement/relocation/artifactId") + .isEqualTo("my.artifact"); + assertThat(pom).textAtPath("/project/distributionManagement/relocation/version").isEqualTo("version"); + assertThat(pom).textAtPath("/project/distributionManagement/relocation/message").isEqualTo("message"); + + assertThat(pom).textAtPath("/project/distributionManagement/repository/id").isEqualTo("id"); + assertThat(pom).textAtPath("/project/distributionManagement/repository/layout").isEqualTo("layout"); + assertThat(pom).textAtPath("/project/distributionManagement/repository/name").isEqualTo("name"); + assertThat(pom).textAtPath("/project/distributionManagement/repository/url").isEqualTo("url"); + assertThat(pom).textAtPath("/project/distributionManagement/repository/releases/checksumPolicy") + .isEqualTo("checksumPolicy"); + assertThat(pom).textAtPath("/project/distributionManagement/repository/releases/updatePolicy") + .isEqualTo("updatePolicy"); + assertThat(pom).textAtPath("/project/distributionManagement/repository/snapshots/checksumPolicy") + .isEqualTo("checksumPolicy"); + assertThat(pom).textAtPath("/project/distributionManagement/repository/snapshots/updatePolicy") + .isEqualTo("updatePolicy"); + + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/id").isEqualTo("id"); + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/layout").isEqualTo("layout"); + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/name").isEqualTo("name"); + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/url").isEqualTo("url"); + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/releases/checksumPolicy") + .isEqualTo("checksumPolicy"); + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/releases/updatePolicy") + .isEqualTo("updatePolicy"); + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/snapshots/checksumPolicy") + .isEqualTo("checksumPolicy"); + assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/snapshots/updatePolicy") + .isEqualTo("updatePolicy"); + + assertThat(pom).textAtPath("/project/distributionManagement/site/id").isEqualTo("id"); + assertThat(pom).textAtPath("/project/distributionManagement/site/name").isEqualTo("name"); + assertThat(pom).textAtPath("/project/distributionManagement/site/url").isEqualTo("url"); + assertThat(pom).nodeAtPath("/project/distributionManagement/site").matches((node) -> node.getAttributes() + .getNamedItem("child.site.url.inherit.append.path").getTextContent().equals("true")); + }); + } + + @Test + void pomWithDistributionManagementWithNullAttributeValue() { + MavenBuild build = new MavenBuild(); + build.distributionManagement().site((site) -> site.id("id").childSiteUrlInheritAppendPath(null)); + generatePom(build, (pom) -> { + assertThat(pom).textAtPath("/project/distributionManagement/site/id").isEqualTo("id"); + assertThat(pom).nodeAtPath("/project/distributionManagement/site") + .matches((node) -> node.getAttributes().getNamedItem("child.site.url.inherit.append.path") == null); + }); + } + private void generatePom(MavenBuild mavenBuild, Consumer consumer) { MavenBuildWriter writer = new MavenBuildWriter(); StringWriter out = new StringWriter(); diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java new file mode 100644 index 00000000..c48ca66a --- /dev/null +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java @@ -0,0 +1,118 @@ +/* + * Copyright 2012-2019 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.spring.initializr.generator.buildsystem.maven; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link MavenDistributionManagement} + * + * @author Joachim Pasquali + */ +class MavenDistributionManagementTests { + + private static final String TEST_URL = "testURL"; + + @Test + void emptyDistributionManagement() { + MavenDistributionManagement result = builder().build(); + assertThat(result.isEmpty()).isTrue(); + assertThat(result.getRelocation().isEmpty()).isTrue(); + assertThat(result.getRepository().isEmpty()).isTrue(); + assertThat(result.getSite().isEmpty()).isTrue(); + assertThat(result.getSnapshotRepository().isEmpty()).isTrue(); + } + + @Test + void addDownloadUrl() { + MavenDistributionManagement result = builder().downloadUrl(TEST_URL).build(); + assertThat(result.getDownloadUrl()).isEqualTo(TEST_URL); + } + + @Test + void addRelocation() { + MavenDistributionManagement result = builder().relocation((relocation) -> relocation.artifactId("artifactId") + .groupId("groupId").version("version").message("message")).build(); + assertThat(result.getRelocation().getArtifactId()).isEqualTo("artifactId"); + assertThat(result.getRelocation().getGroupId()).isEqualTo("groupId"); + assertThat(result.getRelocation().getVersion()).isEqualTo("version"); + assertThat(result.getRelocation().getMessage()).isEqualTo("message"); + } + + @Test + void addRepository() { + MavenDistributionManagement result = builder().repository((repository) -> repository.id("id").layout("layout") + .name("name") + .releases((releases) -> releases.checksumPolicy("checksumPolicy").enabled(Boolean.FALSE) + .updatePolicy("updatePolicy")) + .snapshots((snapshots) -> snapshots.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) + .uniqueVersion(Boolean.FALSE).url("url")).build(); + assertThat(result.getRepository().getId()).isEqualTo("id"); + assertThat(result.getRepository().getUrl()).isEqualTo("url"); + assertThat(result.getRepository().getUniqueVersion()).isFalse(); + assertThat(result.getRepository().getLayout()).isEqualTo("layout"); + assertThat(result.getRepository().getName()).isEqualTo("name"); + assertThat(result.getRepository().getReleases().isEnabled()).isFalse(); + assertThat(result.getRepository().getReleases().getChecksumPolicy()).isEqualTo("checksumPolicy"); + assertThat(result.getRepository().getReleases().getUpdatePolicy()).isEqualTo("updatePolicy"); + + assertThat(result.getRepository().getSnapshots().getChecksumPolicy()).isEqualTo("checksumPolicy"); + assertThat(result.getRepository().getSnapshots().getUpdatePolicy()).isEqualTo("updatePolicy"); + assertThat(result.getRepository().getSnapshots().isEnabled()).isTrue(); + + } + + @Test + void addSnapshotRepository() { + MavenDistributionManagement result = builder().snapshotRepository((repository) -> repository.id("id") + .layout("layout").name("name").releases((releases) -> releases.checksumPolicy("checksumPolicy") + .enabled(Boolean.TRUE).updatePolicy("updatePolicy")) + .uniqueVersion(Boolean.FALSE).url("url")).build(); + assertThat(result.getSnapshotRepository().getId()).isEqualTo("id"); + assertThat(result.getSnapshotRepository().getLayout()).isEqualTo("layout"); + assertThat(result.getSnapshotRepository().getName()).isEqualTo("name"); + assertThat(result.getSnapshotRepository().getReleases().getChecksumPolicy()).isEqualTo("checksumPolicy"); + assertThat(result.getSnapshotRepository().getReleases().getUpdatePolicy()).isEqualTo("updatePolicy"); + + assertThat(result.getSnapshotRepository().getSnapshots().isEmpty()).isTrue(); + + } + + @Test + void addSite() { + MavenDistributionManagement result = builder() + .site((site) -> site.id("id").name("name").url("url").childSiteUrlInheritAppendPath(Boolean.FALSE)) + .build(); + assertThat(result.getSite().getId()).isEqualTo("id"); + assertThat(result.getSite().getName()).isEqualTo("name"); + assertThat(result.getSite().getUrl()).isEqualTo("url"); + assertThat(result.getSite().getChildSiteUrlInheritAppendPath()).isFalse(); + } + + @Test + void addSiteWithNullAttribute() { + MavenDistributionManagement result = builder().site((site) -> site.id("id")).build(); + assertThat(result.getSite().getChildSiteUrlInheritAppendPath()).isNull(); + } + + private MavenDistributionManagement.Builder builder() { + return new MavenDistributionManagement.Builder(); + } + +} From d190e6860eeb4a9cbf6a212222f9841b15fc81a5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 27 Dec 2019 16:03:43 +0100 Subject: [PATCH 2/2] Polish "Add distributionManagement support for Maven pom" See gh-1038 --- .../buildsystem/maven/MavenBuild.java | 10 + .../buildsystem/maven/MavenBuildWriter.java | 77 +-- .../maven/MavenDistributionManagement.java | 556 ++++++++++-------- .../maven/MavenBuildWriterTests.java | 142 +++-- .../MavenDistributionManagementTests.java | 107 ++-- 5 files changed, 485 insertions(+), 407 deletions(-) diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java index 4c11a1e0..d98b6f0f 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java @@ -57,10 +57,20 @@ public class MavenBuild extends Build { return this.settings.build(); } + /** + * Return a builder to configure the {@linkplain MavenDistributionManagement + * distribution management} of this build. + * @return a builder for {@link MavenDistributionManagement} + */ public MavenDistributionManagement.Builder distributionManagement() { return this.distributionManagement; } + /** + * Return the {@linkplain MavenDistributionManagement distribution management} of this + * build. + * @return the {@link MavenDistributionManagement} + */ public MavenDistributionManagement getDistributionManagement() { return this.distributionManagement.build(); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java index 7c6efc6a..52ee5814 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java @@ -18,7 +18,6 @@ package io.spring.initializr.generator.buildsystem.maven; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Locale; @@ -38,7 +37,6 @@ import io.spring.initializr.generator.buildsystem.MavenRepository; import io.spring.initializr.generator.buildsystem.PropertyContainer; import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.DeploymentRepository; import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.Relocation; -import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.RepositoryPolicy; import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.Site; import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Configuration; import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Execution; @@ -434,32 +432,31 @@ public class MavenBuildWriter { private void writeDistributionManagement(IndentingWriter writer, MavenBuild build) { MavenDistributionManagement distributionManagement = build.getDistributionManagement(); - if (!distributionManagement.isEmpty()) { - writeElement(writer, "distributionManagement", () -> { - writeSingleElement(writer, "downloadUrl", distributionManagement.getDownloadUrl()); - this.writeDeploymentRepository(writer, "repository", distributionManagement.getRepository()); - this.writeDeploymentRepository(writer, "snapshotRepository", - distributionManagement.getSnapshotRepository()); - if (!distributionManagement.getRelocation().isEmpty()) { - Relocation relocation = distributionManagement.getRelocation(); - writeElement(writer, "relocation", () -> { - writeSingleElement(writer, "groupId", relocation.getGroupId()); - writeSingleElement(writer, "artifactId", relocation.getArtifactId()); - writeSingleElement(writer, "version", relocation.getVersion()); - writeSingleElement(writer, "message", relocation.getMessage()); - }); - } - if (!distributionManagement.getSite().isEmpty()) { - Site site = distributionManagement.getSite(); - writeElementWithAttributes(writer, "site", () -> { - writeSingleElement(writer, "id", site.getId()); - writeSingleElement(writer, "name", site.getName()); - writeSingleElement(writer, "url", site.getUrl()); - }, Collections.singletonMap("child.site.url.inherit.append.path", - site.getChildSiteUrlInheritAppendPath())); - } - }); + if (distributionManagement.isEmpty()) { + return; } + writeElement(writer, "distributionManagement", () -> { + writeSingleElement(writer, "downloadUrl", distributionManagement.getDownloadUrl()); + writeDeploymentRepository(writer, "repository", distributionManagement.getRepository()); + writeDeploymentRepository(writer, "snapshotRepository", distributionManagement.getSnapshotRepository()); + Site site = distributionManagement.getSite(); + if (!site.isEmpty()) { + writeElement(writer, "site", () -> { + writeSingleElement(writer, "id", site.getId()); + writeSingleElement(writer, "name", site.getName()); + writeSingleElement(writer, "url", site.getUrl()); + }); + } + Relocation relocation = distributionManagement.getRelocation(); + if (!relocation.isEmpty()) { + writeElement(writer, "relocation", () -> { + writeSingleElement(writer, "groupId", relocation.getGroupId()); + writeSingleElement(writer, "artifactId", relocation.getArtifactId()); + writeSingleElement(writer, "version", relocation.getVersion()); + writeSingleElement(writer, "message", relocation.getMessage()); + }); + } + }); } private void writeDeploymentRepository(IndentingWriter writer, String name, DeploymentRepository repository) { @@ -469,19 +466,9 @@ public class MavenBuildWriter { writeSingleElement(writer, "name", repository.getName()); writeSingleElement(writer, "url", repository.getUrl()); writeSingleElement(writer, "layout", repository.getLayout()); - writeSingleElement(writer, "uniqueVersion", repository.getUniqueVersion().toString()); - this.writeRepositoryPolicy(writer, "releases", repository.getReleases()); - this.writeRepositoryPolicy(writer, "snapshots", repository.getSnapshots()); - }); - } - } - - private void writeRepositoryPolicy(IndentingWriter writer, String name, RepositoryPolicy policy) { - if (!policy.isEmpty()) { - writeElement(writer, name, () -> { - writeSingleElement(writer, "enabled", policy.isEnabled().toString()); - writeSingleElement(writer, "updatePolicy", policy.getUpdatePolicy()); - writeSingleElement(writer, "checksumPolicy", policy.getChecksumPolicy()); + if (repository.getUniqueVersion() != null) { + writeSingleElement(writer, "uniqueVersion", Boolean.toString(repository.getUniqueVersion())); + } }); } } @@ -494,16 +481,6 @@ public class MavenBuildWriter { } } - private void writeElementWithAttributes(IndentingWriter writer, String name, Runnable withContent, - Map attributeMap) { - writer.print(String.format("<%s", name)); - attributeMap.entrySet().stream().filter((entry) -> entry.getValue() != null).forEach( - (entry) -> writer.print(String.format(" %s=\"%s\"", entry.getKey(), entry.getValue().toString()))); - writer.println(">"); - writer.indented(withContent); - writer.println(String.format("", name)); - } - private void writeElement(IndentingWriter writer, String name, Runnable withContent) { writer.println(String.format("<%s>", name)); writer.indented(withContent); diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java index 6ab8bd9f..58e75a2a 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagement.java @@ -16,17 +16,13 @@ package io.spring.initializr.generator.buildsystem.maven; -import java.util.Optional; import java.util.function.Consumer; /** - * Maven DistributionManagement as defined in the Maven XSD: https://maven.apache.org/xsd/maven-4.0.0.xsd. - * For further documentation see - * - * https://maven.apache.org/pom.html#Distribution_Management + * Maven {@code } section. * * @author Joachim Pasquali + * @author Stephane Nicoll */ public class MavenDistributionManagement { @@ -40,7 +36,7 @@ public class MavenDistributionManagement { private final Relocation relocation; - protected MavenDistributionManagement(final Builder builder) { + MavenDistributionManagement(Builder builder) { this.downloadUrl = builder.downloadUrl; this.repository = builder.repository.build(); this.snapshotRepository = builder.snapshotRepository.build(); @@ -53,22 +49,45 @@ public class MavenDistributionManagement { && this.site.isEmpty() && this.relocation.isEmpty(); } + /** + * Return the URL where this project can be downloaded from. + * @return the URL of the project's download page + */ public String getDownloadUrl() { return this.downloadUrl; } + /** + * Return the information needed to deploy the artifacts generated by the project to a + * remote repository. + * @return the {@link DeploymentRepository} for released artifacts + */ public DeploymentRepository getRepository() { return this.repository; } + /** + * Return the information needed to deploy the snapshot artifacts generated by the + * project to a remote repository. + * @return the {@link DeploymentRepository} for snapshot artifacts + */ public DeploymentRepository getSnapshotRepository() { return this.snapshotRepository; } + /** + * Return the information needed for deploying the web site of the project. + * @return the {@link Site} + */ public Site getSite() { return this.site; } + /** + * Return the relocation information of the artifact if it has been moved to a new + * groupId and/or artifactId. + * @return the {@link Relocation} + */ public Relocation getRelocation() { return this.relocation; } @@ -77,211 +96,80 @@ public class MavenDistributionManagement { private String downloadUrl; - private DeploymentRepositoryBuilder repository = new DeploymentRepositoryBuilder(); + private DeploymentRepository.Builder repository = new DeploymentRepository.Builder(); - private DeploymentRepositoryBuilder snapshotRepository = new DeploymentRepositoryBuilder(); + private DeploymentRepository.Builder snapshotRepository = new DeploymentRepository.Builder(); - private SiteBuilder site = new SiteBuilder(); + private Site.Builder site = new Site.Builder(); - private RelocationBuilder relocation = new RelocationBuilder(); + private Relocation.Builder relocation = new Relocation.Builder(); - public Builder downloadUrl(final String downloadUrl) { + /** + * Specify the URL where this project can be downloaded from. + * @param downloadUrl the URL of the project's download page + * @return this for method chaining + */ + public Builder downloadUrl(String downloadUrl) { this.downloadUrl = downloadUrl; return this; } - public Builder repository(Consumer repository) { + /** + * Customize the {@code repository} using the specified consumer. + * @param repository a consumer of the current repository + * @return this for method chaining + */ + public Builder repository(Consumer repository) { repository.accept(this.repository); return this; } - public Builder snapshotRepository(Consumer snapshotRepository) { + /** + * Customize the {@code snapshotRepository} using the specified consumer. + * @param snapshotRepository a consumer of the current snapshot repository + * @return this for method chaining + */ + public Builder snapshotRepository(Consumer snapshotRepository) { snapshotRepository.accept(this.snapshotRepository); return this; } - public Builder site(Consumer site) { + /** + * Customize the {@code site} using the specified consumer. + * @param site a consumer of the current site + * @return this for method chaining + */ + public Builder site(Consumer site) { site.accept(this.site); return this; } - public Builder relocation(Consumer relocation) { + /** + * Customize the {@code relocation} using the specified consumer. + * @param relocation a consumer of the current relocation + * @return this for method chaining + */ + public Builder relocation(Consumer relocation) { relocation.accept(this.relocation); return this; } + /** + * Build a {@link MavenDistributionManagement} with the current state of this + * builder. + * @return a {@link MavenDistributionManagement} + */ public MavenDistributionManagement build() { return new MavenDistributionManagement(this); } } - public static class DeploymentRepositoryBuilder { - - private Boolean uniqueVersion; - - private RepositoryPolicyBuilder releases = new RepositoryPolicyBuilder(); - - private RepositoryPolicyBuilder snapshots = new RepositoryPolicyBuilder(); - - private String id; - - private String name; - - private String url; - - private String layout; - - public DeploymentRepositoryBuilder uniqueVersion(Boolean uniqueVersion) { - this.uniqueVersion = uniqueVersion; - return this; - } - - public DeploymentRepositoryBuilder releases(Consumer releases) { - releases.accept(this.releases); - return this; - } - - public DeploymentRepositoryBuilder snapshots(Consumer snapshots) { - snapshots.accept(this.snapshots); - return this; - } - - public DeploymentRepositoryBuilder id(String id) { - this.id = id; - return this; - } - - public DeploymentRepositoryBuilder name(String name) { - this.name = name; - return this; - } - - public DeploymentRepositoryBuilder url(String url) { - this.url = url; - return this; - } - - public DeploymentRepositoryBuilder layout(String layout) { - this.layout = layout; - return this; - } - - public DeploymentRepository build() { - return new DeploymentRepository(this); - } - - } - - public static class RepositoryPolicyBuilder { - - private Boolean enabled; - - private String updatePolicy; - - private String checksumPolicy; - - public RepositoryPolicyBuilder enabled(Boolean enabled) { - this.enabled = enabled; - return this; - } - - public RepositoryPolicyBuilder updatePolicy(String updatePolicy) { - this.updatePolicy = updatePolicy; - return this; - } - - public RepositoryPolicyBuilder checksumPolicy(String checksumPolicy) { - this.checksumPolicy = checksumPolicy; - return this; - } - - public RepositoryPolicy build() { - return new RepositoryPolicy(this); - } - - } - - public static class SiteBuilder { - - private String id; - - private String url; - - private String name; - - private Boolean childSiteUrlInheritAppendPath; - - public SiteBuilder childSiteUrlInheritAppendPath(Boolean childSiteUrlInheritAppendPath) { - this.childSiteUrlInheritAppendPath = childSiteUrlInheritAppendPath; - return this; - } - - public SiteBuilder id(String id) { - this.id = id; - return this; - } - - public SiteBuilder url(String url) { - this.url = url; - return this; - } - - public SiteBuilder name(String name) { - this.name = name; - return this; - } - - public Site build() { - return new Site(this); - } - - } - - public static class RelocationBuilder { - - private String groupId; - - private String artifactId; - - private String version; - - private String message; - - public RelocationBuilder groupId(String groupId) { - this.groupId = groupId; - return this; - } - - public RelocationBuilder artifactId(String artifactId) { - this.artifactId = artifactId; - return this; - } - - public RelocationBuilder version(String version) { - this.version = version; - return this; - } - - public RelocationBuilder message(String message) { - this.message = message; - return this; - } - - public Relocation build() { - return new Relocation(this); - } - - } - + /** + * Describe where to deploy artifacts. + */ public static class DeploymentRepository { - private final Boolean uniqueVersion; - - private final RepositoryPolicy releases; - - private final RepositoryPolicy snapshots; - private final String id; private final String name; @@ -290,122 +178,240 @@ public class MavenDistributionManagement { private final String layout; - protected DeploymentRepository(DeploymentRepositoryBuilder builder) { - this.uniqueVersion = builder.uniqueVersion; - this.releases = builder.releases.build(); - this.snapshots = builder.snapshots.build(); + private final Boolean uniqueVersion; + + DeploymentRepository(Builder builder) { this.id = builder.id; this.name = builder.name; this.url = builder.url; this.layout = builder.layout; + this.uniqueVersion = builder.uniqueVersion; } public boolean isEmpty() { - return this.uniqueVersion == null && this.id == null && this.name == null && this.url == null - && this.layout == null && this.releases.isEmpty() && this.snapshots.isEmpty(); - } - - public Boolean getUniqueVersion() { - return Optional.ofNullable(this.uniqueVersion).orElse(Boolean.TRUE); - } - - public RepositoryPolicy getReleases() { - return this.releases; - } - - public RepositoryPolicy getSnapshots() { - return this.snapshots; + return this.id == null && this.name == null && this.url == null && this.layout == null + && this.uniqueVersion == null; } + /** + * Return the identifier of the repository. + * @return the repository ID + */ public String getId() { return this.id; } + /** + * Return the name of the repository. + * @return the repository name + */ public String getName() { return this.name; } + /** + * Return the url of the repository to use to upload artifacts. + * @return the repository url + */ public String getUrl() { return this.url; } + /** + * Return the repository layout. Can be {@code default} or {@code legacy}. + * @return the repository layout + */ public String getLayout() { return this.layout; } - } - - public static class RepositoryPolicy { - - private final Boolean enabled; - - private final String updatePolicy; - - private final String checksumPolicy; - - protected RepositoryPolicy(RepositoryPolicyBuilder builder) { - this.enabled = builder.enabled; - this.updatePolicy = builder.updatePolicy; - this.checksumPolicy = builder.checksumPolicy; + /** + * Return whether to assign snapshots a unique version comprised of the timestamp + * and build number, or to use the same version each time. + * @return {@code true} to assign each snapshot a unique version + */ + public Boolean getUniqueVersion() { + return this.uniqueVersion; } - public boolean isEmpty() { - return this.enabled == null && this.updatePolicy == null && this.checksumPolicy == null; - } + public static class Builder { - public Boolean isEnabled() { - return Optional.ofNullable(this.enabled).orElse(Boolean.TRUE); - } + private String id; - public String getUpdatePolicy() { - return this.updatePolicy; - } + private String name; + + private String url; + + private String layout; + + private Boolean uniqueVersion; + + /** + * Set the id of the repository. + * @param id the identifier + * @return this for method chaining + */ + public Builder id(String id) { + this.id = id; + return this; + } + + /** + * Set the name of the repository. + * @param name the name + * @return this for method chaining + */ + public Builder name(String name) { + this.name = name; + return this; + } + + /** + * Set the url of the repository to use to upload artifacts. Specify both the + * location and the transport protocol used to transfer a built artifact to + * the repository. + * @param url the url + * @return this for method chaining + */ + public Builder url(String url) { + this.url = url; + return this; + } + + /** + * Set the repository layout, can be {@code default} or {@code legacy}. + * @param layout the layout + * @return this for method chaining + */ + public Builder layout(String layout) { + this.layout = layout; + return this; + } + + /** + * Set whether snapshots should be assigned a unique version comprised of the + * timestamp and build number. + * @param uniqueVersion {@code true} to use unique version for snapshots + * @return this for method chaining + */ + public Builder uniqueVersion(Boolean uniqueVersion) { + this.uniqueVersion = uniqueVersion; + return this; + } + + /** + * Build a {@link DeploymentRepository} with the current state of this + * builder. + * @return a {@link DeploymentRepository} + */ + public DeploymentRepository build() { + return new DeploymentRepository(this); + } - public String getChecksumPolicy() { - return this.checksumPolicy; } } + /** + * Information needed for deploying the web site of the project. + */ public static class Site { private final String id; - private final String url; - private final String name; - private final Boolean childSiteUrlInheritAppendPath; + private final String url; - public Site(SiteBuilder builder) { + Site(Builder builder) { this.id = builder.id; - this.url = builder.url; this.name = builder.name; - this.childSiteUrlInheritAppendPath = builder.childSiteUrlInheritAppendPath; + this.url = builder.url; } public boolean isEmpty() { - return this.id == null && this.url == null && this.name == null; + return this.id == null && this.name == null && this.url == null; } + /** + * Return the identifier of the repository. + * @return the repository ID + */ public String getId() { return this.id; } - public String getUrl() { - return this.url; - } - + /** + * Return the name of the repository. + * @return the repository name + */ public String getName() { return this.name; } - public Boolean getChildSiteUrlInheritAppendPath() { - return this.childSiteUrlInheritAppendPath; + /** + * Return the url of the repository to use to upload the site. + * @return the repository url + */ + public String getUrl() { + return this.url; + } + + public static class Builder { + + private String id; + + private String name; + + private String url; + + /** + * Set the id of the repository. + * @param id the identifier + * @return this for method chaining + */ + public Builder id(String id) { + this.id = id; + return this; + } + + /** + * Set the name of the repository. + * @param name the name + * @return this for method chaining + */ + public Builder name(String name) { + this.name = name; + return this; + } + + /** + * Set the url of the repository to use to upload the site. Specify both the + * location and the transport protocol to use. + * @param url the url + * @return this for method chaining + */ + public Builder url(String url) { + this.url = url; + return this; + } + + /** + * Build a {@link Site} with the current state of this builder. + * @return a {@link Site} + */ + public Site build() { + return new Site(this); + } + } } + /** + * Relocation information of the artifact if it has been moved to a new groupId and/or + * artifactId. + */ public static class Relocation { private final String groupId; @@ -416,7 +422,7 @@ public class MavenDistributionManagement { private final String message; - public Relocation(RelocationBuilder builder) { + Relocation(Builder builder) { this.groupId = builder.groupId; this.artifactId = builder.artifactId; this.version = builder.version; @@ -427,22 +433,98 @@ public class MavenDistributionManagement { return this.groupId == null && this.artifactId == null && this.version == null && this.message == null; } + /** + * Return the new group ID of the dependency. + * @return the relocated group ID + */ public String getGroupId() { return this.groupId; } + /** + * Return the new artifact ID of the dependency. + * @return the relocated artifact ID + */ public String getArtifactId() { return this.artifactId; } + /** + * Return the new version of the dependency. + * @return the relocated version + */ public String getVersion() { return this.version; } + /** + * Return a message that provides more details about the relocation. + * @return the relocation message + */ public String getMessage() { return this.message; } + public static class Builder { + + private String groupId; + + private String artifactId; + + private String version; + + private String message; + + /** + * Specify the new group ID of the dependency. + * @param groupId the new group ID of the dependency + * @return this for method chaining + */ + public Builder groupId(String groupId) { + this.groupId = groupId; + return this; + } + + /** + * Specify the new artifact ID of the dependency. + * @param artifactId the new artifact ID of the dependency + * @return this for method chaining + */ + public Builder artifactId(String artifactId) { + this.artifactId = artifactId; + return this; + } + + /** + * Specify the new version of the dependency. + * @param version the new version of the dependency + * @return this for method chaining + */ + public Builder version(String version) { + this.version = version; + return this; + } + + /** + * Specify a message that provides more details about the relocation. + * @param message the relocation message + * @return this for method chaining + */ + public Builder message(String message) { + this.message = message; + return this; + } + + /** + * Build a {@link Relocation} with the current state of this builder. + * @return a {@link Relocation} + */ + public Relocation build() { + return new Relocation(this); + } + + } + } } diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java index 427b8c39..ca63c2cc 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java @@ -659,74 +659,98 @@ class MavenBuildWriterTests { } @Test - void pomWithDistributionManagement() { + void powWithDistributionManagementEmpty() { MavenBuild build = new MavenBuild(); + generatePom(build, (pom) -> assertThat(pom).nodeAtPath("/project/distributionManagement").isNull()); + } - build.distributionManagement().downloadUrl("downloadUrl") - .relocation((relocation) -> relocation.groupId("groupId").artifactId("my.artifact").version("version") - .message("message")) - .repository((repository) -> repository.id("id").layout("layout").name("name") - .releases((releases) -> releases.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) - .snapshots( - (snapshots) -> snapshots.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) - .url("url")) - .site((site) -> site.id("id").name("name").url("url").childSiteUrlInheritAppendPath(Boolean.TRUE)) - .snapshotRepository((snapshotRepository) -> snapshotRepository.id("id").layout("layout").name("name") - .releases((releases) -> releases.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) - .snapshots( - (snapshots) -> snapshots.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) - .url("url")); - + @Test + void powWithDistributionManagementDownloadUrl() { + MavenBuild build = new MavenBuild(); + build.distributionManagement().downloadUrl("https://example.com/download"); generatePom(build, (pom) -> { - assertThat(pom).textAtPath("/project/distributionManagement/downloadUrl").isEqualTo("downloadUrl"); - assertThat(pom).textAtPath("/project/distributionManagement/relocation/groupId").isEqualTo("groupId"); - assertThat(pom).textAtPath("/project/distributionManagement/relocation/artifactId") - .isEqualTo("my.artifact"); - assertThat(pom).textAtPath("/project/distributionManagement/relocation/version").isEqualTo("version"); - assertThat(pom).textAtPath("/project/distributionManagement/relocation/message").isEqualTo("message"); - - assertThat(pom).textAtPath("/project/distributionManagement/repository/id").isEqualTo("id"); - assertThat(pom).textAtPath("/project/distributionManagement/repository/layout").isEqualTo("layout"); - assertThat(pom).textAtPath("/project/distributionManagement/repository/name").isEqualTo("name"); - assertThat(pom).textAtPath("/project/distributionManagement/repository/url").isEqualTo("url"); - assertThat(pom).textAtPath("/project/distributionManagement/repository/releases/checksumPolicy") - .isEqualTo("checksumPolicy"); - assertThat(pom).textAtPath("/project/distributionManagement/repository/releases/updatePolicy") - .isEqualTo("updatePolicy"); - assertThat(pom).textAtPath("/project/distributionManagement/repository/snapshots/checksumPolicy") - .isEqualTo("checksumPolicy"); - assertThat(pom).textAtPath("/project/distributionManagement/repository/snapshots/updatePolicy") - .isEqualTo("updatePolicy"); - - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/id").isEqualTo("id"); - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/layout").isEqualTo("layout"); - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/name").isEqualTo("name"); - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/url").isEqualTo("url"); - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/releases/checksumPolicy") - .isEqualTo("checksumPolicy"); - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/releases/updatePolicy") - .isEqualTo("updatePolicy"); - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/snapshots/checksumPolicy") - .isEqualTo("checksumPolicy"); - assertThat(pom).textAtPath("/project/distributionManagement/snapshotRepository/snapshots/updatePolicy") - .isEqualTo("updatePolicy"); - - assertThat(pom).textAtPath("/project/distributionManagement/site/id").isEqualTo("id"); - assertThat(pom).textAtPath("/project/distributionManagement/site/name").isEqualTo("name"); - assertThat(pom).textAtPath("/project/distributionManagement/site/url").isEqualTo("url"); - assertThat(pom).nodeAtPath("/project/distributionManagement/site").matches((node) -> node.getAttributes() - .getNamedItem("child.site.url.inherit.append.path").getTextContent().equals("true")); + NodeAssert distributionManagement = pom.nodeAtPath("/project/distributionManagement"); + assertThat(distributionManagement).textAtPath("downloadUrl").isEqualTo("https://example.com/download"); + assertThat(distributionManagement).nodeAtPath("repository").isNull(); + assertThat(distributionManagement).nodeAtPath("snapshotRepository").isNull(); + assertThat(distributionManagement).nodeAtPath("site").isNull(); + assertThat(distributionManagement).nodeAtPath("relocation").isNull(); }); } @Test - void pomWithDistributionManagementWithNullAttributeValue() { + void powWithDistributionManagementRepository() { MavenBuild build = new MavenBuild(); - build.distributionManagement().site((site) -> site.id("id").childSiteUrlInheritAppendPath(null)); + build.distributionManagement().repository((repository) -> repository.id("released-repo").name("released repo") + .url("https://upload.example.com/releases")); generatePom(build, (pom) -> { - assertThat(pom).textAtPath("/project/distributionManagement/site/id").isEqualTo("id"); - assertThat(pom).nodeAtPath("/project/distributionManagement/site") - .matches((node) -> node.getAttributes().getNamedItem("child.site.url.inherit.append.path") == null); + NodeAssert distributionManagement = pom.nodeAtPath("/project/distributionManagement"); + assertThat(distributionManagement).textAtPath("downloadUrl").isNullOrEmpty(); + assertThat(distributionManagement).textAtPath("repository/id").isEqualTo("released-repo"); + assertThat(distributionManagement).textAtPath("repository/name").isEqualTo("released repo"); + assertThat(distributionManagement).textAtPath("repository/url") + .isEqualTo("https://upload.example.com/releases"); + assertThat(distributionManagement).textAtPath("repository/layout").isNullOrEmpty(); + assertThat(distributionManagement).textAtPath("repository/uniqueVersion").isNullOrEmpty(); + assertThat(distributionManagement).nodeAtPath("snapshotRepository").isNull(); + assertThat(distributionManagement).nodeAtPath("site").isNull(); + assertThat(distributionManagement).nodeAtPath("relocation").isNull(); + }); + } + + @Test + void powWithDistributionManagementSnapshotRepository() { + MavenBuild build = new MavenBuild(); + build.distributionManagement().snapshotRepository((repository) -> repository.id("snapshot-repo") + .name("snapshot repo").url("scp://upload.example.com/snapshots").layout("legacy").uniqueVersion(true)); + generatePom(build, (pom) -> { + NodeAssert distributionManagement = pom.nodeAtPath("/project/distributionManagement"); + assertThat(distributionManagement).textAtPath("downloadUrl").isNullOrEmpty(); + assertThat(distributionManagement).nodeAtPath("repository").isNull(); + assertThat(distributionManagement).textAtPath("snapshotRepository/id").isEqualTo("snapshot-repo"); + assertThat(distributionManagement).textAtPath("snapshotRepository/name").isEqualTo("snapshot repo"); + assertThat(distributionManagement).textAtPath("snapshotRepository/url") + .isEqualTo("scp://upload.example.com/snapshots"); + assertThat(distributionManagement).textAtPath("snapshotRepository/layout").isEqualTo("legacy"); + assertThat(distributionManagement).textAtPath("snapshotRepository/uniqueVersion").isEqualTo("true"); + assertThat(distributionManagement).nodeAtPath("site").isNull(); + assertThat(distributionManagement).nodeAtPath("relocation").isNull(); + }); + } + + @Test + void powWithDistributionManagementSite() { + MavenBuild build = new MavenBuild(); + build.distributionManagement().site((site) -> site.id("website").name("web site")) + .site((site) -> site.url("scp://www.example.com/www/docs/project")); + generatePom(build, (pom) -> { + NodeAssert distributionManagement = pom.nodeAtPath("/project/distributionManagement"); + assertThat(distributionManagement).textAtPath("downloadUrl").isNullOrEmpty(); + assertThat(distributionManagement).nodeAtPath("repository").isNull(); + assertThat(distributionManagement).nodeAtPath("snapshotRepository").isNull(); + assertThat(distributionManagement).textAtPath("site/id").isEqualTo("website"); + assertThat(distributionManagement).textAtPath("site/name").isEqualTo("web site"); + assertThat(distributionManagement).textAtPath("site/url") + .isEqualTo("scp://www.example.com/www/docs/project"); + assertThat(distributionManagement).nodeAtPath("relocation").isNull(); + }); + } + + @Test + void powWithDistributionManagementRelocation() { + MavenBuild build = new MavenBuild(); + build.distributionManagement().relocation((relocation) -> relocation.groupId("com.example.new") + .artifactId("project").version("1.0.0").message("moved")); + generatePom(build, (pom) -> { + NodeAssert distributionManagement = pom.nodeAtPath("/project/distributionManagement"); + assertThat(distributionManagement).textAtPath("downloadUrl").isNullOrEmpty(); + assertThat(distributionManagement).nodeAtPath("repository").isNull(); + assertThat(distributionManagement).nodeAtPath("snapshotRepository").isNull(); + assertThat(distributionManagement).nodeAtPath("site").isNull(); + assertThat(distributionManagement).textAtPath("relocation/groupId").isEqualTo("com.example.new"); + assertThat(distributionManagement).textAtPath("relocation/artifactId").isEqualTo("project"); + assertThat(distributionManagement).textAtPath("relocation/version").isEqualTo("1.0.0"); + assertThat(distributionManagement).textAtPath("relocation/message").isEqualTo("moved"); }); } diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java index c48ca66a..0178272d 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenDistributionManagementTests.java @@ -16,21 +16,22 @@ package io.spring.initializr.generator.buildsystem.maven; +import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.DeploymentRepository; +import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.Site; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link MavenDistributionManagement} + * Tests for {@link MavenDistributionManagement}. * * @author Joachim Pasquali + * @author Stephane Nicoll */ class MavenDistributionManagementTests { - private static final String TEST_URL = "testURL"; - @Test - void emptyDistributionManagement() { + void distributionManagementEmpty() { MavenDistributionManagement result = builder().build(); assertThat(result.isEmpty()).isTrue(); assertThat(result.getRelocation().isEmpty()).isTrue(); @@ -40,75 +41,59 @@ class MavenDistributionManagementTests { } @Test - void addDownloadUrl() { - MavenDistributionManagement result = builder().downloadUrl(TEST_URL).build(); - assertThat(result.getDownloadUrl()).isEqualTo(TEST_URL); + void distributionManagementWithDownloadUrl() { + MavenDistributionManagement mdm = builder().downloadUrl("https://example.com/download").build(); + assertThat(mdm.getDownloadUrl()).isEqualTo("https://example.com/download"); } @Test - void addRelocation() { - MavenDistributionManagement result = builder().relocation((relocation) -> relocation.artifactId("artifactId") - .groupId("groupId").version("version").message("message")).build(); - assertThat(result.getRelocation().getArtifactId()).isEqualTo("artifactId"); - assertThat(result.getRelocation().getGroupId()).isEqualTo("groupId"); - assertThat(result.getRelocation().getVersion()).isEqualTo("version"); - assertThat(result.getRelocation().getMessage()).isEqualTo("message"); + void distributionManagementWithRepository() { + MavenDistributionManagement mdm = builder() + .repository((repository) -> repository.id("released-repo").name("released repo") + .url("https://upload.example.com/releases")) + .repository((repository) -> repository.layout("default")).build(); + DeploymentRepository repository = mdm.getRepository(); + assertThat(repository.getId()).isEqualTo("released-repo"); + assertThat(repository.getName()).isEqualTo("released repo"); + assertThat(repository.getUrl()).isEqualTo("https://upload.example.com/releases"); + assertThat(repository.getLayout()).isEqualTo("default"); + assertThat(repository.getUniqueVersion()).isNull(); } @Test - void addRepository() { - MavenDistributionManagement result = builder().repository((repository) -> repository.id("id").layout("layout") - .name("name") - .releases((releases) -> releases.checksumPolicy("checksumPolicy").enabled(Boolean.FALSE) - .updatePolicy("updatePolicy")) - .snapshots((snapshots) -> snapshots.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) - .uniqueVersion(Boolean.FALSE).url("url")).build(); - assertThat(result.getRepository().getId()).isEqualTo("id"); - assertThat(result.getRepository().getUrl()).isEqualTo("url"); - assertThat(result.getRepository().getUniqueVersion()).isFalse(); - assertThat(result.getRepository().getLayout()).isEqualTo("layout"); - assertThat(result.getRepository().getName()).isEqualTo("name"); - assertThat(result.getRepository().getReleases().isEnabled()).isFalse(); - assertThat(result.getRepository().getReleases().getChecksumPolicy()).isEqualTo("checksumPolicy"); - assertThat(result.getRepository().getReleases().getUpdatePolicy()).isEqualTo("updatePolicy"); - - assertThat(result.getRepository().getSnapshots().getChecksumPolicy()).isEqualTo("checksumPolicy"); - assertThat(result.getRepository().getSnapshots().getUpdatePolicy()).isEqualTo("updatePolicy"); - assertThat(result.getRepository().getSnapshots().isEnabled()).isTrue(); - + void distributionManagementWithSnapshotRepository() { + MavenDistributionManagement mdm = builder() + .snapshotRepository((repository) -> repository.id("snapshot-repo").name("snapshot repo") + .url("scp://upload.example.com/snapshots")) + .snapshotRepository((repository) -> repository.uniqueVersion(true)).build(); + DeploymentRepository snapshotRepository = mdm.getSnapshotRepository(); + assertThat(snapshotRepository.getId()).isEqualTo("snapshot-repo"); + assertThat(snapshotRepository.getName()).isEqualTo("snapshot repo"); + assertThat(snapshotRepository.getUrl()).isEqualTo("scp://upload.example.com/snapshots"); + assertThat(snapshotRepository.getLayout()).isNull(); + assertThat(snapshotRepository.getUniqueVersion()).isTrue(); } @Test - void addSnapshotRepository() { - MavenDistributionManagement result = builder().snapshotRepository((repository) -> repository.id("id") - .layout("layout").name("name").releases((releases) -> releases.checksumPolicy("checksumPolicy") - .enabled(Boolean.TRUE).updatePolicy("updatePolicy")) - .uniqueVersion(Boolean.FALSE).url("url")).build(); - assertThat(result.getSnapshotRepository().getId()).isEqualTo("id"); - assertThat(result.getSnapshotRepository().getLayout()).isEqualTo("layout"); - assertThat(result.getSnapshotRepository().getName()).isEqualTo("name"); - assertThat(result.getSnapshotRepository().getReleases().getChecksumPolicy()).isEqualTo("checksumPolicy"); - assertThat(result.getSnapshotRepository().getReleases().getUpdatePolicy()).isEqualTo("updatePolicy"); - - assertThat(result.getSnapshotRepository().getSnapshots().isEmpty()).isTrue(); - + void distributionManagementWithSite() { + MavenDistributionManagement mdm = builder().site((site) -> site.id("website").name("web site")) + .site((site) -> site.url("scp://www.example.com/www/docs/project")).build(); + Site site = mdm.getSite(); + assertThat(site.getId()).isEqualTo("website"); + assertThat(site.getName()).isEqualTo("web site"); + assertThat(site.getUrl()).isEqualTo("scp://www.example.com/www/docs/project"); } @Test - void addSite() { - MavenDistributionManagement result = builder() - .site((site) -> site.id("id").name("name").url("url").childSiteUrlInheritAppendPath(Boolean.FALSE)) - .build(); - assertThat(result.getSite().getId()).isEqualTo("id"); - assertThat(result.getSite().getName()).isEqualTo("name"); - assertThat(result.getSite().getUrl()).isEqualTo("url"); - assertThat(result.getSite().getChildSiteUrlInheritAppendPath()).isFalse(); - } - - @Test - void addSiteWithNullAttribute() { - MavenDistributionManagement result = builder().site((site) -> site.id("id")).build(); - assertThat(result.getSite().getChildSiteUrlInheritAppendPath()).isNull(); + void distributionManagementWithRelocation() { + MavenDistributionManagement mdm = builder() + .relocation( + (relocation) -> relocation.groupId("com.example.new").artifactId("project").version("1.0.0")) + .relocation((relocation) -> relocation.message("Moved to com.example.new")).build(); + assertThat(mdm.getRelocation().getGroupId()).isEqualTo("com.example.new"); + assertThat(mdm.getRelocation().getArtifactId()).isEqualTo("project"); + assertThat(mdm.getRelocation().getVersion()).isEqualTo("1.0.0"); + assertThat(mdm.getRelocation().getMessage()).isEqualTo("Moved to com.example.new"); } private MavenDistributionManagement.Builder builder() {