Polish "Add distributionManagement support for Maven pom"

See gh-1038
This commit is contained in:
Stephane Nicoll 2019-12-27 16:03:43 +01:00
parent 10deca608d
commit d190e6860e
5 changed files with 485 additions and 407 deletions

View File

@ -57,10 +57,20 @@ public class MavenBuild extends Build {
return this.settings.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() { public MavenDistributionManagement.Builder distributionManagement() {
return this.distributionManagement; return this.distributionManagement;
} }
/**
* Return the {@linkplain MavenDistributionManagement distribution management} of this
* build.
* @return the {@link MavenDistributionManagement}
*/
public MavenDistributionManagement getDistributionManagement() { public MavenDistributionManagement getDistributionManagement() {
return this.distributionManagement.build(); return this.distributionManagement.build();
} }

View File

@ -18,7 +18,6 @@ package io.spring.initializr.generator.buildsystem.maven;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; 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.PropertyContainer;
import io.spring.initializr.generator.buildsystem.maven.MavenDistributionManagement.DeploymentRepository; 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.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.MavenDistributionManagement.Site;
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin.Configuration; 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.Execution;
@ -434,32 +432,31 @@ public class MavenBuildWriter {
private void writeDistributionManagement(IndentingWriter writer, MavenBuild build) { private void writeDistributionManagement(IndentingWriter writer, MavenBuild build) {
MavenDistributionManagement distributionManagement = build.getDistributionManagement(); MavenDistributionManagement distributionManagement = build.getDistributionManagement();
if (!distributionManagement.isEmpty()) { if (distributionManagement.isEmpty()) {
writeElement(writer, "distributionManagement", () -> { return;
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()));
}
});
} }
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) { private void writeDeploymentRepository(IndentingWriter writer, String name, DeploymentRepository repository) {
@ -469,19 +466,9 @@ public class MavenBuildWriter {
writeSingleElement(writer, "name", repository.getName()); writeSingleElement(writer, "name", repository.getName());
writeSingleElement(writer, "url", repository.getUrl()); writeSingleElement(writer, "url", repository.getUrl());
writeSingleElement(writer, "layout", repository.getLayout()); writeSingleElement(writer, "layout", repository.getLayout());
writeSingleElement(writer, "uniqueVersion", repository.getUniqueVersion().toString()); if (repository.getUniqueVersion() != null) {
this.writeRepositoryPolicy(writer, "releases", repository.getReleases()); writeSingleElement(writer, "uniqueVersion", Boolean.toString(repository.getUniqueVersion()));
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());
}); });
} }
} }
@ -494,16 +481,6 @@ public class MavenBuildWriter {
} }
} }
private void writeElementWithAttributes(IndentingWriter writer, String name, Runnable withContent,
Map<String, Object> 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("</%s>", name));
}
private void writeElement(IndentingWriter writer, String name, Runnable withContent) { private void writeElement(IndentingWriter writer, String name, Runnable withContent) {
writer.println(String.format("<%s>", name)); writer.println(String.format("<%s>", name));
writer.indented(withContent); writer.indented(withContent);

View File

@ -16,17 +16,13 @@
package io.spring.initializr.generator.buildsystem.maven; package io.spring.initializr.generator.buildsystem.maven;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
* Maven DistributionManagement as defined in the Maven XSD: <a href= * Maven {@code <distributionManagement>} section.
* "https://maven.apache.org/xsd/maven-4.0.0.xsd">https://maven.apache.org/xsd/maven-4.0.0.xsd</a>.
* For further documentation see
* <a href="https://maven.apache.org/pom.html#Distribution_Management">
* https://maven.apache.org/pom.html#Distribution_Management </a>
* *
* @author Joachim Pasquali * @author Joachim Pasquali
* @author Stephane Nicoll
*/ */
public class MavenDistributionManagement { public class MavenDistributionManagement {
@ -40,7 +36,7 @@ public class MavenDistributionManagement {
private final Relocation relocation; private final Relocation relocation;
protected MavenDistributionManagement(final Builder builder) { MavenDistributionManagement(Builder builder) {
this.downloadUrl = builder.downloadUrl; this.downloadUrl = builder.downloadUrl;
this.repository = builder.repository.build(); this.repository = builder.repository.build();
this.snapshotRepository = builder.snapshotRepository.build(); this.snapshotRepository = builder.snapshotRepository.build();
@ -53,22 +49,45 @@ public class MavenDistributionManagement {
&& this.site.isEmpty() && this.relocation.isEmpty(); && 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() { public String getDownloadUrl() {
return this.downloadUrl; 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() { public DeploymentRepository getRepository() {
return this.repository; 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() { public DeploymentRepository getSnapshotRepository() {
return this.snapshotRepository; return this.snapshotRepository;
} }
/**
* Return the information needed for deploying the web site of the project.
* @return the {@link Site}
*/
public Site getSite() { public Site getSite() {
return this.site; 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() { public Relocation getRelocation() {
return this.relocation; return this.relocation;
} }
@ -77,211 +96,80 @@ public class MavenDistributionManagement {
private String downloadUrl; 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; this.downloadUrl = downloadUrl;
return this; return this;
} }
public Builder repository(Consumer<DeploymentRepositoryBuilder> 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<DeploymentRepository.Builder> repository) {
repository.accept(this.repository); repository.accept(this.repository);
return this; return this;
} }
public Builder snapshotRepository(Consumer<DeploymentRepositoryBuilder> 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<DeploymentRepository.Builder> snapshotRepository) {
snapshotRepository.accept(this.snapshotRepository); snapshotRepository.accept(this.snapshotRepository);
return this; return this;
} }
public Builder site(Consumer<SiteBuilder> 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.Builder> site) {
site.accept(this.site); site.accept(this.site);
return this; return this;
} }
public Builder relocation(Consumer<RelocationBuilder> 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.Builder> relocation) {
relocation.accept(this.relocation); relocation.accept(this.relocation);
return this; return this;
} }
/**
* Build a {@link MavenDistributionManagement} with the current state of this
* builder.
* @return a {@link MavenDistributionManagement}
*/
public MavenDistributionManagement build() { public MavenDistributionManagement build() {
return new MavenDistributionManagement(this); return new MavenDistributionManagement(this);
} }
} }
public static class DeploymentRepositoryBuilder { /**
* Describe where to deploy artifacts.
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<RepositoryPolicyBuilder> releases) {
releases.accept(this.releases);
return this;
}
public DeploymentRepositoryBuilder snapshots(Consumer<RepositoryPolicyBuilder> 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 { public static class DeploymentRepository {
private final Boolean uniqueVersion;
private final RepositoryPolicy releases;
private final RepositoryPolicy snapshots;
private final String id; private final String id;
private final String name; private final String name;
@ -290,122 +178,240 @@ public class MavenDistributionManagement {
private final String layout; private final String layout;
protected DeploymentRepository(DeploymentRepositoryBuilder builder) { private final Boolean uniqueVersion;
this.uniqueVersion = builder.uniqueVersion;
this.releases = builder.releases.build(); DeploymentRepository(Builder builder) {
this.snapshots = builder.snapshots.build();
this.id = builder.id; this.id = builder.id;
this.name = builder.name; this.name = builder.name;
this.url = builder.url; this.url = builder.url;
this.layout = builder.layout; this.layout = builder.layout;
this.uniqueVersion = builder.uniqueVersion;
} }
public boolean isEmpty() { public boolean isEmpty() {
return this.uniqueVersion == null && this.id == null && this.name == null && this.url == null return this.id == null && this.name == null && this.url == null && this.layout == null
&& this.layout == null && this.releases.isEmpty() && this.snapshots.isEmpty(); && this.uniqueVersion == null;
}
public Boolean getUniqueVersion() {
return Optional.ofNullable(this.uniqueVersion).orElse(Boolean.TRUE);
}
public RepositoryPolicy getReleases() {
return this.releases;
}
public RepositoryPolicy getSnapshots() {
return this.snapshots;
} }
/**
* Return the identifier of the repository.
* @return the repository ID
*/
public String getId() { public String getId() {
return this.id; return this.id;
} }
/**
* Return the name of the repository.
* @return the repository name
*/
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* Return the url of the repository to use to upload artifacts.
* @return the repository url
*/
public String getUrl() { public String getUrl() {
return this.url; return this.url;
} }
/**
* Return the repository layout. Can be {@code default} or {@code legacy}.
* @return the repository layout
*/
public String getLayout() { public String getLayout() {
return this.layout; return this.layout;
} }
} /**
* Return whether to assign snapshots a unique version comprised of the timestamp
public static class RepositoryPolicy { * and build number, or to use the same version each time.
* @return {@code true} to assign each snapshot a unique version
private final Boolean enabled; */
public Boolean getUniqueVersion() {
private final String updatePolicy; return this.uniqueVersion;
private final String checksumPolicy;
protected RepositoryPolicy(RepositoryPolicyBuilder builder) {
this.enabled = builder.enabled;
this.updatePolicy = builder.updatePolicy;
this.checksumPolicy = builder.checksumPolicy;
} }
public boolean isEmpty() { public static class Builder {
return this.enabled == null && this.updatePolicy == null && this.checksumPolicy == null;
}
public Boolean isEnabled() { private String id;
return Optional.ofNullable(this.enabled).orElse(Boolean.TRUE);
}
public String getUpdatePolicy() { private String name;
return this.updatePolicy;
} 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 { public static class Site {
private final String id; private final String id;
private final String url;
private final String name; private final String name;
private final Boolean childSiteUrlInheritAppendPath; private final String url;
public Site(SiteBuilder builder) { Site(Builder builder) {
this.id = builder.id; this.id = builder.id;
this.url = builder.url;
this.name = builder.name; this.name = builder.name;
this.childSiteUrlInheritAppendPath = builder.childSiteUrlInheritAppendPath; this.url = builder.url;
} }
public boolean isEmpty() { 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() { public String getId() {
return this.id; return this.id;
} }
public String getUrl() { /**
return this.url; * Return the name of the repository.
} * @return the repository name
*/
public String getName() { public String getName() {
return this.name; 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 { public static class Relocation {
private final String groupId; private final String groupId;
@ -416,7 +422,7 @@ public class MavenDistributionManagement {
private final String message; private final String message;
public Relocation(RelocationBuilder builder) { Relocation(Builder builder) {
this.groupId = builder.groupId; this.groupId = builder.groupId;
this.artifactId = builder.artifactId; this.artifactId = builder.artifactId;
this.version = builder.version; 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 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() { public String getGroupId() {
return this.groupId; return this.groupId;
} }
/**
* Return the new artifact ID of the dependency.
* @return the relocated artifact ID
*/
public String getArtifactId() { public String getArtifactId() {
return this.artifactId; return this.artifactId;
} }
/**
* Return the new version of the dependency.
* @return the relocated version
*/
public String getVersion() { public String getVersion() {
return this.version; return this.version;
} }
/**
* Return a message that provides more details about the relocation.
* @return the relocation message
*/
public String getMessage() { public String getMessage() {
return this.message; 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);
}
}
} }
} }

View File

@ -659,74 +659,98 @@ class MavenBuildWriterTests {
} }
@Test @Test
void pomWithDistributionManagement() { void powWithDistributionManagementEmpty() {
MavenBuild build = new MavenBuild(); MavenBuild build = new MavenBuild();
generatePom(build, (pom) -> assertThat(pom).nodeAtPath("/project/distributionManagement").isNull());
}
build.distributionManagement().downloadUrl("downloadUrl") @Test
.relocation((relocation) -> relocation.groupId("groupId").artifactId("my.artifact").version("version") void powWithDistributionManagementDownloadUrl() {
.message("message")) MavenBuild build = new MavenBuild();
.repository((repository) -> repository.id("id").layout("layout").name("name") build.distributionManagement().downloadUrl("https://example.com/download");
.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) -> { generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/distributionManagement/downloadUrl").isEqualTo("downloadUrl"); NodeAssert distributionManagement = pom.nodeAtPath("/project/distributionManagement");
assertThat(pom).textAtPath("/project/distributionManagement/relocation/groupId").isEqualTo("groupId"); assertThat(distributionManagement).textAtPath("downloadUrl").isEqualTo("https://example.com/download");
assertThat(pom).textAtPath("/project/distributionManagement/relocation/artifactId") assertThat(distributionManagement).nodeAtPath("repository").isNull();
.isEqualTo("my.artifact"); assertThat(distributionManagement).nodeAtPath("snapshotRepository").isNull();
assertThat(pom).textAtPath("/project/distributionManagement/relocation/version").isEqualTo("version"); assertThat(distributionManagement).nodeAtPath("site").isNull();
assertThat(pom).textAtPath("/project/distributionManagement/relocation/message").isEqualTo("message"); assertThat(distributionManagement).nodeAtPath("relocation").isNull();
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 @Test
void pomWithDistributionManagementWithNullAttributeValue() { void powWithDistributionManagementRepository() {
MavenBuild build = new MavenBuild(); 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) -> { generatePom(build, (pom) -> {
assertThat(pom).textAtPath("/project/distributionManagement/site/id").isEqualTo("id"); NodeAssert distributionManagement = pom.nodeAtPath("/project/distributionManagement");
assertThat(pom).nodeAtPath("/project/distributionManagement/site") assertThat(distributionManagement).textAtPath("downloadUrl").isNullOrEmpty();
.matches((node) -> node.getAttributes().getNamedItem("child.site.url.inherit.append.path") == null); 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");
}); });
} }

View File

@ -16,21 +16,22 @@
package io.spring.initializr.generator.buildsystem.maven; 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 org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Tests for {@link MavenDistributionManagement} * Tests for {@link MavenDistributionManagement}.
* *
* @author Joachim Pasquali * @author Joachim Pasquali
* @author Stephane Nicoll
*/ */
class MavenDistributionManagementTests { class MavenDistributionManagementTests {
private static final String TEST_URL = "testURL";
@Test @Test
void emptyDistributionManagement() { void distributionManagementEmpty() {
MavenDistributionManagement result = builder().build(); MavenDistributionManagement result = builder().build();
assertThat(result.isEmpty()).isTrue(); assertThat(result.isEmpty()).isTrue();
assertThat(result.getRelocation().isEmpty()).isTrue(); assertThat(result.getRelocation().isEmpty()).isTrue();
@ -40,75 +41,59 @@ class MavenDistributionManagementTests {
} }
@Test @Test
void addDownloadUrl() { void distributionManagementWithDownloadUrl() {
MavenDistributionManagement result = builder().downloadUrl(TEST_URL).build(); MavenDistributionManagement mdm = builder().downloadUrl("https://example.com/download").build();
assertThat(result.getDownloadUrl()).isEqualTo(TEST_URL); assertThat(mdm.getDownloadUrl()).isEqualTo("https://example.com/download");
} }
@Test @Test
void addRelocation() { void distributionManagementWithRepository() {
MavenDistributionManagement result = builder().relocation((relocation) -> relocation.artifactId("artifactId") MavenDistributionManagement mdm = builder()
.groupId("groupId").version("version").message("message")).build(); .repository((repository) -> repository.id("released-repo").name("released repo")
assertThat(result.getRelocation().getArtifactId()).isEqualTo("artifactId"); .url("https://upload.example.com/releases"))
assertThat(result.getRelocation().getGroupId()).isEqualTo("groupId"); .repository((repository) -> repository.layout("default")).build();
assertThat(result.getRelocation().getVersion()).isEqualTo("version"); DeploymentRepository repository = mdm.getRepository();
assertThat(result.getRelocation().getMessage()).isEqualTo("message"); 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 @Test
void addRepository() { void distributionManagementWithSnapshotRepository() {
MavenDistributionManagement result = builder().repository((repository) -> repository.id("id").layout("layout") MavenDistributionManagement mdm = builder()
.name("name") .snapshotRepository((repository) -> repository.id("snapshot-repo").name("snapshot repo")
.releases((releases) -> releases.checksumPolicy("checksumPolicy").enabled(Boolean.FALSE) .url("scp://upload.example.com/snapshots"))
.updatePolicy("updatePolicy")) .snapshotRepository((repository) -> repository.uniqueVersion(true)).build();
.snapshots((snapshots) -> snapshots.checksumPolicy("checksumPolicy").updatePolicy("updatePolicy")) DeploymentRepository snapshotRepository = mdm.getSnapshotRepository();
.uniqueVersion(Boolean.FALSE).url("url")).build(); assertThat(snapshotRepository.getId()).isEqualTo("snapshot-repo");
assertThat(result.getRepository().getId()).isEqualTo("id"); assertThat(snapshotRepository.getName()).isEqualTo("snapshot repo");
assertThat(result.getRepository().getUrl()).isEqualTo("url"); assertThat(snapshotRepository.getUrl()).isEqualTo("scp://upload.example.com/snapshots");
assertThat(result.getRepository().getUniqueVersion()).isFalse(); assertThat(snapshotRepository.getLayout()).isNull();
assertThat(result.getRepository().getLayout()).isEqualTo("layout"); assertThat(snapshotRepository.getUniqueVersion()).isTrue();
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 @Test
void addSnapshotRepository() { void distributionManagementWithSite() {
MavenDistributionManagement result = builder().snapshotRepository((repository) -> repository.id("id") MavenDistributionManagement mdm = builder().site((site) -> site.id("website").name("web site"))
.layout("layout").name("name").releases((releases) -> releases.checksumPolicy("checksumPolicy") .site((site) -> site.url("scp://www.example.com/www/docs/project")).build();
.enabled(Boolean.TRUE).updatePolicy("updatePolicy")) Site site = mdm.getSite();
.uniqueVersion(Boolean.FALSE).url("url")).build(); assertThat(site.getId()).isEqualTo("website");
assertThat(result.getSnapshotRepository().getId()).isEqualTo("id"); assertThat(site.getName()).isEqualTo("web site");
assertThat(result.getSnapshotRepository().getLayout()).isEqualTo("layout"); assertThat(site.getUrl()).isEqualTo("scp://www.example.com/www/docs/project");
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 @Test
void addSite() { void distributionManagementWithRelocation() {
MavenDistributionManagement result = builder() MavenDistributionManagement mdm = builder()
.site((site) -> site.id("id").name("name").url("url").childSiteUrlInheritAppendPath(Boolean.FALSE)) .relocation(
.build(); (relocation) -> relocation.groupId("com.example.new").artifactId("project").version("1.0.0"))
assertThat(result.getSite().getId()).isEqualTo("id"); .relocation((relocation) -> relocation.message("Moved to com.example.new")).build();
assertThat(result.getSite().getName()).isEqualTo("name"); assertThat(mdm.getRelocation().getGroupId()).isEqualTo("com.example.new");
assertThat(result.getSite().getUrl()).isEqualTo("url"); assertThat(mdm.getRelocation().getArtifactId()).isEqualTo("project");
assertThat(result.getSite().getChildSiteUrlInheritAppendPath()).isFalse(); assertThat(mdm.getRelocation().getVersion()).isEqualTo("1.0.0");
} assertThat(mdm.getRelocation().getMessage()).isEqualTo("Moved to com.example.new");
@Test
void addSiteWithNullAttribute() {
MavenDistributionManagement result = builder().site((site) -> site.id("id")).build();
assertThat(result.getSite().getChildSiteUrlInheritAppendPath()).isNull();
} }
private MavenDistributionManagement.Builder builder() { private MavenDistributionManagement.Builder builder() {