mirror of
https://gitee.com/dcren/initializr.git
synced 2026-02-26 05:32:58 +08:00
Polish "Add dependency classifier support"
See gh-1049
This commit is contained in:
@@ -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.
|
||||
@@ -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) {
|
||||
@@ -110,6 +110,14 @@ public class Dependency {
|
||||
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}).
|
||||
@@ -119,14 +127,6 @@ 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
|
||||
@@ -151,10 +151,10 @@ public class Dependency {
|
||||
|
||||
private DependencyScope scope;
|
||||
|
||||
private String classifier;
|
||||
|
||||
private String type;
|
||||
|
||||
private String classifier;
|
||||
|
||||
private Set<Exclusion> exclusions = new LinkedHashSet<>();
|
||||
|
||||
protected Builder(String groupId, String artifactId) {
|
||||
@@ -182,13 +182,13 @@ public class Dependency {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -208,8 +208,8 @@ public class Dependency {
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
@@ -134,8 +134,8 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
|
||||
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) ? "(" : " ");
|
||||
|
||||
@@ -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.
|
||||
@@ -149,8 +149,8 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
|
||||
@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 : "") + "\")");
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -49,8 +49,7 @@ 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)
|
||||
.classifier(dependency.getClassifier());
|
||||
return new Builder(dependency.getGroupId(), dependency.getArtifactId()).initialize(dependency);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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.
|
||||
@@ -36,6 +36,7 @@ class DependencyTests {
|
||||
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();
|
||||
}
|
||||
@@ -48,6 +49,19 @@ class DependencyTests {
|
||||
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();
|
||||
}
|
||||
@@ -59,6 +73,7 @@ class DependencyTests {
|
||||
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();
|
||||
}
|
||||
@@ -71,6 +86,7 @@ class DependencyTests {
|
||||
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"));
|
||||
|
||||
@@ -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.
|
||||
@@ -305,16 +305,6 @@ 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();
|
||||
@@ -373,6 +363,14 @@ class GroovyDslGradleBuildWriterTests {
|
||||
" 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();
|
||||
@@ -410,11 +408,11 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@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:classifier@tar.gz'", "}");
|
||||
assertThat(lines).containsSequence("dependencies {", " implementation 'com.example:acme:test-jar@tar.gz'",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -313,16 +313,6 @@ 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();
|
||||
@@ -381,6 +371,15 @@ class KotlinDslGradleBuildWriterTests {
|
||||
" 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();
|
||||
@@ -418,11 +417,11 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@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:classifier@tar.gz\")", "}");
|
||||
assertThat(lines).containsSequence("dependencies {", " implementation(\"com.example:acme:test-jar@tar.gz\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -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.
|
||||
@@ -223,20 +223,6 @@ 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();
|
||||
@@ -346,6 +332,19 @@ class MavenBuildWriterTests {
|
||||
});
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
@@ -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.
|
||||
@@ -33,24 +33,24 @@ class MavenDependencyTests {
|
||||
@Test
|
||||
void initializeFromStandardDependency() {
|
||||
Dependency original = Dependency.withCoordinates("com.example", "test")
|
||||
.version(VersionReference.ofValue("1.0.0")).scope(DependencyScope.RUNTIME).type("zip")
|
||||
.classifier("classifier").build();
|
||||
.version(VersionReference.ofValue("1.0.0")).scope(DependencyScope.RUNTIME).classifier("test-jar")
|
||||
.type("zip").build();
|
||||
MavenDependency dependency = MavenDependency.from(original).build();
|
||||
assertThat(original).isNotSameAs(dependency);
|
||||
assertThat(dependency.getGroupId()).isEqualTo("com.example");
|
||||
assertThat(dependency.getArtifactId()).isEqualTo("test");
|
||||
assertThat(dependency.getVersion()).isEqualTo(VersionReference.ofValue("1.0.0"));
|
||||
assertThat(dependency.getScope()).isEqualTo(DependencyScope.RUNTIME);
|
||||
assertThat(dependency.getClassifier()).isEqualTo("test-jar");
|
||||
assertThat(dependency.getType()).isEqualTo("zip");
|
||||
assertThat(dependency.getClassifier()).isEqualTo("classifier");
|
||||
assertThat(dependency.isOptional()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializeFromMavenDependency() {
|
||||
Dependency original = MavenDependency.withCoordinates("com.example", "test")
|
||||
.version(VersionReference.ofValue("1.0.0")).scope(DependencyScope.RUNTIME).type("zip").optional(true)
|
||||
.classifier("classifier").build();
|
||||
.version(VersionReference.ofValue("1.0.0")).scope(DependencyScope.RUNTIME).classifier("test-jar")
|
||||
.type("zip").optional(true).build();
|
||||
MavenDependency dependency = MavenDependency.from(original).build();
|
||||
assertThat(original).isNotSameAs(dependency);
|
||||
assertThat(dependency.getGroupId()).isEqualTo("com.example");
|
||||
@@ -58,18 +58,8 @@ 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.getClassifier()).isEqualTo("test-jar");
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user