From 9a35fd11a476f930413dd1f1e0a905508c62859a Mon Sep 17 00:00:00 2001 From: Jafer Khan Date: Sun, 29 Mar 2020 22:00:58 +0500 Subject: [PATCH] Add support for writing credentials in Gradle's build file. Fixes gh-997 --- .../gradle/repositories-build.gradle.gen | 8 ++- .../gradle/repositories-build.gradle.kts.gen | 8 ++- .../repositories-milestone-build.gradle.gen | 4 +- ...epositories-milestone-build.gradle.kts.gen | 4 +- .../buildsystem/MavenRepository.java | 24 +++++++++ .../MavenRepositoryCredentials.java | 51 +++++++++++++++++++ .../buildsystem/gradle/GradleBuildWriter.java | 11 ++-- .../gradle/GradleSettingsWriter.java | 28 ++++++++-- .../gradle/GroovyDslGradleBuildWriter.java | 24 +++++++-- .../gradle/GroovyDslGradleSettingsWriter.java | 5 +- .../gradle/KotlinDslGradleBuildWriter.java | 24 +++++++-- .../gradle/KotlinDslGradleSettingsWriter.java | 9 +++- .../GroovyDslGradleBuildWriterTests.java | 21 ++++++-- .../GroovyDslGradleSettingsWriterTests.java | 30 ++++++++--- .../KotlinDslGradleBuildWriterTests.java | 21 ++++++-- .../KotlinDslGradleSettingsWriterTests.java | 29 ++++++++--- 16 files changed, 257 insertions(+), 44 deletions(-) create mode 100644 initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryCredentials.java diff --git a/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.gen b/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.gen index dc722db5a1..cc79d4b635 100644 --- a/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.gen +++ b/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.gen @@ -10,8 +10,12 @@ sourceCompatibility = '1.8' repositories { mavenCentral() - maven { url 'https://example.com/foo' } - maven { url 'https://example.com/bar' } + maven { + url 'https://example.com/foo' + } + maven { + url 'https://example.com/bar' + } } dependencies { diff --git a/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.kts.gen b/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.kts.gen index 9ab60d6225..c802be9c1c 100644 --- a/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.kts.gen +++ b/initializr-generator-spring/src/test/resources/project/gradle/repositories-build.gradle.kts.gen @@ -10,8 +10,12 @@ java.sourceCompatibility = JavaVersion.VERSION_1_8 repositories { mavenCentral() - maven { url = uri("https://example.com/foo") } - maven { url = uri("https://example.com/bar") } + maven { + url = uri("https://example.com/foo") + } + maven { + url = uri("https://example.com/bar") + } } dependencies { diff --git a/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.gen b/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.gen index 23edb0fa5c..8aa590bb89 100644 --- a/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.gen +++ b/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.gen @@ -10,7 +10,9 @@ sourceCompatibility = '1.8' repositories { mavenCentral() - maven { url 'https://repo.spring.io/milestone' } + maven { + url 'https://repo.spring.io/milestone' + } } dependencies { diff --git a/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.kts.gen b/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.kts.gen index 45144a3d39..ca25126942 100644 --- a/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.kts.gen +++ b/initializr-generator-spring/src/test/resources/project/gradle/repositories-milestone-build.gradle.kts.gen @@ -10,7 +10,9 @@ java.sourceCompatibility = JavaVersion.VERSION_1_8 repositories { mavenCentral() - maven { url = uri("https://repo.spring.io/milestone") } + maven { + url = uri("https://repo.spring.io/milestone") + } } dependencies { diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java index a32bcf2a9f..8efae13e12 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepository.java @@ -20,6 +20,7 @@ * A Maven repository. * * @author Andy Wilkinson + * @author Jafer Khan Shamshad */ public class MavenRepository { @@ -37,11 +38,14 @@ public class MavenRepository { private final boolean snapshotsEnabled; + private final MavenRepositoryCredentials credentials; + protected MavenRepository(Builder builder) { this.id = builder.id; this.name = builder.name; this.url = builder.url; this.snapshotsEnabled = builder.snapshotsEnabled; + this.credentials = builder.credentials; } /** @@ -87,6 +91,14 @@ public boolean isSnapshotsEnabled() { return this.snapshotsEnabled; } + /** + * Return the credentials required for accessing the repository. + * @return the repository credentials + */ + public MavenRepositoryCredentials getCredentials() { + return this.credentials; + } + public static class Builder { private String id; @@ -97,6 +109,8 @@ public static class Builder { private boolean snapshotsEnabled; + private MavenRepositoryCredentials credentials; + public Builder(String id, String url) { this.id = id; this.name = id; @@ -143,6 +157,16 @@ public Builder snapshotsEnabled(boolean snapshotsEnabled) { return this; } + /** + * Set the credentials required for accessing the repository. + * @param credentials the credentials + * @return this for method chaining + */ + public Builder credentials(MavenRepositoryCredentials credentials) { + this.credentials = credentials; + return this; + } + /** * Build a {@link MavenRepository} with the current state of this builder. * @return a {@link MavenRepository} diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryCredentials.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryCredentials.java new file mode 100644 index 0000000000..ff0c9be081 --- /dev/null +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/MavenRepositoryCredentials.java @@ -0,0 +1,51 @@ +/* + * 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. + * 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; + +/** + * Represents credentials for a {@link MavenRepository maven repository}. + * + * @author Jafer Khan Shamshad + */ +public class MavenRepositoryCredentials { + + private final String username; + + private final String password; + + public MavenRepositoryCredentials(String username, String password) { + this.username = username; + this.password = password; + } + + /** + * Return the username required to access the repository. + * @return the username + */ + public String getUsername() { + return this.username; + } + + /** + * Return the password required to access the repository. + * @return the password + */ + public String getPassword() { + return this.password; + } + +} diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java index c69e72d967..3b22750888 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriter.java @@ -46,6 +46,7 @@ * @author Andy Wilkinson * @author Stephane Nicoll * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad * @see GroovyDslGradleBuildWriter * @see KotlinDslGradleBuildWriter */ @@ -96,11 +97,15 @@ protected List extractStandardPlugin(GradleBuild build) { protected abstract void writeConfigurations(IndentingWriter writer, GradleConfigurationContainer configurations); protected final void writeRepositories(IndentingWriter writer, GradleBuild build) { - writeNestedCollection(writer, "repositories", build.repositories().items().collect(Collectors.toList()), - this::repositoryAsString); + List repositories = build.repositories().items().collect(Collectors.toList()); + if (!repositories.isEmpty()) { + writer.println("repositories {"); + writer.indented(() -> repositories.forEach((repository) -> writeRepository(writer, repository))); + writer.println("}"); + } } - protected abstract String repositoryAsString(MavenRepository repository); + protected abstract void writeRepository(IndentingWriter writer, MavenRepository repository); private void writeProperties(IndentingWriter writer, PropertyContainer properties) { if (properties.isEmpty()) { diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleSettingsWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleSettingsWriter.java index c6167965fd..64a402b035 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleSettingsWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleSettingsWriter.java @@ -17,6 +17,7 @@ package io.spring.initializr.generator.buildsystem.gradle; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryCredentials; import io.spring.initializr.generator.io.IndentingWriter; /** @@ -24,6 +25,7 @@ * * @author Andy Wilkinson * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad * @see GroovyDslGradleSettingsWriter * @see KotlinDslGradleSettingsWriter */ @@ -55,7 +57,7 @@ private void writePluginManagement(IndentingWriter writer, GradleBuild build) { private void writeRepositories(IndentingWriter writer, GradleBuild build) { writer.println("repositories {"); writer.indented(() -> { - build.pluginRepositories().items().map(this::repositoryAsString).forEach(writer::println); + build.pluginRepositories().items().forEach((repository) -> writeRepository(writer, repository)); writer.println("gradlePluginPortal()"); }); writer.println("}"); @@ -79,15 +81,31 @@ private void writeResolutionStrategyIfNecessary(IndentingWriter writer, GradleBu writer.println("}"); } - private String repositoryAsString(MavenRepository repository) { + private void writeRepository(IndentingWriter writer, MavenRepository repository) { if (MavenRepository.MAVEN_CENTRAL.equals(repository)) { - return "mavenCentral()"; + writer.println("mavenCentral()"); + return; } - return "maven { " + urlAssignment(repository.getUrl()) + " }"; + + writer.println("maven {"); + writer.indented(() -> { + writer.println(propertyAssignment("url", repository.getUrl())); + + MavenRepositoryCredentials credentials = repository.getCredentials(); + if (credentials != null) { + writer.println("credentials {"); + writer.indented(() -> { + writer.println(propertyAssignment("username", credentials.getUsername())); + writer.println(propertyAssignment("password", credentials.getPassword())); + }); + writer.println("}"); + } + }); + writer.println("}"); } protected abstract String wrapWithQuotes(String value); - protected abstract String urlAssignment(String url); + protected abstract String propertyAssignment(String name, String value); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java index 780dbf4ba7..49a2c5f186 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriter.java @@ -25,6 +25,7 @@ import io.spring.initializr.generator.buildsystem.Dependency; import io.spring.initializr.generator.buildsystem.Dependency.Exclusion; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryCredentials; import io.spring.initializr.generator.io.IndentingWriter; import io.spring.initializr.generator.version.VersionProperty; import io.spring.initializr.generator.version.VersionReference; @@ -33,6 +34,7 @@ * A {@link GradleBuild} writer for {@code build.gradle}. * * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad */ public class GroovyDslGradleBuildWriter extends GradleBuildWriter { @@ -92,11 +94,27 @@ protected void writeJavaSourceCompatibility(IndentingWriter writer, GradleBuildS } @Override - protected String repositoryAsString(MavenRepository repository) { + protected void writeRepository(IndentingWriter writer, MavenRepository repository) { if (MavenRepository.MAVEN_CENTRAL.equals(repository)) { - return "mavenCentral()"; + writer.println("mavenCentral()"); + return; } - return "maven { url '" + repository.getUrl() + "' }"; + + writer.println("maven {"); + writer.indented(() -> { + writer.println("url '" + repository.getUrl() + "'"); + + MavenRepositoryCredentials credentials = repository.getCredentials(); + if (credentials != null) { + writer.println("credentials {"); + writer.indented(() -> { + writer.println("username '" + credentials.getUsername() + "'"); + writer.println("password '" + credentials.getPassword() + "'"); + }); + writer.println("}"); + } + }); + writer.println("}"); } @Override diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriter.java index efc9e7fefc..724e9d206e 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriter.java @@ -20,6 +20,7 @@ * A {@link GradleBuild} writer for {@code settings.gradle}. * * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad */ public class GroovyDslGradleSettingsWriter extends GradleSettingsWriter { @@ -29,8 +30,8 @@ protected String wrapWithQuotes(String value) { } @Override - protected String urlAssignment(String url) { - return "url " + wrapWithQuotes(url); + protected String propertyAssignment(String name, String value) { + return name + " " + wrapWithQuotes(value); } } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java index a308332206..ebfb80b9a6 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriter.java @@ -27,6 +27,7 @@ import io.spring.initializr.generator.buildsystem.Dependency; import io.spring.initializr.generator.buildsystem.Dependency.Exclusion; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryCredentials; import io.spring.initializr.generator.io.IndentingWriter; import io.spring.initializr.generator.version.VersionProperty; import io.spring.initializr.generator.version.VersionReference; @@ -35,6 +36,7 @@ * A {@link GradleBuild} writer for {@code build.gradle.kts}. * * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad */ public class KotlinDslGradleBuildWriter extends GradleBuildWriter { @@ -109,11 +111,27 @@ private String configurationReference(String configurationName, Collection { + writer.println("url = uri(\"" + repository.getUrl() + "\")"); + + MavenRepositoryCredentials credentials = repository.getCredentials(); + if (credentials != null) { + writer.println("credentials {"); + writer.indented(() -> { + writer.println("username = \"" + credentials.getUsername() + "\""); + writer.println("password = \"" + credentials.getPassword() + "\""); + }); + writer.println("}"); + } + }); + writer.println("}"); } @Override diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriter.java index a62ecb26dd..62c2d2988b 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriter.java @@ -20,6 +20,7 @@ * A {@link GradleBuild} writer for {@code settings.gradle.kts}. * * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad */ public class KotlinDslGradleSettingsWriter extends GradleSettingsWriter { @@ -29,8 +30,12 @@ protected String wrapWithQuotes(String value) { } @Override - protected String urlAssignment(String url) { - return "url = uri(\"" + url + "\")"; + protected String propertyAssignment(String name, String value) { + if (name.equalsIgnoreCase("url")) { + return name + " = uri(" + wrapWithQuotes(value) + ")"; + } + + return name + " = " + wrapWithQuotes(value); } } diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java index ef790feef8..d154953a26 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleBuildWriterTests.java @@ -25,6 +25,7 @@ import io.spring.initializr.generator.buildsystem.Dependency.Exclusion; import io.spring.initializr.generator.buildsystem.DependencyScope; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryCredentials; import io.spring.initializr.generator.io.IndentingWriter; import io.spring.initializr.generator.version.VersionProperty; import io.spring.initializr.generator.version.VersionReference; @@ -38,6 +39,7 @@ * @author Andy Wilkinson * @author Jean-Baptiste Nizet * @author Stephane Nicoll + * @author Jafer Khan Shamshad */ class GroovyDslGradleBuildWriterTests { @@ -116,8 +118,19 @@ void gradleBuildWithRepository() { GradleBuild build = new GradleBuild(); build.repositories().add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone")); List lines = generateBuild(build); - assertThat(lines).containsSequence("repositories {", " maven { url 'https://repo.spring.io/milestone' }", - "}"); + assertThat(lines).containsSequence("repositories {", " maven {", + " url 'https://repo.spring.io/milestone'", " }", "}"); + } + + @Test + void gradleBuildWithRepositoryCredentials() { + GradleBuild build = new GradleBuild(); + build.repositories().add(MavenRepository.withIdAndUrl("foo-repository", "https://example.com/foo") + .credentials(new MavenRepositoryCredentials("foo", "bar"))); + List lines = generateBuild(build); + assertThat(lines).containsSequence("repositories {", " maven {", " url 'https://example.com/foo'", + " credentials {", " username 'foo'", " password 'bar'", " }", + " }", "}"); } @Test @@ -126,8 +139,8 @@ void gradleBuildWithSnapshotRepository() { build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot") .snapshotsEnabled(true)); List lines = generateBuild(build); - assertThat(lines).containsSequence("repositories {", " maven { url 'https://repo.spring.io/snapshot' }", - "}"); + assertThat(lines).containsSequence("repositories {", " maven {", + " url 'https://repo.spring.io/snapshot'", " }", "}"); } @Test diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriterTests.java index a01dd0dece..f48ca450cc 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/GroovyDslGradleSettingsWriterTests.java @@ -21,6 +21,7 @@ import java.util.List; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryCredentials; import io.spring.initializr.generator.io.IndentingWriter; import org.junit.jupiter.api.Test; @@ -31,6 +32,7 @@ * * @author Andy Wilkinson * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad */ class GroovyDslGradleSettingsWriterTests { @@ -56,14 +58,30 @@ void gradleBuildWithPluginRepository() { build.pluginRepositories().add(MavenRepository .withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones")); List lines = generateSettings(build); - assertThat(lines).containsSequence("pluginManagement {", " repositories {", - " maven { url 'https://repo.spring.io/milestone' }", " gradlePluginPortal()", " }", - " resolutionStrategy {", " eachPlugin {", + assertThat(lines).containsSequence("pluginManagement {", " repositories {", " maven {", + " url 'https://repo.spring.io/milestone'", " }", " gradlePluginPortal()", + " }", " resolutionStrategy {", " eachPlugin {", " if (requested.id.id == 'org.springframework.boot') {", " useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")", " }", " }", " }", "}"); } + @Test + void gradleBuildWithPluginRepositoryCredentials() { + GradleBuild build = new GradleBuild(); + build.pluginRepositories().add(MavenRepository.withIdAndUrl("foo-repository", "https://example.com/foo") + .credentials(new MavenRepositoryCredentials("foo", "bar"))); + List lines = generateSettings(build); + assertThat(lines).containsSequence("pluginManagement {", " repositories {", " maven {", + " url 'https://example.com/foo'", " credentials {", + " username 'foo'", " password 'bar'", " }", " }", + " gradlePluginPortal()", " }", " resolutionStrategy {", " eachPlugin {", + " if (requested.id.id == 'org.springframework.boot') {", + " useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")", + " }", " }", " }", "}"); + + } + @Test void gradleBuildWithSnapshotPluginRepository() { GradleBuild build = new GradleBuild(); @@ -71,9 +89,9 @@ void gradleBuildWithSnapshotPluginRepository() { .add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot") .name("Spring Snapshots").snapshotsEnabled(true)); List lines = generateSettings(build); - assertThat(lines).containsSequence("pluginManagement {", " repositories {", - " maven { url 'https://repo.spring.io/snapshot' }", " gradlePluginPortal()", " }", - " resolutionStrategy {", " eachPlugin {", + assertThat(lines).containsSequence("pluginManagement {", " repositories {", " maven {", + " url 'https://repo.spring.io/snapshot'", " }", " gradlePluginPortal()", + " }", " resolutionStrategy {", " eachPlugin {", " if (requested.id.id == 'org.springframework.boot') {", " useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")", " }", " }", " }", "}"); diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java index 7be12aa012..eb8b1f39f5 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleBuildWriterTests.java @@ -25,6 +25,7 @@ import io.spring.initializr.generator.buildsystem.Dependency.Exclusion; import io.spring.initializr.generator.buildsystem.DependencyScope; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryCredentials; import io.spring.initializr.generator.io.IndentingWriter; import io.spring.initializr.generator.version.VersionProperty; import io.spring.initializr.generator.version.VersionReference; @@ -37,6 +38,7 @@ * Tests for {@link KotlinDslGradleBuildWriter} * * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad */ class KotlinDslGradleBuildWriterTests { @@ -127,8 +129,19 @@ void gradleBuildWithRepository() { GradleBuild build = new GradleBuild(); build.repositories().add(MavenRepository.withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone")); List lines = generateBuild(build); - assertThat(lines).containsSequence("repositories {", - " maven { url = uri(\"https://repo.spring.io/milestone\") }", "}"); + assertThat(lines).containsSequence("repositories {", " maven {", + " url = uri(\"https://repo.spring.io/milestone\")", " }", "}"); + } + + @Test + void gradleBuildWithRepositoryCredentials() { + GradleBuild build = new GradleBuild(); + build.repositories().add(MavenRepository.withIdAndUrl("foo-repository", "https://example.com/foo") + .credentials(new MavenRepositoryCredentials("foo", "bar"))); + List lines = generateBuild(build); + assertThat(lines).containsSequence("repositories {", " maven {", + " url = uri(\"https://example.com/foo\")", " credentials {", + " username = \"foo\"", " password = \"bar\"", " }", " }", "}"); } @Test @@ -137,8 +150,8 @@ void gradleBuildWithSnapshotRepository() { build.repositories().add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot") .snapshotsEnabled(true)); List lines = generateBuild(build); - assertThat(lines).containsSequence("repositories {", - " maven { url = uri(\"https://repo.spring.io/snapshot\") }", "}"); + assertThat(lines).containsSequence("repositories {", " maven {", + " url = uri(\"https://repo.spring.io/snapshot\")", " }", "}"); } @Test diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriterTests.java index a891dc6f59..a6b17e9d36 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/gradle/KotlinDslGradleSettingsWriterTests.java @@ -21,6 +21,7 @@ import java.util.List; import io.spring.initializr.generator.buildsystem.MavenRepository; +import io.spring.initializr.generator.buildsystem.MavenRepositoryCredentials; import io.spring.initializr.generator.io.IndentingWriter; import org.junit.jupiter.api.Test; @@ -30,6 +31,7 @@ * Tests for {@link KotlinDslGradleSettingsWriter}. * * @author Jean-Baptiste Nizet + * @author Jafer Khan Shamshad */ class KotlinDslGradleSettingsWriterTests { @@ -55,14 +57,29 @@ void gradleBuildWithPluginRepository() { build.pluginRepositories().add(MavenRepository .withIdAndUrl("spring-milestones", "https://repo.spring.io/milestone").name("Spring Milestones")); List lines = generateSettings(build); - assertThat(lines).containsSequence("pluginManagement {", " repositories {", - " maven { url = uri(\"https://repo.spring.io/milestone\") }", " gradlePluginPortal()", - " }", " resolutionStrategy {", " eachPlugin {", + assertThat(lines).containsSequence("pluginManagement {", " repositories {", " maven {", + " url = uri(\"https://repo.spring.io/milestone\")", " }", + " gradlePluginPortal()", " }", " resolutionStrategy {", " eachPlugin {", " if (requested.id.id == \"org.springframework.boot\") {", " useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")", " }", " }", " }", "}"); } + @Test + void gradleBuildWithPluginRepositoryCredentials() { + GradleBuild build = new GradleBuild(); + build.pluginRepositories().add(MavenRepository.withIdAndUrl("foo-repository", "https://example.com/foo") + .name("foo-repo").credentials(new MavenRepositoryCredentials("foo", "bar"))); + List lines = generateSettings(build); + assertThat(lines).containsSequence("pluginManagement {", " repositories {", " maven {", + " url = uri(\"https://example.com/foo\")", " credentials {", + " username = \"foo\"", " password = \"bar\"", " }", + " }", " gradlePluginPortal()", " }", " resolutionStrategy {", + " eachPlugin {", " if (requested.id.id == \"org.springframework.boot\") {", + " useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")", + " }", " }", " }", "}"); + } + @Test void gradleBuildWithSnapshotPluginRepository() { GradleBuild build = new GradleBuild(); @@ -70,9 +87,9 @@ void gradleBuildWithSnapshotPluginRepository() { .add(MavenRepository.withIdAndUrl("spring-snapshots", "https://repo.spring.io/snapshot") .name("Spring Snapshots").snapshotsEnabled(true)); List lines = generateSettings(build); - assertThat(lines).containsSequence("pluginManagement {", " repositories {", - " maven { url = uri(\"https://repo.spring.io/snapshot\") }", " gradlePluginPortal()", - " }", " resolutionStrategy {", " eachPlugin {", + assertThat(lines).containsSequence("pluginManagement {", " repositories {", " maven {", + " url = uri(\"https://repo.spring.io/snapshot\")", " }", + " gradlePluginPortal()", " }", " resolutionStrategy {", " eachPlugin {", " if (requested.id.id == \"org.springframework.boot\") {", " useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")", " }", " }", " }", "}");