Add support for Maven <scm>

See gh-1051
This commit is contained in:
Joachim Pasquali
2020-01-04 12:01:54 -05:00
committed by Stephane Nicoll
parent 326fd43c37
commit 4151be0a64
5 changed files with 229 additions and 0 deletions

View File

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

View File

@@ -66,6 +66,7 @@ public class MavenBuildWriter {
*/
public void writeTo(IndentingWriter writer, MavenBuild build) {
MavenBuildSettings settings = build.getSettings();
Scm scm = build.getScm();
writeProject(writer, () -> {
writeParent(writer, build);
writeProjectCoordinates(writer, settings);
@@ -79,6 +80,7 @@ public class MavenBuildWriter {
writeBuild(writer, build);
writeRepositories(writer, build);
writeDistributionManagement(writer, build);
writeScm(writer, scm);
});
}
@@ -468,6 +470,17 @@ 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) {
if (text != null) {
writer.print(String.format("<%s>", name));

View File

@@ -0,0 +1,142 @@
/*
* 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;
/**
* Maven Scm.
*
* @author Joachim Pasquali
*/
public class Scm {
private final String connection;
private final String developerConnection;
private final String tag;
private final String url;
protected Scm(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;
}
/**
* 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 Optional.ofNullable(this.tag).orElse("HEAD");
}
/**
* 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 Scm build() {
return new Scm(this);
}
}
}

View File

@@ -767,6 +767,20 @@ 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) {
MavenBuildWriter writer = new MavenBuildWriter();
StringWriter out = new StringWriter();

View File

@@ -0,0 +1,50 @@
/*
* 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 Scm}
*
* @author Joachim Pasquali
*/
public class ScmTest {
@Test
void emptyTest() {
Scm scm = this.builder().build();
assertThat(scm.isEmpty()).isTrue();
}
@Test
void allElementsTest() {
Scm scm = this.builder().connection("connection").developerConnection("developerConnection").url("url")
.tag("tag").build();
assertThat(scm.getConnection()).isEqualTo("connection");
assertThat(scm.getDeveloperConnection()).isEqualTo("developerConnection");
assertThat(scm.getTag()).isEqualTo("tag");
assertThat(scm.getUrl()).isEqualTo("url");
}
private Scm.Builder builder() {
return new Scm.Builder();
}
}