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

Allow overriding the default Gradle library repository through a new Gradle property #26743

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public void setPathVariables(Map<String, File> pathVariables) {
* The JDK to use for this module.
* If {@code null}, the value of the existing or default ipr XML (inherited) is used.
* If it is set to <code>inherited</code>, the project SDK is used.
* Otherwise the SDK for the corresponding value of java version is used for this module.
* Otherwise, the SDK for the corresponding value of java version is used for this module.
* <p>
* For example see docs for {@link IdeaModule}
*/
Expand Down Expand Up @@ -621,7 +621,11 @@ 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,
DefaultGradleApiSourcesResolver.newSourcesResolverWithDefaultRepoConfig(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,12 @@ 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,
DefaultGradleApiSourcesResolver.newSourcesResolverWithDefaultRepoConfig(projectInternal),
EclipseClassPathUtil.isInferModulePath(this.project)
);
return classpathFactory.createEntries();
}

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

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

import com.google.common.annotations.VisibleForTesting;
import org.gradle.api.Action;
import org.gradle.api.artifacts.dsl.DependencyHandler;
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 java.io.File;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -37,13 +43,45 @@ 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;
private final DependencyHandler dependencyHandler;

public DefaultGradleApiSourcesResolver(DetachedResolver resolver) {
this.resolver = resolver;
addGradleLibsRepository();
public DefaultGradleApiSourcesResolver(DependencyHandler dependencyHandler) {
this.dependencyHandler = dependencyHandler;
}

public static DefaultGradleApiSourcesResolver newSourcesResolverWithDefaultRepoConfig(ProjectInternal projectInternal) {
return newSourcesResolver(
projectInternal,
a -> {
a.setName("Gradle Libs");
a.setUrl(gradleLibsRepoUrl(
GRADLE_LIBS_REPO_URL,
() -> Objects.toString(projectInternal.findProperty(GRADLE_LIBS_REPO_OVERRIDE_PROPERTY), null),
() -> System.getenv(GRADLE_LIBS_REPO_OVERRIDE_VAR)
));
}
);
}

@SafeVarargs
@VisibleForTesting
static String gradleLibsRepoUrl(String defaultRepoUrl, Supplier<String>... repoUrlOverrideSources) {
for (Supplier<String> repoUrlOverrideSource : repoUrlOverrideSources) {
String repoUrlOverride = repoUrlOverrideSource.get();
if (repoUrlOverride != null) {
return repoUrlOverride;
}
}
return defaultRepoUrl;
}

public static DefaultGradleApiSourcesResolver newSourcesResolver(ProjectInternal projectInternal, Action<? super MavenArtifactRepository> repoConfigAction) {
DetachedResolver resolver = projectInternal.newDetachedResolver();
resolver.getRepositories().maven(repoConfigAction);
return new DefaultGradleApiSourcesResolver(resolver.getDependencies());
}

@Override
Expand All @@ -58,7 +96,7 @@ public File resolveLocalGroovySources(String jarName) {
}

private File downloadLocalGroovySources(String artifact, VersionNumber version) {
ArtifactResolutionResult result = resolver.getDependencies().createArtifactResolutionQuery()
ArtifactResolutionResult result = dependencyHandler.createArtifactResolutionQuery()
.forModule(groovyGroupName(version), artifact, version.toString())
.withArtifacts(JvmLibrary.class, Collections.singletonList(SourcesArtifact.class))
.execute();
Expand All @@ -72,16 +110,4 @@ private File downloadLocalGroovySources(String artifact, VersionNumber version)
}
return null;
}

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

private static String gradleLibsRepoUrl() {
String repoOverride = System.getenv(GRADLE_LIBS_REPO_OVERRIDE_VAR);
return repoOverride != null ? repoOverride : GRADLE_LIBS_REPO_URL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 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
*
* http://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 org.gradle.plugins.ide.internal.resolver

import spock.lang.Specification

import java.util.function.Supplier

class DefaultGradleApiSourcesResolverTest extends Specification {

def "when using multiple repo url override sources then the first not null gradle repo url override is used"() {
String repoUrl
when:
repoUrl = DefaultGradleApiSourcesResolver.gradleLibsRepoUrl(
"d",
{ null } as Supplier<String>,
{ "a" } as Supplier<String>,
{ "b" } as Supplier<String>
)
then:
repoUrl == "a"
}

def "when all repo url override sources return null then the default gradle repo url is used"() {
String repoUrl
when:
repoUrl = DefaultGradleApiSourcesResolver.gradleLibsRepoUrl(
"d",
{ null } as Supplier<String>,
{ null } as Supplier<String>,
{ null } as Supplier<String>
)
then:
repoUrl == "d"
}

def "when there are no repo url override sources then the default gradle repo url is used"() {
String repoUrl
when:
repoUrl = DefaultGradleApiSourcesResolver.gradleLibsRepoUrl("d")
then:
repoUrl == "d"
}
}