Merge pull request #1051 from jpasquali

* pr/1051:
  Polish "Add support for Maven <scm>"
  Add support for Maven <scm>

Closes gh-1051
This commit is contained in:
Stephane Nicoll 2020-03-17 06:22:17 +01:00
commit 335cd87e7e
5 changed files with 254 additions and 5 deletions

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");
* 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.Collections;
import java.util.List;
import java.util.function.Consumer;
import io.spring.initializr.generator.buildsystem.BuildSettings;
import io.spring.initializr.generator.packaging.Packaging;
@ -44,6 +45,8 @@ public class MavenBuildSettings extends BuildSettings {
private final List<MavenDeveloper> developers;
private final MavenScm scm;
private final String sourceDirectory;
private final String testSourceDirectory;
@ -56,6 +59,7 @@ public class MavenBuildSettings extends BuildSettings {
this.description = builder.description;
this.licenses = Collections.unmodifiableList(new ArrayList<>(builder.licenses));
this.developers = Collections.unmodifiableList(new ArrayList<>(builder.developers));
this.scm = builder.scm.build();
this.sourceDirectory = builder.sourceDirectory;
this.testSourceDirectory = builder.testSourceDirectory;
}
@ -110,6 +114,14 @@ public class MavenBuildSettings extends BuildSettings {
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
* {@code ${basedir}}.
@ -147,6 +159,8 @@ public class MavenBuildSettings extends BuildSettings {
private List<MavenDeveloper> developers = new ArrayList<>();
private final MavenScm.Builder scm = new MavenScm.Builder();
private String sourceDirectory;
private String testSourceDirectory;
@ -197,6 +211,16 @@ public class MavenBuildSettings extends BuildSettings {
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.
* @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.
* @param description the description of the project
* Customize the {@code scm} section using the specified consumer.
* @param scm a consumer of the current version control section
* @return this for method chaining
*/
public Builder description(String description) {
this.description = description;
public Builder scm(Consumer<MavenScm.Builder> scm) {
scm.accept(this.scm);
return self();
}

View File

@ -73,6 +73,7 @@ public class MavenBuildWriter {
writeProjectName(writer, settings);
writeCollectionElement(writer, "licenses", settings.getLicenses(), this::writeLicense);
writeCollectionElement(writer, "developers", settings.getDevelopers(), this::writeDeveloper);
writeScm(writer, settings.getScm());
writeProperties(writer, build.properties());
writeDependencies(writer, build);
writeDependencyManagement(writer, build);
@ -174,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) {
if (build.dependencies().isEmpty()) {
return;

View File

@ -0,0 +1,140 @@
/*
* Copyright 2012-2020 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;
/**
* A version control section of a {@link MavenBuild}.
*
* @author Joachim Pasquali
*/
public class MavenScm {
private final String connection;
private final String developerConnection;
private final String tag;
private final String url;
MavenScm(Builder builder) {
this.connection = builder.connection;
this.developerConnection = builder.developerConnection;
this.tag = builder.tag;
this.url = builder.url;
}
public boolean isEmpty() {
return this.connection == null && this.developerConnection == null && this.tag == null && this.url == null;
}
/**
* Return the source control management system URL that describes the repository and
* how to connect to the repository.
* @return the source control management system URL
*/
public String getConnection() {
return this.connection;
}
/**
*
* Just like <code>connection</code>, but for developers, i.e. this scm connection
* will not be read only.
* @return the source control management system URL for developers
*/
public String getDeveloperConnection() {
return this.developerConnection;
}
/**
* The tag of current code. By default, it's set to HEAD during development.
* @return the tag of current code
*/
public String getTag() {
return this.tag;
}
/**
* The URL to the project's browsable SCM repository.
* @return the URL to the project's browsable SCM repository
*/
public String getUrl() {
return this.url;
}
public static class Builder {
private String connection;
private String developerConnection;
private String tag;
private String url;
/**
* Specify the source control management system URL that describes the repository
* and how to connect to the repository.
* @param connection the source control management system URL
* @return this for method chaining
*/
public Builder connection(String connection) {
this.connection = connection;
return this;
}
/**
* Specify the source control management system URL for developers that describes
* the repository and how to connect to the repository.
* @param developerConnection the source control management system URL for
* developers
* @return this for method chaining
*/
public Builder developerConnection(String developerConnection) {
this.developerConnection = developerConnection;
return this;
}
/**
* Specify the tag of current code. By default, it's set to HEAD during
* development.
* @param tag the tag of current code
* @return this for method chaining
*/
public Builder tag(String tag) {
this.tag = tag;
return this;
}
/**
* Specify the URL to the project's browsable SCM repository.
* @param url the URL to the project's browsable SCM repository
* @return this for method chaining
*/
public Builder url(String url) {
this.url = url;
return this;
}
public MavenScm build() {
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
void pomWithProperties() {
MavenBuild build = new MavenBuild();

View File

@ -0,0 +1,52 @@
/*
* Copyright 2012-2020 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 MavenScm}.
*
* @author Joachim Pasquali
*/
public class MavenScmTest {
@Test
void isEmptyWithNoData() {
MavenScm mavenScm = new MavenScm.Builder().build();
assertThat(mavenScm.isEmpty()).isTrue();
}
@Test
void isEmptyWithData() {
MavenScm mavenScm = new MavenScm.Builder().connection("some-connection").build();
assertThat(mavenScm.isEmpty()).isFalse();
}
@Test
void allElementsTest() {
MavenScm mavenScm = new MavenScm.Builder().connection("connection").developerConnection("developerConnection")
.url("url").tag("tag").build();
assertThat(mavenScm.getConnection()).isEqualTo("connection");
assertThat(mavenScm.getDeveloperConnection()).isEqualTo("developerConnection");
assertThat(mavenScm.getTag()).isEqualTo("tag");
assertThat(mavenScm.getUrl()).isEqualTo("url");
}
}