Skip to content

Commit

Permalink
Polish "Add dependency classifier support"
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Mar 17, 2020
1 parent a81e4ef commit 835f699
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ with that id is assumed.
* A `version` if Spring Boot does not already provide a dependency management for
that dependency.
* A `scope` (can be `compile`, `runtime`, `provided` or `test`).
* A `classifier` (if the dependency to use is classified, such as `test-jar`).
* The reference to a `bom` or a `repository` that must be added to the project once
that dependency is added.
* A `compatibilityRange` used to determine the platform versions that are compatible
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -41,10 +41,10 @@ public class Dependency {

private final DependencyScope scope;

private final String type;

private final String classifier;

private final String type;

private final Set<Exclusion> exclusions;

protected Dependency(Builder<?> builder) {
Expand Down Expand Up @@ -110,6 +110,14 @@ public DependencyScope getScope() {
return this.scope;
}

/**
* The classifier of this dependency. Can be {@code null}
* @return the classifier or {@code null}
*/
public String getClassifier() {
return this.classifier;
}

/**
* The type of the dependency. Can be {@code null} to indicate that the default type
* should be used (i.e. {@code jar}).
Expand All @@ -119,14 +127,6 @@ public String getType() {
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
Expand All @@ -151,10 +151,10 @@ public static class Builder<B extends Builder<B>> {

private DependencyScope scope;

private String classifier;

private String type;

private String classifier;

private Set<Exclusion> exclusions = new LinkedHashSet<>();

protected Builder(String groupId, String artifactId) {
Expand Down Expand Up @@ -182,13 +182,13 @@ public B scope(DependencyScope scope) {
return self();
}

public B type(String type) {
this.type = type;
public B classifier(String classifier) {
this.classifier = classifier;
return self();
}

public B classifier(String classifier) {
this.classifier = classifier;
public B type(String type) {
this.type = type;
return self();
}

Expand All @@ -208,8 +208,8 @@ protected B self() {
}

protected B initialize(Dependency dependency) {
version(dependency.getVersion()).scope(dependency.getScope()).type(dependency.getType())
.exclusions(dependency.getExclusions());
version(dependency.getVersion()).scope(dependency.getScope()).classifier(dependency.getClassifier())
.type(dependency.getType()).exclusions(dependency.getExclusions());
return self();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -134,8 +134,8 @@ protected void writeConfiguration(IndentingWriter writer, GradleConfiguration co
protected void writeDependency(IndentingWriter writer, Dependency dependency) {
String quoteStyle = determineQuoteStyle(dependency.getVersion());
String version = determineVersion(dependency.getVersion());
String type = dependency.getType();
String classifier = dependency.getClassifier();
String type = dependency.getType();
boolean hasExclusions = !dependency.getExclusions().isEmpty();
writer.print(configurationForDependency(dependency));
writer.print((hasExclusions) ? "(" : " ");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -149,8 +149,8 @@ protected void writeConfiguration(IndentingWriter writer, GradleConfiguration co
@Override
protected void writeDependency(IndentingWriter writer, Dependency dependency) {
String version = determineVersion(dependency.getVersion());
String type = dependency.getType();
String classifier = dependency.getClassifier();
String type = dependency.getType();
writer.print(configurationForDependency(dependency) + "(\"" + dependency.getGroupId() + ":"
+ dependency.getArtifactId() + ((version != null) ? ":" + version : "")
+ ((classifier != null) ? ":" + classifier : "") + ((type != null) ? "@" + type : "") + "\")");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public static Builder withCoordinates(String groupId, String artifactId) {
* @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)
.classifier(dependency.getClassifier());
return new Builder(dependency.getGroupId(), dependency.getArtifactId()).initialize(dependency);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -36,6 +36,7 @@ void dependencyWithCoordinatesOnly() {
assertThat(dependency.getArtifactId()).isEqualTo("acme");
assertThat(dependency.getScope()).isNull();
assertThat(dependency.getVersion()).isNull();
assertThat(dependency.getClassifier()).isNull();
assertThat(dependency.getType()).isNull();
assertThat(dependency.getExclusions()).isEmpty();
}
Expand All @@ -48,6 +49,19 @@ void dependencyWithScopeAndVersionValue() {
assertThat(dependency.getArtifactId()).isEqualTo("acme");
assertThat(dependency.getScope()).isEqualTo(DependencyScope.RUNTIME);
assertThat(dependency.getVersion().getValue()).isEqualTo("1.0.0");
assertThat(dependency.getClassifier()).isNull();
assertThat(dependency.getType()).isNull();
assertThat(dependency.getExclusions()).isEmpty();
}

@Test
void dependencyWithClassifier() {
Dependency dependency = Dependency.withCoordinates("com.example", "acme").classifier("test").build();
assertThat(dependency.getGroupId()).isEqualTo("com.example");
assertThat(dependency.getArtifactId()).isEqualTo("acme");
assertThat(dependency.getScope()).isNull();
assertThat(dependency.getVersion()).isNull();
assertThat(dependency.getClassifier()).isEqualTo("test");
assertThat(dependency.getType()).isNull();
assertThat(dependency.getExclusions()).isEmpty();
}
Expand All @@ -59,6 +73,7 @@ void dependencyWithType() {
assertThat(dependency.getArtifactId()).isEqualTo("acme");
assertThat(dependency.getScope()).isNull();
assertThat(dependency.getVersion()).isNull();
assertThat(dependency.getClassifier()).isNull();
assertThat(dependency.getType()).isEqualTo("test-zip");
assertThat(dependency.getExclusions()).isEmpty();
}
Expand All @@ -71,6 +86,7 @@ void dependencyWithExclusions() {
assertThat(dependency.getArtifactId()).isEqualTo("acme");
assertThat(dependency.getScope()).isNull();
assertThat(dependency.getVersion()).isNull();
assertThat(dependency.getClassifier()).isNull();
assertThat(dependency.getType()).isNull();
assertThat(dependency.getExclusions()).containsExactly(new Exclusion("com.example", "exclude1"),
new Exclusion("com.example", "exclude2"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -305,16 +305,6 @@ void gradleBuildWithCompileDependency() {
" 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();
Expand Down Expand Up @@ -373,6 +363,14 @@ void gradleBuildWithTestRuntimeDependency() {
" testRuntimeOnly 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'", "}");
}

@Test
void gradleBuildWithClassifierDependency() {
GradleBuild build = new GradleBuild();
build.dependencies().add("root", Dependency.withCoordinates("com.example", "acme").classifier("test-jar"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {", " implementation 'com.example:acme:test-jar'", "}");
}

@Test
void gradleBuildWithExclusions() {
GradleBuild build = new GradleBuild();
Expand Down Expand Up @@ -410,11 +408,11 @@ void gradleBuildWithNonNullArtifactTypeDependency() {
@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"));
build.dependencies().add("root", Dependency.withCoordinates("com.example", "acme")
.scope(DependencyScope.COMPILE).type("tar.gz").classifier("test-jar"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" implementation 'org.springframework.boot:spring-boot-starter:[email protected]'", "}");
assertThat(lines).containsSequence("dependencies {", " implementation 'com.example:acme:[email protected]'",
"}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -313,16 +313,6 @@ void gradleBuildWithCompileDependency() {
" 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();
Expand Down Expand Up @@ -381,6 +371,15 @@ void gradleBuildWithTestRuntimeDependency() {
" testRuntimeOnly(\"de.flapdoodle.embed:de.flapdoodle.embed.mongo\")", "}");
}

@Test
void gradleBuildWithClassifierDependency() {
GradleBuild build = new GradleBuild();
build.dependencies().add("root", Dependency.withCoordinates("com.example", "acme")
.scope(DependencyScope.COMPILE).classifier("test-jar"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {", " implementation(\"com.example:acme:test-jar\")", "}");
}

@Test
void gradleBuildWithExclusions() {
GradleBuild build = new GradleBuild();
Expand Down Expand Up @@ -418,11 +417,11 @@ void gradleBuildWithNonNullArtifactTypeDependency() {
@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"));
build.dependencies().add("root", Dependency.withCoordinates("com.example", "acme")
.scope(DependencyScope.COMPILE).type("tar.gz").classifier("test-jar"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("dependencies {",
" implementation(\"org.springframework.boot:spring-boot-starter:[email protected]\")", "}");
assertThat(lines).containsSequence("dependencies {", " implementation(\"com.example:acme:[email protected]\")",
"}");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -223,20 +223,6 @@ void pomWithAnnotationProcessorDependency() {
});
}

@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();
Expand Down Expand Up @@ -346,6 +332,19 @@ void pomWithTestRuntimeDependency() {
});
}

@Test
void pomWithClassifierDependency() {
MavenBuild build = new MavenBuild();
build.settings().coordinates("com.example.demo", "demo");
build.dependencies().add("foo-bar", Dependency.withCoordinates("com.example", "acme").classifier("test-jar"));
generatePom(build, (pom) -> {
NodeAssert dependency = pom.nodeAtPath("/project/dependencies/dependency");
assertThat(dependency).textAtPath("groupId").isEqualTo("com.example");
assertThat(dependency).textAtPath("artifactId").isEqualTo("acme");
assertThat(dependency).textAtPath("classifier").isEqualTo("test-jar");
});
}

@Test
void pomWithExclusions() {
MavenBuild build = new MavenBuild();
Expand Down
Loading

0 comments on commit 835f699

Please sign in to comment.