Skip to content

Commit

Permalink
Allow overriding the default Gradle library repository through a new …
Browse files Browse the repository at this point in the history
…property called `org.gradle.internal.gradle.libs.repo.override` (previously overriding was only possible through the `GRADLE_LIBS_REPO_OVERRIDE` environment variable).

The property can be more useful because it can be defined in the build itself (either through gradle.properties or programmatically)

Signed-off-by: akiraly <kiralyattila.hu@gmail.com>
  • Loading branch information
akiraly committed Nov 14, 2023
1 parent 3516c9b commit 684da6a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public void setOutputFile(File newOutputFile) {
public Set<Dependency> resolveDependencies() {
ProjectInternal projectInternal = (ProjectInternal) project;
IdeArtifactRegistry ideArtifactRegistry = projectInternal.getServices().get(IdeArtifactRegistry.class);
IdeaDependenciesProvider ideaDependenciesProvider = new IdeaDependenciesProvider(projectInternal, ideArtifactRegistry, new DefaultGradleApiSourcesResolver(projectInternal.newDetachedResolver()));
IdeaDependenciesProvider ideaDependenciesProvider = new IdeaDependenciesProvider(projectInternal, ideArtifactRegistry, new DefaultGradleApiSourcesResolver(projectInternal));
return ideaDependenciesProvider.provide(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ dependencies {
@Requires(IntegTestPreconditions.IsDaemonExecutor)
def "does not download gradleApi() sources when sources download is disabled"() {
given:
executer.withEnvironmentVars('GRADLE_REPO_OVERRIDE': "$server.uri/")
executer.withEnvironmentVars('GRADLE_LIBS_REPO_OVERRIDE': "$server.uri/")
buildScript """
apply plugin: "java"
Expand All @@ -336,7 +336,7 @@ dependencies {
@Requires(IntegTestPreconditions.IsDaemonExecutor)
def "does not download gradleApi() sources when offline"() {
given:
executer.withEnvironmentVars('GRADLE_REPO_OVERRIDE': "$server.uri/")
executer.withEnvironmentVars('GRADLE_LIBS_REPO_OVERRIDE': "$server.uri/")
buildScript """
apply plugin: "java"
Expand Down Expand Up @@ -392,6 +392,42 @@ dependencies {
ideFileContainsEntry("groovy-xml-${groovyVersion}.jar", ["groovy-xml-${groovyVersion}-sources.jar"], [])
}
@ToBeFixedForConfigurationCache
@Requires(UnitTestPreconditions.StableGroovy) // localGroovy() version cannot be swapped-out when a snapshot Groovy build is used
def "sources for localGroovy() are downloaded and attached when using property based url override"() {
given:
def repo = givenGroovyExistsInGradleRepo()
propertiesFile << "org.gradle.internal.gradle.libs.repo.override=$repo.uri/"
buildScript """
apply plugin: "java"
apply plugin: "idea"
apply plugin: "eclipse"

dependencies {
implementation localGroovy()
}
"""
when:
succeeds ideTask
then:
ideFileContainsEntry("groovy-${groovyVersion}.jar", ["groovy-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-ant-${groovyVersion}.jar", ["groovy-ant-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-astbuilder-${groovyVersion}.jar", ["groovy-astbuilder-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-console-${groovyVersion}.jar", ["groovy-console-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-datetime-${groovyVersion}.jar", ["groovy-datetime-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-dateutil-${groovyVersion}.jar", ["groovy-dateutil-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-groovydoc-${groovyVersion}.jar", ["groovy-groovydoc-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-json-${groovyVersion}.jar", ["groovy-json-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-nio-${groovyVersion}.jar", ["groovy-nio-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-sql-${groovyVersion}.jar", ["groovy-sql-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-templates-${groovyVersion}.jar", ["groovy-templates-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-test-${groovyVersion}.jar", ["groovy-test-${groovyVersion}-sources.jar"], [])
ideFileContainsEntry("groovy-xml-${groovyVersion}.jar", ["groovy-xml-${groovyVersion}-sources.jar"], [])
}
@ToBeFixedForConfigurationCache
@Requires(UnitTestPreconditions.StableGroovy) // localGroovy() version cannot be swapped-out when a snapshot Groovy build is used
def "sources for localGroovy() are downloaded and attached when using gradleApi()"() {
Expand All @@ -416,6 +452,30 @@ dependencies {
ideFileContainsEntry("groovy-${groovyVersion}.jar", ["groovy-${groovyVersion}-sources.jar"], [])
}
@ToBeFixedForConfigurationCache
@Requires(UnitTestPreconditions.StableGroovy) // localGroovy() version cannot be swapped-out when a snapshot Groovy build is used
def "sources for localGroovy() are downloaded and attached when using gradleApi() and property based url override"() {
given:
def repo = givenGroovyExistsInGradleRepo()
propertiesFile << "org.gradle.internal.gradle.libs.repo.override=$repo.uri/"
buildScript """
apply plugin: "java"
apply plugin: "idea"
apply plugin: "eclipse"

dependencies {
implementation gradleApi()
}
"""
when:
succeeds ideTask
then:
ideFileContainsEntry("groovy-${groovyVersion}.jar", ["groovy-${groovyVersion}-sources.jar"], [])
}
@ToBeFixedForConfigurationCache
@Requires(
value = [UnitTestPreconditions.StableGroovy, IntegTestPreconditions.NotEmbeddedExecutor],
Expand Down Expand Up @@ -443,6 +503,33 @@ dependencies {
ideFileContainsEntry("groovy-${groovyVersion}.jar", ["groovy-${groovyVersion}-sources.jar"], [])
}
@ToBeFixedForConfigurationCache
@Requires(
value = [UnitTestPreconditions.StableGroovy, IntegTestPreconditions.NotEmbeddedExecutor],
reason = "localGroovy() version cannot be swapped-out when a snapshot Groovy build is used"
)
def "sources for localGroovy() are downloaded and attached when using gradleTestKit() and property based url override"() {
given:
def repo = givenGroovyExistsInGradleRepo()
propertiesFile << "org.gradle.internal.gradle.libs.repo.override=$repo.uri/"
buildScript """
apply plugin: "java"
apply plugin: "idea"
apply plugin: "eclipse"

dependencies {
implementation gradleTestKit()
}
"""
when:
succeeds ideTask
then:
ideFileContainsEntry("groovy-${groovyVersion}.jar", ["groovy-${groovyVersion}-sources.jar"], [])
}
@ToBeFixedForConfigurationCache
def "does not download localGroovy() sources when sources download is disabled"() {
given:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void file(Action<? super XmlFileContentMerger> action) {
public List<ClasspathEntry> resolveDependencies() {
ProjectInternal projectInternal = (ProjectInternal) this.project;
IdeArtifactRegistry ideArtifactRegistry = projectInternal.getServices().get(IdeArtifactRegistry.class);
ClasspathFactory classpathFactory = new ClasspathFactory(this, ideArtifactRegistry, new DefaultGradleApiSourcesResolver(projectInternal.newDetachedResolver()), EclipseClassPathUtil.isInferModulePath(this.project));
ClasspathFactory classpathFactory = new ClasspathFactory(this, ideArtifactRegistry, new DefaultGradleApiSourcesResolver(projectInternal), EclipseClassPathUtil.isInferModulePath(this.project));
return classpathFactory.createEntries();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package org.gradle.plugins.ide.internal.resolver;

import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.artifacts.result.ArtifactResolutionResult;
import org.gradle.api.artifacts.result.ArtifactResult;
import org.gradle.api.artifacts.result.ComponentArtifactsResult;
import org.gradle.api.artifacts.result.ResolvedArtifactResult;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.internal.project.ProjectInternal.DetachedResolver;
import org.gradle.jvm.JvmLibrary;
import org.gradle.language.base.artifact.SourcesArtifact;
import org.gradle.util.internal.VersionNumber;

import javax.annotation.Nullable;
import java.io.File;
import java.util.Collections;
import java.util.regex.Matcher;
Expand All @@ -37,13 +38,14 @@ public class DefaultGradleApiSourcesResolver implements GradleApiSourcesResolver

private static final String GRADLE_LIBS_REPO_URL = "https://repo.gradle.org/gradle/list/libs-releases";
private static final String GRADLE_LIBS_REPO_OVERRIDE_VAR = "GRADLE_LIBS_REPO_OVERRIDE";
private static final String GRADLE_LIBS_REPO_OVERRIDE_PROPERTY = "org.gradle.internal.gradle.libs.repo.override";
private static final Pattern FILE_NAME_PATTERN = Pattern.compile("(groovy(-.+?)?)-(\\d.+?)\\.jar");

private final DetachedResolver resolver;

public DefaultGradleApiSourcesResolver(DetachedResolver resolver) {
this.resolver = resolver;
addGradleLibsRepository();
public DefaultGradleApiSourcesResolver(ProjectInternal projectInternal) {
this.resolver = projectInternal.newDetachedResolver();
addGradleLibsRepository((String) projectInternal.findProperty(GRADLE_LIBS_REPO_OVERRIDE_PROPERTY));
}

@Override
Expand Down Expand Up @@ -73,14 +75,17 @@ private File downloadLocalGroovySources(String artifact, VersionNumber version)
return null;
}

private MavenArtifactRepository addGradleLibsRepository() {
return resolver.getRepositories().maven(a -> {
private void addGradleLibsRepository(@Nullable String repoUrlOverride) {
resolver.getRepositories().maven(a -> {
a.setName("Gradle Libs");
a.setUrl(gradleLibsRepoUrl());
a.setUrl(gradleLibsRepoUrl(repoUrlOverride));
});
}

private static String gradleLibsRepoUrl() {
private static String gradleLibsRepoUrl(@Nullable String repoUrlOverride) {
if (repoUrlOverride != null) {
return repoUrlOverride;
}
String repoOverride = System.getenv(GRADLE_LIBS_REPO_OVERRIDE_VAR);
return repoOverride != null ? repoOverride : GRADLE_LIBS_REPO_URL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ Specifies the JDK installation directory to use for the client VM.
This VM is also used for the daemon unless a different one is specified in a Gradle properties file with `org.gradle.java.home`.

`GRADLE_LIBS_REPO_OVERRIDE`::
Overrides for the default Gradle library repository.
Overrides for the default Gradle library repository. The default Gradle library repository can also be overridden
with the `org.gradle.internal.gradle.libs.repo.override` property.
+
Can be used to specify a default Gradle repository URL in `org.gradle.plugins.ide.internal.resolver`.
+
Expand Down

0 comments on commit 684da6a

Please sign in to comment.