Polish "Add support for Maven <scm>"

See gh-1051
This commit is contained in:
Stephane Nicoll
2020-03-17 06:16:30 +01:00
parent 4151be0a64
commit 95c306cc9b
6 changed files with 88 additions and 68 deletions

View File

@@ -39,8 +39,6 @@ public class MavenBuild extends Build {
private final MavenDistributionManagement.Builder distributionManagement = new MavenDistributionManagement.Builder(); private final MavenDistributionManagement.Builder distributionManagement = new MavenDistributionManagement.Builder();
private final Scm.Builder scm = new Scm.Builder();
public MavenBuild(BuildItemResolver buildItemResolver) { public MavenBuild(BuildItemResolver buildItemResolver) {
super(buildItemResolver); super(buildItemResolver);
} }
@@ -49,14 +47,6 @@ public class MavenBuild extends Build {
this(null); this(null);
} }
public Scm.Builder scm() {
return this.scm;
}
public Scm getScm() {
return this.scm.build();
}
@Override @Override
public MavenBuildSettings.Builder settings() { public MavenBuildSettings.Builder settings() {
return this.settings; return this.settings;

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
import io.spring.initializr.generator.buildsystem.BuildSettings; import io.spring.initializr.generator.buildsystem.BuildSettings;
import io.spring.initializr.generator.packaging.Packaging; import io.spring.initializr.generator.packaging.Packaging;
@@ -44,6 +45,8 @@ public class MavenBuildSettings extends BuildSettings {
private final List<MavenDeveloper> developers; private final List<MavenDeveloper> developers;
private final MavenScm scm;
private final String sourceDirectory; private final String sourceDirectory;
private final String testSourceDirectory; private final String testSourceDirectory;
@@ -56,6 +59,7 @@ public class MavenBuildSettings extends BuildSettings {
this.description = builder.description; this.description = builder.description;
this.licenses = Collections.unmodifiableList(new ArrayList<>(builder.licenses)); this.licenses = Collections.unmodifiableList(new ArrayList<>(builder.licenses));
this.developers = Collections.unmodifiableList(new ArrayList<>(builder.developers)); this.developers = Collections.unmodifiableList(new ArrayList<>(builder.developers));
this.scm = builder.scm.build();
this.sourceDirectory = builder.sourceDirectory; this.sourceDirectory = builder.sourceDirectory;
this.testSourceDirectory = builder.testSourceDirectory; this.testSourceDirectory = builder.testSourceDirectory;
} }
@@ -110,6 +114,14 @@ public class MavenBuildSettings extends BuildSettings {
return this.developers; return this.developers;
} }
/**
* Return the {@linkplain MavenScm version control} section of the project.
* @return the version control of the project
*/
public MavenScm getScm() {
return this.scm;
}
/** /**
* Return the location of main source code. Can use Maven properties such as * Return the location of main source code. Can use Maven properties such as
* {@code ${basedir}}. * {@code ${basedir}}.
@@ -147,6 +159,8 @@ public class MavenBuildSettings extends BuildSettings {
private List<MavenDeveloper> developers = new ArrayList<>(); private List<MavenDeveloper> developers = new ArrayList<>();
private final MavenScm.Builder scm = new MavenScm.Builder();
private String sourceDirectory; private String sourceDirectory;
private String testSourceDirectory; private String testSourceDirectory;
@@ -197,6 +211,16 @@ public class MavenBuildSettings extends BuildSettings {
return self(); return self();
} }
/**
* Set a human readable description of the project.
* @param description the description of the project
* @return this for method chaining
*/
public Builder description(String description) {
this.description = description;
return self();
}
/** /**
* Set the licenses of the project. * Set the licenses of the project.
* @param licenses the licenses associated with the project * @param licenses the licenses associated with the project
@@ -218,12 +242,12 @@ public class MavenBuildSettings extends BuildSettings {
} }
/** /**
* Set a human readable description of the project. * Customize the {@code scm} section using the specified consumer.
* @param description the description of the project * @param scm a consumer of the current version control section
* @return this for method chaining * @return this for method chaining
*/ */
public Builder description(String description) { public Builder scm(Consumer<MavenScm.Builder> scm) {
this.description = description; scm.accept(this.scm);
return self(); return self();
} }

View File

@@ -66,7 +66,6 @@ public class MavenBuildWriter {
*/ */
public void writeTo(IndentingWriter writer, MavenBuild build) { public void writeTo(IndentingWriter writer, MavenBuild build) {
MavenBuildSettings settings = build.getSettings(); MavenBuildSettings settings = build.getSettings();
Scm scm = build.getScm();
writeProject(writer, () -> { writeProject(writer, () -> {
writeParent(writer, build); writeParent(writer, build);
writeProjectCoordinates(writer, settings); writeProjectCoordinates(writer, settings);
@@ -74,13 +73,13 @@ public class MavenBuildWriter {
writeProjectName(writer, settings); writeProjectName(writer, settings);
writeCollectionElement(writer, "licenses", settings.getLicenses(), this::writeLicense); writeCollectionElement(writer, "licenses", settings.getLicenses(), this::writeLicense);
writeCollectionElement(writer, "developers", settings.getDevelopers(), this::writeDeveloper); writeCollectionElement(writer, "developers", settings.getDevelopers(), this::writeDeveloper);
writeScm(writer, settings.getScm());
writeProperties(writer, build.properties()); writeProperties(writer, build.properties());
writeDependencies(writer, build); writeDependencies(writer, build);
writeDependencyManagement(writer, build); writeDependencyManagement(writer, build);
writeBuild(writer, build); writeBuild(writer, build);
writeRepositories(writer, build); writeRepositories(writer, build);
writeDistributionManagement(writer, build); writeDistributionManagement(writer, build);
writeScm(writer, scm);
}); });
} }
@@ -176,6 +175,17 @@ public class MavenBuildWriter {
}); });
} }
private void writeScm(IndentingWriter writer, MavenScm mavenScm) {
if (!mavenScm.isEmpty()) {
writeElement(writer, "scm", () -> {
writeSingleElement(writer, "connection", mavenScm.getConnection());
writeSingleElement(writer, "developerConnection", mavenScm.getDeveloperConnection());
writeSingleElement(writer, "tag", mavenScm.getTag());
writeSingleElement(writer, "url", mavenScm.getUrl());
});
}
}
private void writeDependencies(IndentingWriter writer, MavenBuild build) { private void writeDependencies(IndentingWriter writer, MavenBuild build) {
if (build.dependencies().isEmpty()) { if (build.dependencies().isEmpty()) {
return; return;
@@ -470,17 +480,6 @@ public class MavenBuildWriter {
} }
} }
private void writeScm(IndentingWriter writer, Scm scm) {
if (!scm.isEmpty()) {
writeElement(writer, "scm", () -> {
writeSingleElement(writer, "connection", scm.getConnection());
writeSingleElement(writer, "developerConnection", scm.getDeveloperConnection());
writeSingleElement(writer, "tag", scm.getTag());
writeSingleElement(writer, "url", scm.getUrl());
});
}
}
private void writeSingleElement(IndentingWriter writer, String name, String text) { private void writeSingleElement(IndentingWriter writer, String name, String text) {
if (text != null) { if (text != null) {
writer.print(String.format("<%s>", name)); writer.print(String.format("<%s>", name));

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -16,14 +16,12 @@
package io.spring.initializr.generator.buildsystem.maven; package io.spring.initializr.generator.buildsystem.maven;
import java.util.Optional;
/** /**
* Maven Scm. * A version control section of a {@link MavenBuild}.
* *
* @author Joachim Pasquali * @author Joachim Pasquali
*/ */
public class Scm { public class MavenScm {
private final String connection; private final String connection;
@@ -33,7 +31,7 @@ public class Scm {
private final String url; private final String url;
protected Scm(Builder builder) { MavenScm(Builder builder) {
this.connection = builder.connection; this.connection = builder.connection;
this.developerConnection = builder.developerConnection; this.developerConnection = builder.developerConnection;
this.tag = builder.tag; this.tag = builder.tag;
@@ -41,7 +39,7 @@ public class Scm {
} }
public boolean isEmpty() { public boolean isEmpty() {
return this.connection == null && this.developerConnection == null && this.tag == null; return this.connection == null && this.developerConnection == null && this.tag == null && this.url == null;
} }
/** /**
@@ -68,7 +66,7 @@ public class Scm {
* @return the tag of current code * @return the tag of current code
*/ */
public String getTag() { public String getTag() {
return Optional.ofNullable(this.tag).orElse("HEAD"); return this.tag;
} }
/** /**
@@ -133,8 +131,8 @@ public class Scm {
return this; return this;
} }
public Scm build() { public MavenScm build() {
return new Scm(this); return new MavenScm(this);
} }
} }

View File

@@ -184,6 +184,27 @@ class MavenBuildWriterTests {
}); });
} }
@Test
void pomWithNoScm() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo").build();
generatePom(build, (pom) -> assertThat(pom.nodeAtPath("/project/scm")).isNull());
}
@Test
void pomWithScm() {
MavenBuild build = new MavenBuild();
build.settings().scm(
(scm) -> scm.connection("connection").developerConnection("developerConnection").tag("tag").url("url"));
generatePom(build, (pom) -> {
NodeAssert dependency = pom.nodeAtPath("/project/scm");
assertThat(dependency).textAtPath("connection").isEqualTo("connection");
assertThat(dependency).textAtPath("developerConnection").isEqualTo("developerConnection");
assertThat(dependency).textAtPath("tag").isEqualTo("tag");
assertThat(dependency).textAtPath("url").isEqualTo("url");
});
}
@Test @Test
void pomWithProperties() { void pomWithProperties() {
MavenBuild build = new MavenBuild(); MavenBuild build = new MavenBuild();
@@ -767,20 +788,6 @@ class MavenBuildWriterTests {
}); });
} }
@Test
void pomWithScm() {
MavenBuild build = new MavenBuild();
build.scm().connection("connection").developerConnection("developerConnection").tag("tag").url("url");
generatePom(build, (pom) -> {
NodeAssert dependency = pom.nodeAtPath("/project/scm");
assertThat(dependency).textAtPath("connection").isEqualTo("connection");
assertThat(dependency).textAtPath("developerConnection").isEqualTo("developerConnection");
assertThat(dependency).textAtPath("tag").isEqualTo("tag");
assertThat(dependency).textAtPath("url").isEqualTo("url");
});
}
private void generatePom(MavenBuild mavenBuild, Consumer<NodeAssert> consumer) { private void generatePom(MavenBuild mavenBuild, Consumer<NodeAssert> consumer) {
MavenBuildWriter writer = new MavenBuildWriter(); MavenBuildWriter writer = new MavenBuildWriter();
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -21,30 +21,32 @@ 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 Scm} * Tests for {@link MavenScm}.
* *
* @author Joachim Pasquali * @author Joachim Pasquali
*/ */
public class ScmTest { public class MavenScmTest {
@Test @Test
void emptyTest() { void isEmptyWithNoData() {
Scm scm = this.builder().build(); MavenScm mavenScm = new MavenScm.Builder().build();
assertThat(scm.isEmpty()).isTrue(); assertThat(mavenScm.isEmpty()).isTrue();
}
@Test
void isEmptyWithData() {
MavenScm mavenScm = new MavenScm.Builder().connection("some-connection").build();
assertThat(mavenScm.isEmpty()).isFalse();
} }
@Test @Test
void allElementsTest() { void allElementsTest() {
Scm scm = this.builder().connection("connection").developerConnection("developerConnection").url("url") MavenScm mavenScm = new MavenScm.Builder().connection("connection").developerConnection("developerConnection")
.tag("tag").build(); .url("url").tag("tag").build();
assertThat(scm.getConnection()).isEqualTo("connection"); assertThat(mavenScm.getConnection()).isEqualTo("connection");
assertThat(scm.getDeveloperConnection()).isEqualTo("developerConnection"); assertThat(mavenScm.getDeveloperConnection()).isEqualTo("developerConnection");
assertThat(scm.getTag()).isEqualTo("tag"); assertThat(mavenScm.getTag()).isEqualTo("tag");
assertThat(scm.getUrl()).isEqualTo("url"); assertThat(mavenScm.getUrl()).isEqualTo("url");
}
private Scm.Builder builder() {
return new Scm.Builder();
} }
} }