Model that a Maven dependency is optional

Closes gh-913
This commit is contained in:
Stephane Nicoll
2019-05-31 09:35:43 +02:00
parent e6f287b298
commit d32b2f6a20
5 changed files with 121 additions and 21 deletions

View File

@@ -45,7 +45,7 @@ public class Dependency {
private final Set<Exclusion> exclusions;
Dependency(Builder builder) {
protected Dependency(Builder<?> builder) {
this.groupId = builder.groupId;
this.artifactId = builder.artifactId;
this.version = builder.version;
@@ -54,7 +54,7 @@ public class Dependency {
this.exclusions = new LinkedHashSet<>(builder.exclusions);
}
public static Builder withCoordinates(String groupId, String artifactId) {
public static Builder<?> withCoordinates(String groupId, String artifactId) {
return new Builder(groupId, artifactId);
}
@@ -111,9 +111,10 @@ public class Dependency {
/**
* Builder for a dependency.
*
* @param <B> builder type
* @see Dependency#withCoordinates(String, String)
*/
public static final class Builder {
public static class Builder<B extends Builder> {
private String groupId;
@@ -127,39 +128,44 @@ public class Dependency {
private Set<Exclusion> exclusions = new LinkedHashSet<>();
private Builder(String groupId, String artifactId) {
protected Builder(String groupId, String artifactId) {
this.groupId = groupId;
this.artifactId = artifactId;
}
public Builder groupId(String groupId) {
public B groupId(String groupId) {
this.groupId = groupId;
return this;
return self();
}
public Builder artifactId(String artifactId) {
public B artifactId(String artifactId) {
this.artifactId = artifactId;
return this;
return self();
}
public Builder version(VersionReference version) {
public B version(VersionReference version) {
this.version = version;
return this;
return self();
}
public Builder scope(DependencyScope scope) {
public B scope(DependencyScope scope) {
this.scope = scope;
return this;
return self();
}
public Builder type(String type) {
public B type(String type) {
this.type = type;
return this;
return self();
}
public Builder exclusions(Exclusion... exclusions) {
public B exclusions(Exclusion... exclusions) {
this.exclusions = new LinkedHashSet<>(Arrays.asList(exclusions));
return this;
return self();
}
@SuppressWarnings("unchecked")
protected B self() {
return (B) this;
}
public Dependency build() {

View File

@@ -47,7 +47,7 @@ public class DependencyContainer extends BuildItemContainer<String, Dependency>
* @param id the id of the dependency
* @param builder the state of the dependency
*/
public void add(String id, Dependency.Builder builder) {
public void add(String id, Dependency.Builder<?> builder) {
add(id, builder.build());
}

View File

@@ -155,7 +155,7 @@ public class MavenBuildWriter {
writeSingleElement(writer, "version",
determineVersion(dependency.getVersion()));
writeSingleElement(writer, "scope", scopeForType(dependency.getScope()));
if (isOptional(dependency.getScope())) {
if (isOptional(dependency)) {
writeSingleElement(writer, "optional", Boolean.toString(true));
}
writeSingleElement(writer, "type", dependency.getType());
@@ -202,9 +202,13 @@ public class MavenBuildWriter {
}
}
private boolean isOptional(DependencyScope type) {
return (type == DependencyScope.ANNOTATION_PROCESSOR
|| type == DependencyScope.COMPILE_ONLY);
private boolean isOptional(Dependency dependency) {
if (dependency instanceof MavenDependency
&& ((MavenDependency) dependency).isOptional()) {
return true;
}
return (dependency.getScope() == DependencyScope.ANNOTATION_PROCESSOR
|| dependency.getScope() == DependencyScope.COMPILE_ONLY);
}
private void writeDependencyManagement(IndentingWriter writer, MavenBuild build) {

View File

@@ -0,0 +1,68 @@
/*
* 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 io.spring.initializr.generator.buildsystem.Dependency;
/**
* A {@link Dependency} with specific settings for the Maven build system.
*
* @author Stephane Nicoll
*/
public class MavenDependency extends Dependency {
protected final boolean optional;
protected MavenDependency(Builder builder) {
super(builder);
this.optional = builder.optional;
}
public static Builder withCoordinates(String groupId, String artifactId) {
return new Builder(groupId, artifactId);
}
public boolean isOptional() {
return this.optional;
}
/**
* Builder for a Maven dependency.
*
* @see MavenDependency#withCoordinates(String, String)
*/
public static class Builder extends Dependency.Builder<Builder> {
private boolean optional;
protected Builder(String groupId, String artifactId) {
super(groupId, artifactId);
}
public Builder optional(boolean optional) {
this.optional = optional;
return self();
}
@Override
public Dependency build() {
return new MavenDependency(this);
}
}
}

View File

@@ -287,6 +287,28 @@ class MavenBuildWriterTests {
});
}
@Test
void pomWithOptionalDependency() throws Exception {
MavenBuild build = new MavenBuild();
build.setGroup("com.example.demo");
build.setArtifact("demo");
build.dependencies().add("annotation-processor",
MavenDependency
.withCoordinates("org.springframework.boot",
"spring-boot-configuration-processor")
.scope(DependencyScope.COMPILE).optional(true));
generatePom(build, (pom) -> {
NodeAssert dependency = pom.nodeAtPath("/project/dependencies/dependency");
assertThat(dependency).textAtPath("groupId")
.isEqualTo("org.springframework.boot");
assertThat(dependency).textAtPath("artifactId")
.isEqualTo("spring-boot-configuration-processor");
assertThat(dependency).textAtPath("version").isNullOrEmpty();
assertThat(dependency).textAtPath("scope").isNullOrEmpty();
assertThat(dependency).textAtPath("optional").isEqualTo("true");
});
}
@Test
void pomWithNonNullArtifactTypeDependency() throws Exception {
MavenBuild build = new MavenBuild();