mirror of
https://gitee.com/dcren/initializr.git
synced 2025-04-05 17:38:06 +08:00
Add dependency builder
This commit adds a builder for Dependency to allow it to be further tuned without having to add an extra constructor.
This commit is contained in:
parent
7a30f771f2
commit
b8cac0407b
@ -17,6 +17,7 @@
|
||||
package io.spring.initializr.generator.spring.build;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Build;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
|
||||
@ -56,10 +57,11 @@ public class BuildProjectGenerationConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnPlatformVersion("2.2.0.M3")
|
||||
public BuildCustomizer<Build> junitJupiterTestStarterContributor() {
|
||||
return (build) -> build.dependencies().add("test", "org.springframework.boot",
|
||||
"spring-boot-starter-test", null, DependencyScope.TEST_COMPILE, null,
|
||||
new Exclusion("org.junit.vintage", "junit-vintage-engine"),
|
||||
new Exclusion("junit", "junit"));
|
||||
return (build) -> build.dependencies().add("test", Dependency
|
||||
.withCoordinates("org.springframework.boot", "spring-boot-starter-test")
|
||||
.scope(DependencyScope.TEST_COMPILE)
|
||||
.exclusions(new Exclusion("org.junit.vintage", "junit-vintage-engine"),
|
||||
new Exclusion("junit", "junit")));
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -17,6 +17,7 @@
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Build;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
|
||||
import io.spring.initializr.generator.spring.build.BuildCustomizer;
|
||||
@ -40,10 +41,13 @@ class KotlinDependenciesConfigurer implements BuildCustomizer<Build> {
|
||||
@Override
|
||||
public void customize(Build build) {
|
||||
VersionReference version = determineDependencyVersion(build);
|
||||
build.dependencies().add("kotlin-stdlib", "org.jetbrains.kotlin",
|
||||
"kotlin-stdlib-jdk8", version, DependencyScope.COMPILE);
|
||||
build.dependencies().add("kotlin-reflect", "org.jetbrains.kotlin",
|
||||
"kotlin-reflect", version, DependencyScope.COMPILE);
|
||||
build.dependencies()
|
||||
.add("kotlin-stdlib", Dependency
|
||||
.withCoordinates("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
|
||||
.version(version).scope(DependencyScope.COMPILE));
|
||||
build.dependencies().add("kotlin-reflect",
|
||||
Dependency.withCoordinates("org.jetbrains.kotlin", "kotlin-reflect")
|
||||
.version(version).scope(DependencyScope.COMPILE));
|
||||
}
|
||||
|
||||
private VersionReference determineDependencyVersion(Build build) {
|
||||
|
@ -64,9 +64,10 @@ class SimpleBuildCustomizerTests {
|
||||
@Test
|
||||
void customizeDependencies() {
|
||||
ProjectDescription description = initializeDescription();
|
||||
Dependency one = new Dependency("com.example", "one", DependencyScope.COMPILE);
|
||||
Dependency two = new Dependency("com.example.acme", "two",
|
||||
DependencyScope.COMPILE);
|
||||
Dependency one = Dependency.withCoordinates("com.example", "one")
|
||||
.scope(DependencyScope.COMPILE).build();
|
||||
Dependency two = Dependency.withCoordinates("com.example.acme", "two")
|
||||
.scope(DependencyScope.COMPILE).build();
|
||||
description.addDependency("two", two);
|
||||
description.addDependency("one", one);
|
||||
MavenBuild build = customizeBuild(description);
|
||||
|
@ -117,7 +117,8 @@ class GradleKtsProjectGenerationConfigurationTests {
|
||||
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
|
||||
description.setLanguage(new JavaLanguage("11"));
|
||||
description.addDependency("acme",
|
||||
new Dependency("com.example", "acme", DependencyScope.COMPILE));
|
||||
Dependency.withCoordinates("com.example", "acme")
|
||||
.scope(DependencyScope.COMPILE).build());
|
||||
ProjectStructure projectStructure = this.projectTester.generate(description);
|
||||
assertThat(projectStructure.getRelativePathsOfProjectFiles())
|
||||
.contains("build.gradle.kts");
|
||||
|
@ -121,7 +121,8 @@ class GradleProjectGenerationConfigurationTests {
|
||||
description.setPlatformVersion(Version.parse("2.1.0.RELEASE"));
|
||||
description.setLanguage(new JavaLanguage("11"));
|
||||
description.addDependency("acme",
|
||||
new Dependency("com.example", "acme", DependencyScope.COMPILE));
|
||||
Dependency.withCoordinates("com.example", "acme")
|
||||
.scope(DependencyScope.COMPILE).build());
|
||||
ProjectStructure projectStructure = this.projectTester.generate(description);
|
||||
List<String> relativePaths = projectStructure.getRelativePathsOfProjectFiles();
|
||||
assertThat(relativePaths).contains("build.gradle");
|
||||
|
@ -16,7 +16,8 @@
|
||||
|
||||
package io.spring.initializr.generator.buildsystem;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@ -44,32 +45,17 @@ public class Dependency {
|
||||
|
||||
private final Set<Exclusion> exclusions;
|
||||
|
||||
public Dependency(String groupId, String artifactId) {
|
||||
this(groupId, artifactId, DependencyScope.COMPILE);
|
||||
Dependency(Builder builder) {
|
||||
this.groupId = builder.groupId;
|
||||
this.artifactId = builder.artifactId;
|
||||
this.version = builder.version;
|
||||
this.scope = builder.scope;
|
||||
this.type = builder.type;
|
||||
this.exclusions = new LinkedHashSet<>(builder.exclusions);
|
||||
}
|
||||
|
||||
public Dependency(String groupId, String artifactId, DependencyScope scope) {
|
||||
this(groupId, artifactId, null, scope);
|
||||
}
|
||||
|
||||
public Dependency(String groupId, String artifactId, VersionReference version,
|
||||
DependencyScope scope) {
|
||||
this(groupId, artifactId, version, scope, null);
|
||||
}
|
||||
|
||||
public Dependency(String groupId, String artifactId, VersionReference version,
|
||||
DependencyScope scope, String type) {
|
||||
this(groupId, artifactId, version, scope, type, null);
|
||||
}
|
||||
|
||||
public Dependency(String groupId, String artifactId, VersionReference version,
|
||||
DependencyScope scope, String type, Set<Exclusion> exclusions) {
|
||||
this.groupId = groupId;
|
||||
this.artifactId = artifactId;
|
||||
this.version = version;
|
||||
this.scope = scope;
|
||||
this.type = type;
|
||||
this.exclusions = (exclusions != null) ? exclusions : Collections.emptySet();
|
||||
public static Builder withCoordinates(String groupId, String artifactId) {
|
||||
return new Builder(groupId, artifactId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -122,6 +108,66 @@ public class Dependency {
|
||||
return this.exclusions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for a dependency.
|
||||
*
|
||||
* @see Dependency#withCoordinates(String, String)
|
||||
*/
|
||||
public static final class Builder {
|
||||
|
||||
private String groupId;
|
||||
|
||||
private String artifactId;
|
||||
|
||||
private VersionReference version;
|
||||
|
||||
private DependencyScope scope;
|
||||
|
||||
private String type;
|
||||
|
||||
private Set<Exclusion> exclusions = new LinkedHashSet<>();
|
||||
|
||||
private Builder(String groupId, String artifactId) {
|
||||
this.groupId = groupId;
|
||||
this.artifactId = artifactId;
|
||||
}
|
||||
|
||||
public Builder groupId(String groupId) {
|
||||
this.groupId = groupId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder artifactId(String artifactId) {
|
||||
this.artifactId = artifactId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder version(VersionReference version) {
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder scope(DependencyScope scope) {
|
||||
this.scope = scope;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder type(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder exclusions(Exclusion... exclusions) {
|
||||
this.exclusions = new LinkedHashSet<>(Arrays.asList(exclusions));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Dependency build() {
|
||||
return new Dependency(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the reference to a transitive dependency to exclude.
|
||||
*/
|
||||
|
@ -16,14 +16,9 @@
|
||||
|
||||
package io.spring.initializr.generator.buildsystem;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.function.Function;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
|
||||
/**
|
||||
* A {@link BuildItemContainer} implementation for dependencies.
|
||||
*
|
||||
@ -43,54 +38,17 @@ public class DependencyContainer extends BuildItemContainer<String, Dependency>
|
||||
* @param scope the {@link DependencyScope}
|
||||
*/
|
||||
public void add(String id, String groupId, String artifactId, DependencyScope scope) {
|
||||
add(id, new Dependency(groupId, artifactId, scope));
|
||||
add(id, Dependency.withCoordinates(groupId, artifactId).scope(scope));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link Dependency} with the specified {@code id}, version and
|
||||
* {@link DependencyScope scope}.
|
||||
* Register a {@link Dependency} with the specified {@code id} and
|
||||
* {@link Dependency.Builder state}.
|
||||
* @param id the id of the dependency
|
||||
* @param groupId the groupId
|
||||
* @param artifactId the artifactId
|
||||
* @param version the {@link VersionReference}
|
||||
* @param scope the {@link DependencyScope}
|
||||
* @param builder the state of the dependency
|
||||
*/
|
||||
public void add(String id, String groupId, String artifactId,
|
||||
VersionReference version, DependencyScope scope) {
|
||||
add(id, new Dependency(groupId, artifactId, version, scope));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link Dependency} with the specified {@code id}, version,
|
||||
* {@link DependencyScope scope} and type.
|
||||
* @param id the id of the dependency
|
||||
* @param groupId the groupId
|
||||
* @param artifactId the artifactId
|
||||
* @param version the {@link VersionReference}
|
||||
* @param scope the {@link DependencyScope}
|
||||
* @param type the artifact type
|
||||
*/
|
||||
public void add(String id, String groupId, String artifactId,
|
||||
VersionReference version, DependencyScope scope, String type) {
|
||||
add(id, new Dependency(groupId, artifactId, version, scope, type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link Dependency} with the specified {@code id}, version,
|
||||
* {@link DependencyScope scope}, type and exclusions.
|
||||
* @param id the id of the dependency
|
||||
* @param groupId the groupId
|
||||
* @param artifactId the artifactId
|
||||
* @param version the {@link VersionReference}
|
||||
* @param scope the {@link DependencyScope}
|
||||
* @param type the artifact type
|
||||
* @param exclusions the exclusion(s) to apply
|
||||
*/
|
||||
public void add(String id, String groupId, String artifactId,
|
||||
VersionReference version, DependencyScope scope, String type,
|
||||
Exclusion... exclusions) {
|
||||
add(id, new Dependency(groupId, artifactId, version, scope, type,
|
||||
new LinkedHashSet<>(Arrays.asList(exclusions))));
|
||||
public void add(String id, Dependency.Builder builder) {
|
||||
add(id, builder.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,16 +27,17 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class DependencyComparatorTests {
|
||||
|
||||
private static final Dependency SPRING_BOOT_A = new Dependency(
|
||||
"org.springframework.boot", "spring-boot-a");
|
||||
private static final Dependency SPRING_BOOT_A = Dependency
|
||||
.withCoordinates("org.springframework.boot", "spring-boot-a").build();
|
||||
|
||||
private static final Dependency SPRING_BOOT_B = new Dependency(
|
||||
"org.springframework.boot", "spring-boot-b");
|
||||
private static final Dependency SPRING_BOOT_B = Dependency
|
||||
.withCoordinates("org.springframework.boot", "spring-boot-b").build();
|
||||
|
||||
private static final Dependency LIB_ALPHA = new Dependency("com.example.alpha",
|
||||
"test");
|
||||
private static final Dependency LIB_ALPHA = Dependency
|
||||
.withCoordinates("com.example.alpha", "test").build();
|
||||
|
||||
private static final Dependency LIB_BETA = new Dependency("com.example.beta", "test");
|
||||
private static final Dependency LIB_BETA = Dependency
|
||||
.withCoordinates("com.example.beta", "test").build();
|
||||
|
||||
private final DependencyComparator comparator = new DependencyComparator();
|
||||
|
||||
@ -63,7 +64,8 @@ class DependencyComparatorTests {
|
||||
@Test
|
||||
void compareLibSameGroupId() {
|
||||
assertThat(this.comparator.compare(LIB_BETA,
|
||||
new Dependency("com.example.beta", "a"))).isPositive();
|
||||
Dependency.withCoordinates("com.example.beta", "a").build()))
|
||||
.isPositive();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -48,8 +48,10 @@ class DependencyContainerTests {
|
||||
@Test
|
||||
void addDependencyWithVersion() {
|
||||
DependencyContainer container = createTestContainer();
|
||||
container.add("custom", "com.example", "acme", VersionReference.ofValue("1.0.0"),
|
||||
DependencyScope.COMPILE);
|
||||
container.add("custom",
|
||||
Dependency.withCoordinates("com.example", "acme")
|
||||
.version(VersionReference.ofValue("1.0.0"))
|
||||
.scope(DependencyScope.COMPILE));
|
||||
assertThat(container.ids()).containsOnly("custom");
|
||||
assertThat(container.items()).hasSize(1);
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.test.io.TextTestUtils;
|
||||
@ -60,8 +61,10 @@ class Gradle3BuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithRuntimeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("driver", "com.example", "jdbc-driver",
|
||||
VersionReference.ofValue("1.0.0"), DependencyScope.RUNTIME);
|
||||
build.dependencies().add("driver",
|
||||
Dependency.withCoordinates("com.example", "jdbc-driver")
|
||||
.version(VersionReference.ofValue("1.0.0"))
|
||||
.scope(DependencyScope.RUNTIME));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" runtime 'com.example:jdbc-driver:1.0.0'", "}");
|
||||
@ -113,8 +116,9 @@ class Gradle3BuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithNonNullArtifactTypeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("root", "org.springframework.boot",
|
||||
"spring-boot-starter", null, DependencyScope.COMPILE, "tar.gz");
|
||||
build.dependencies().add("root", Dependency
|
||||
.withCoordinates("org.springframework.boot", "spring-boot-starter")
|
||||
.scope(DependencyScope.COMPILE).type("tar.gz"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" compile 'org.springframework.boot:spring-boot-starter@tar.gz'", "}");
|
||||
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
@ -246,9 +247,10 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithVersionedDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("kotlin-stdlib", "org.jetbrains.kotlin",
|
||||
"kotlin-stdlib-jdk8", VersionReference.ofProperty("kotlin.version"),
|
||||
DependencyScope.COMPILE);
|
||||
build.dependencies().add("kotlin-stdlib",
|
||||
Dependency.withCoordinates("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
|
||||
.version(VersionReference.ofProperty("kotlin.version"))
|
||||
.scope(DependencyScope.COMPILE));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation \"org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}\"",
|
||||
@ -258,9 +260,11 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithExternalVersionedDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("acme", "com.example", "acme",
|
||||
VersionReference.ofProperty(VersionProperty.of("acme.version", false)),
|
||||
DependencyScope.COMPILE);
|
||||
build.dependencies().add("acme",
|
||||
Dependency.withCoordinates("com.example", "acme")
|
||||
.version(VersionReference
|
||||
.ofProperty(VersionProperty.of("acme.version", false)))
|
||||
.scope(DependencyScope.COMPILE));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation \"com.example:acme:${property('acme.version')}\"",
|
||||
@ -339,8 +343,10 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithRuntimeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("driver", "com.example", "jdbc-driver",
|
||||
VersionReference.ofValue("1.0.0"), DependencyScope.RUNTIME);
|
||||
build.dependencies().add("driver",
|
||||
Dependency.withCoordinates("com.example", "jdbc-driver")
|
||||
.version(VersionReference.ofValue("1.0.0"))
|
||||
.scope(DependencyScope.RUNTIME));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" runtimeOnly 'com.example:jdbc-driver:1.0.0'", "}");
|
||||
@ -393,10 +399,11 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithExclusions() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("test", "com.example", "test", null,
|
||||
DependencyScope.COMPILE, null,
|
||||
new Exclusion("com.example.legacy", "legacy-one"),
|
||||
new Exclusion("com.example.another", "legacy-two"));
|
||||
build.dependencies().add("test",
|
||||
Dependency.withCoordinates("com.example", "test")
|
||||
.scope(DependencyScope.COMPILE)
|
||||
.exclusions(new Exclusion("com.example.legacy", "legacy-one"),
|
||||
new Exclusion("com.example.another", "legacy-two")));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation('com.example:test') {",
|
||||
@ -408,8 +415,9 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithNonNullArtifactTypeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("root", "org.springframework.boot",
|
||||
"spring-boot-starter", null, DependencyScope.COMPILE, "tar.gz");
|
||||
build.dependencies().add("root", Dependency
|
||||
.withCoordinates("org.springframework.boot", "spring-boot-starter")
|
||||
.scope(DependencyScope.COMPILE).type("tar.gz"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation 'org.springframework.boot:spring-boot-starter@tar.gz'",
|
||||
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
@ -247,9 +248,10 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithVersionedDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("kotlin-stdlib", "org.jetbrains.kotlin",
|
||||
"kotlin-stdlib-jdk8", VersionReference.ofProperty("kotlin.version"),
|
||||
DependencyScope.COMPILE);
|
||||
build.dependencies().add("kotlin-stdlib",
|
||||
Dependency.withCoordinates("org.jetbrains.kotlin", "kotlin-stdlib-jdk8")
|
||||
.version(VersionReference.ofProperty("kotlin.version"))
|
||||
.scope(DependencyScope.COMPILE));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"org.jetbrains.kotlin:kotlin-stdlib-jdk8:${property(\"kotlinVersion\")}\")",
|
||||
@ -259,9 +261,11 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithExternalVersionedDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("acme", "com.example", "acme",
|
||||
VersionReference.ofProperty(VersionProperty.of("acme.version", false)),
|
||||
DependencyScope.COMPILE);
|
||||
build.dependencies().add("acme",
|
||||
Dependency.withCoordinates("com.example", "acme")
|
||||
.version(VersionReference
|
||||
.ofProperty(VersionProperty.of("acme.version", false)))
|
||||
.scope(DependencyScope.COMPILE));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"com.example:acme:${property(\"acme.version\")}\")",
|
||||
@ -343,8 +347,10 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithRuntimeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("driver", "com.example", "jdbc-driver",
|
||||
VersionReference.ofValue("1.0.0"), DependencyScope.RUNTIME);
|
||||
build.dependencies().add("driver",
|
||||
Dependency.withCoordinates("com.example", "jdbc-driver")
|
||||
.version(VersionReference.ofValue("1.0.0"))
|
||||
.scope(DependencyScope.RUNTIME));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" runtimeOnly(\"com.example:jdbc-driver:1.0.0\")", "}");
|
||||
@ -397,10 +403,11 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithExclusions() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("test", "com.example", "test", null,
|
||||
DependencyScope.COMPILE, null,
|
||||
new Exclusion("com.example.legacy", "legacy-one"),
|
||||
new Exclusion("com.example.another", "legacy-two"));
|
||||
build.dependencies().add("test",
|
||||
Dependency.withCoordinates("com.example", "test")
|
||||
.scope(DependencyScope.COMPILE)
|
||||
.exclusions(new Exclusion("com.example.legacy", "legacy-one"),
|
||||
new Exclusion("com.example.another", "legacy-two")));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"com.example:test\") {",
|
||||
@ -412,8 +419,9 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithNonNullArtifactTypeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("root", "org.springframework.boot",
|
||||
"spring-boot-starter", null, DependencyScope.COMPILE, "tar.gz");
|
||||
build.dependencies().add("root", Dependency
|
||||
.withCoordinates("org.springframework.boot", "spring-boot-starter")
|
||||
.scope(DependencyScope.COMPILE).type("tar.gz"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"org.springframework.boot:spring-boot-starter@tar.gz\")",
|
||||
|
@ -19,6 +19,7 @@ package io.spring.initializr.generator.buildsystem.maven;
|
||||
import java.io.StringWriter;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
@ -261,10 +262,11 @@ class MavenBuildWriterTests {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.setGroup("com.example.demo");
|
||||
build.setArtifact("demo");
|
||||
build.dependencies().add("test", "com.example", "test", null,
|
||||
DependencyScope.COMPILE, null,
|
||||
new Exclusion("com.example.legacy", "legacy-one"),
|
||||
new Exclusion("com.example.another", "legacy-two"));
|
||||
build.dependencies().add("test",
|
||||
Dependency.withCoordinates("com.example", "test")
|
||||
.scope(DependencyScope.COMPILE)
|
||||
.exclusions(new Exclusion("com.example.legacy", "legacy-one"),
|
||||
new Exclusion("com.example.another", "legacy-two")));
|
||||
generatePom(build, (pom) -> {
|
||||
NodeAssert dependency = pom.nodeAtPath("/project/dependencies/dependency");
|
||||
assertThat(dependency).textAtPath("groupId").isEqualTo("com.example");
|
||||
@ -290,8 +292,9 @@ class MavenBuildWriterTests {
|
||||
MavenBuild build = new MavenBuild();
|
||||
build.setGroup("com.example.demo");
|
||||
build.setArtifact("demo");
|
||||
build.dependencies().add("root", "org.springframework.boot",
|
||||
"spring-boot-starter", null, DependencyScope.COMPILE, "tar.gz");
|
||||
build.dependencies().add("root", Dependency
|
||||
.withCoordinates("org.springframework.boot", "spring-boot-starter")
|
||||
.scope(DependencyScope.COMPILE).type("tar.gz"));
|
||||
generatePom(build, (pom) -> {
|
||||
NodeAssert dependency = pom.nodeAtPath("/project/dependencies/dependency");
|
||||
assertThat(dependency).textAtPath("type").isEqualTo("tar.gz");
|
||||
|
@ -45,9 +45,10 @@ public final class MetadataBuildItemMapper {
|
||||
}
|
||||
VersionReference versionReference = (dependency.getVersion() != null)
|
||||
? VersionReference.ofValue(dependency.getVersion()) : null;
|
||||
return new io.spring.initializr.generator.buildsystem.Dependency(
|
||||
dependency.getGroupId(), dependency.getArtifactId(), versionReference,
|
||||
toDependencyScope(dependency.getScope()), dependency.getType());
|
||||
return io.spring.initializr.generator.buildsystem.Dependency
|
||||
.withCoordinates(dependency.getGroupId(), dependency.getArtifactId())
|
||||
.version(versionReference).scope(toDependencyScope(dependency.getScope()))
|
||||
.type(dependency.getType()).build();
|
||||
}
|
||||
|
||||
private static DependencyScope toDependencyScope(String scope) {
|
||||
|
Loading…
Reference in New Issue
Block a user