Add dependency classifier support

See gh-1049
This commit is contained in:
Joachim Pasquali 2019-11-13 22:26:36 -05:00 committed by Stephane Nicoll
parent aa6a994987
commit a81e4ef77b
12 changed files with 126 additions and 7 deletions

View File

@ -43,6 +43,8 @@ public class Dependency {
private final String type;
private final String classifier;
private final Set<Exclusion> exclusions;
protected Dependency(Builder<?> builder) {
@ -50,6 +52,7 @@ public class Dependency {
this.artifactId = builder.artifactId;
this.version = builder.version;
this.scope = builder.scope;
this.classifier = builder.classifier;
this.type = builder.type;
this.exclusions = new LinkedHashSet<>(builder.exclusions);
}
@ -116,6 +119,14 @@ public class Dependency {
return this.type;
}
/**
* The classifier of this dependency. Can be {@code null}
* @return the classifier or {@code null}
*/
public String getClassifier() {
return this.classifier;
}
/**
* The {@link Exclusion exclusions} to apply.
* @return the exclusions to apply
@ -140,6 +151,8 @@ public class Dependency {
private DependencyScope scope;
private String classifier;
private String type;
private Set<Exclusion> exclusions = new LinkedHashSet<>();
@ -174,6 +187,11 @@ public class Dependency {
return self();
}
public B classifier(String classifier) {
this.classifier = classifier;
return self();
}
public B exclusions(Exclusion... exclusions) {
this.exclusions = new LinkedHashSet<>(Arrays.asList(exclusions));
return self();

View File

@ -135,11 +135,13 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
String quoteStyle = determineQuoteStyle(dependency.getVersion());
String version = determineVersion(dependency.getVersion());
String type = dependency.getType();
String classifier = dependency.getClassifier();
boolean hasExclusions = !dependency.getExclusions().isEmpty();
writer.print(configurationForDependency(dependency));
writer.print((hasExclusions) ? "(" : " ");
writer.print(quoteStyle + dependency.getGroupId() + ":" + dependency.getArtifactId()
+ ((version != null) ? ":" + version : "") + ((type != null) ? "@" + type : "") + quoteStyle);
+ ((version != null) ? ":" + version : "") + ((classifier != null) ? ":" + classifier : "")
+ ((type != null) ? "@" + type : "") + quoteStyle);
if (hasExclusions) {
writer.println(") {");
writer.indented(

View File

@ -150,9 +150,10 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
protected void writeDependency(IndentingWriter writer, Dependency dependency) {
String version = determineVersion(dependency.getVersion());
String type = dependency.getType();
String classifier = dependency.getClassifier();
writer.print(configurationForDependency(dependency) + "(\"" + dependency.getGroupId() + ":"
+ dependency.getArtifactId() + ((version != null) ? ":" + version : "")
+ ((type != null) ? "@" + type : "") + "\")");
+ ((classifier != null) ? ":" + classifier : "") + ((type != null) ? "@" + type : "") + "\")");
if (!dependency.getExclusions().isEmpty()) {
writer.println(" {");
writer.indented(

View File

@ -213,6 +213,7 @@ public class MavenBuildWriter {
writeSingleElement(writer, "artifactId", dependency.getArtifactId());
writeSingleElement(writer, "version", determineVersion(dependency.getVersion()));
writeSingleElement(writer, "scope", scopeForType(dependency.getScope()));
writeSingleElement(writer, "classifier", dependency.getClassifier());
if (isOptional(dependency)) {
writeSingleElement(writer, "optional", Boolean.toString(true));
}

View File

@ -49,7 +49,8 @@ public class MavenDependency extends Dependency {
* @return a new builder initialized with the same state as the {@code dependency}
*/
public static Builder from(Dependency dependency) {
return new Builder(dependency.getGroupId(), dependency.getArtifactId()).initialize(dependency);
return new Builder(dependency.getGroupId(), dependency.getArtifactId()).initialize(dependency)
.classifier(dependency.getClassifier());
}
/**

View File

@ -305,6 +305,16 @@ class GroovyDslGradleBuildWriterTests {
" implementation 'org.springframework.boot:spring-boot-starter'", "}");
}
@Test
void gradleBuildWithClassifierDependency() {
GradleBuild build = new GradleBuild();
build.dependencies().add("root",
Dependency.withCoordinates("org.springframework.boot", "spring-boot-starter").classifier("classifier"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" implementation 'org.springframework.boot:spring-boot-starter:classifier'", "}");
}
@Test
void gradleBuildWithNoScopeDependencyDefaultsToCompile() {
GradleBuild build = new GradleBuild();
@ -397,6 +407,16 @@ class GroovyDslGradleBuildWriterTests {
" implementation 'org.springframework.boot:spring-boot-starter@tar.gz'", "}");
}
@Test
void gradleBuildWithNonNullArtifactTypeAndClassifierDependency() {
GradleBuild build = new GradleBuild();
build.dependencies().add("root", Dependency.withCoordinates("org.springframework.boot", "spring-boot-starter")
.scope(DependencyScope.COMPILE).type("tar.gz").classifier("classifier"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" implementation 'org.springframework.boot:spring-boot-starter:classifier@tar.gz'", "}");
}
@Test
void gradleBuildWithBom() {
GradleBuild build = new GradleBuild();

View File

@ -313,6 +313,16 @@ class KotlinDslGradleBuildWriterTests {
" implementation(\"org.springframework.boot:spring-boot-starter\")", "}");
}
@Test
void gradleBuildWithClassifierDependency() {
GradleBuild build = new GradleBuild();
build.dependencies().add("root", Dependency.withCoordinates("org.springframework.boot", "spring-boot-starter")
.scope(DependencyScope.COMPILE).classifier("classifier"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" implementation(\"org.springframework.boot:spring-boot-starter:classifier\")", "}");
}
@Test
void gradleBuildWithNoScopeDependencyDefaultsToCompile() {
GradleBuild build = new GradleBuild();
@ -405,6 +415,16 @@ class KotlinDslGradleBuildWriterTests {
" implementation(\"org.springframework.boot:spring-boot-starter@tar.gz\")", "}");
}
@Test
void gradleBuildWithNonNullArtifactTypeAndClassifierDependency() {
GradleBuild build = new GradleBuild();
build.dependencies().add("root", Dependency.withCoordinates("org.springframework.boot", "spring-boot-starter")
.scope(DependencyScope.COMPILE).type("tar.gz").classifier("classifier"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" implementation(\"org.springframework.boot:spring-boot-starter:classifier@tar.gz\")", "}");
}
@Test
void gradleBuildWithBom() {
GradleBuild build = new GradleBuild();

View File

@ -223,6 +223,20 @@ class MavenBuildWriterTests {
});
}
@Test
void pomWithClassifierDependency() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.dependencies().add("foo-bar", Dependency
.withCoordinates("org.springframework.boot", "spring-boot-foo-bar").classifier("myClassifier"));
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-foo-bar");
assertThat(dependency).textAtPath("classifier").isEqualTo("myClassifier");
});
}
@Test
void pomWithCompileOnlyDependency() {
MavenBuild build = new MavenBuild();

View File

@ -33,7 +33,8 @@ class MavenDependencyTests {
@Test
void initializeFromStandardDependency() {
Dependency original = Dependency.withCoordinates("com.example", "test")
.version(VersionReference.ofValue("1.0.0")).scope(DependencyScope.RUNTIME).type("zip").build();
.version(VersionReference.ofValue("1.0.0")).scope(DependencyScope.RUNTIME).type("zip")
.classifier("classifier").build();
MavenDependency dependency = MavenDependency.from(original).build();
assertThat(original).isNotSameAs(dependency);
assertThat(dependency.getGroupId()).isEqualTo("com.example");
@ -41,6 +42,7 @@ class MavenDependencyTests {
assertThat(dependency.getVersion()).isEqualTo(VersionReference.ofValue("1.0.0"));
assertThat(dependency.getScope()).isEqualTo(DependencyScope.RUNTIME);
assertThat(dependency.getType()).isEqualTo("zip");
assertThat(dependency.getClassifier()).isEqualTo("classifier");
assertThat(dependency.isOptional()).isFalse();
}
@ -48,7 +50,7 @@ class MavenDependencyTests {
void initializeFromMavenDependency() {
Dependency original = MavenDependency.withCoordinates("com.example", "test")
.version(VersionReference.ofValue("1.0.0")).scope(DependencyScope.RUNTIME).type("zip").optional(true)
.build();
.classifier("classifier").build();
MavenDependency dependency = MavenDependency.from(original).build();
assertThat(original).isNotSameAs(dependency);
assertThat(dependency.getGroupId()).isEqualTo("com.example");
@ -56,7 +58,18 @@ class MavenDependencyTests {
assertThat(dependency.getVersion()).isEqualTo(VersionReference.ofValue("1.0.0"));
assertThat(dependency.getScope()).isEqualTo(DependencyScope.RUNTIME);
assertThat(dependency.getType()).isEqualTo("zip");
assertThat(dependency.getClassifier()).isEqualTo("classifier");
assertThat(dependency.isOptional()).isTrue();
}
@Test
void cloneUsingFromMethodOfMavenDependency() {
Dependency original = Dependency.withCoordinates("com.example", "test").classifier("classifier").build();
Dependency dependency = MavenDependency.from(original).build();
assertThat(original).isNotSameAs(dependency);
assertThat(dependency.getGroupId()).isEqualTo("com.example");
assertThat(dependency.getArtifactId()).isEqualTo("test");
assertThat(dependency.getClassifier()).isEqualTo("classifier");
}
}

View File

@ -89,6 +89,8 @@ public class Dependency extends MetadataElement implements Describable {
private String type;
private String classifier;
private List<Mapping> mappings = new ArrayList<>();
private String scope = SCOPE_COMPILE;
@ -143,6 +145,7 @@ public class Dependency extends MetadataElement implements Describable {
this.starter = dependency.starter;
this.keywords.addAll(dependency.keywords);
this.links.addAll(dependency.links);
this.classifier = dependency.classifier;
}
public void setScope(String scope) {
@ -325,6 +328,14 @@ public class Dependency extends MetadataElement implements Describable {
this.version = version;
}
public String getClassifier() {
return this.classifier;
}
public void setClassifier(String classifier) {
this.classifier = classifier;
}
/**
* Return the type, can be {@code null} to indicate that the default type should be
* used (i.e. {@code jar}).
@ -458,12 +469,18 @@ public class Dependency extends MetadataElement implements Describable {
}
public static Dependency withId(String id, String groupId, String artifactId, String version, String scope) {
return withId(id, groupId, artifactId, version, scope, null);
}
public static Dependency withId(String id, String groupId, String artifactId, String version, String scope,
String classifier) {
Dependency dependency = new Dependency();
dependency.setId(id);
dependency.groupId = groupId;
dependency.artifactId = artifactId;
dependency.version = version;
dependency.scope = (scope != null) ? scope : SCOPE_COMPILE;
dependency.classifier = classifier;
return dependency;
}
@ -510,6 +527,8 @@ public class Dependency extends MetadataElement implements Describable {
*/
private String version;
private String classifier;
/**
* The starter setting to use for the mapping or {@code null} to use the default.
*/
@ -542,6 +561,14 @@ public class Dependency extends MetadataElement implements Describable {
this.version = version;
}
public String getClassifier() {
return this.classifier;
}
public void setClassifier(String classifier) {
this.classifier = classifier;
}
public Boolean getStarter() {
return this.starter;
}

View File

@ -46,7 +46,8 @@ public final class MetadataBuildItemMapper {
? VersionReference.ofValue(dependency.getVersion()) : null;
return io.spring.initializr.generator.buildsystem.Dependency
.withCoordinates(dependency.getGroupId(), dependency.getArtifactId()).version(versionReference)
.scope(toDependencyScope(dependency.getScope())).type(dependency.getType()).build();
.scope(toDependencyScope(dependency.getScope())).type(dependency.getType())
.classifier(dependency.getClassifier()).build();
}
private static DependencyScope toDependencyScope(String scope) {

View File

@ -45,7 +45,7 @@ class MetadataBuildItemResolverTests {
void resoleDependencyWithMatchingEntry() {
InitializrMetadata metadata = new InitializrMetadata();
DependencyGroup group = DependencyGroup.create("test");
group.getContent().add(Dependency.withId("test-dep", "com.example", "test", "1.0.0", "runtime"));
group.getContent().add(Dependency.withId("test-dep", "com.example", "test", "1.0.0", "runtime", "classifier"));
metadata.getDependencies().getContent().add(group);
metadata.validate();
MetadataBuildItemResolver resolver = new MetadataBuildItemResolver(metadata, VERSION_2_0_0);
@ -53,6 +53,7 @@ class MetadataBuildItemResolverTests {
assertThat(dependency.getGroupId()).isEqualTo("com.example");
assertThat(dependency.getArtifactId()).isEqualTo("test");
assertThat(dependency.getVersion()).hasToString("1.0.0");
assertThat(dependency.getClassifier()).hasToString("classifier");
assertThat(dependency.getScope()).isEqualTo(DependencyScope.RUNTIME);
}