Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for writing credentials in Gradle's build file #1077

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ sourceCompatibility = '1.8'

repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven {
url 'https://repo.spring.io/milestone'
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* A Maven repository.
*
* @author Andy Wilkinson
* @author Jafer Khan Shamshad
*/
public class MavenRepository {

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Jean-Baptiste Nizet
* @author Jafer Khan Shamshad
* @see GroovyDslGradleBuildWriter
* @see KotlinDslGradleBuildWriter
*/
Expand Down Expand Up @@ -96,11 +97,15 @@ protected List<StandardGradlePlugin> 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<MavenRepository> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
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;

/**
* {@link GradleBuild} settings writer abstraction.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
* @author Jafer Khan Shamshad
* @see GroovyDslGradleSettingsWriter
* @see KotlinDslGradleSettingsWriter
*/
Expand Down Expand Up @@ -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("}");
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -109,11 +111,27 @@ private String configurationReference(String configurationName, Collection<Strin
}

@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 = uri(\"" + repository.getUrl() + "\") }";

writer.println("maven {");
writer.indented(() -> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}

}
Loading