From c9f6ca28675d9680a6713ab0c05b3b0b4c583c16 Mon Sep 17 00:00:00 2001 From: Sebastian Hartte Date: Wed, 25 Dec 2024 23:51:30 +0100 Subject: [PATCH] Do not use internals --- .../net/neoforged/neodev/NeoDevPlugin.java | 25 ++++++++++--------- settings.gradle | 5 ++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/buildSrc/src/main/java/net/neoforged/neodev/NeoDevPlugin.java b/buildSrc/src/main/java/net/neoforged/neodev/NeoDevPlugin.java index 7c26374e8c..4b044b450c 100644 --- a/buildSrc/src/main/java/net/neoforged/neodev/NeoDevPlugin.java +++ b/buildSrc/src/main/java/net/neoforged/neodev/NeoDevPlugin.java @@ -24,7 +24,6 @@ import org.gradle.api.artifacts.repositories.MavenArtifactRepository; import org.gradle.api.file.Directory; import org.gradle.api.file.RegularFile; -import org.gradle.api.internal.GradleInternal; import org.gradle.api.plugins.BasePluginExtension; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginExtension; @@ -38,6 +37,7 @@ import java.io.File; import java.net.URI; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -455,19 +455,20 @@ public void apply(Project project) { private static Provider> getInstallerRepositoryUrls(Project project) { return project.provider(() -> { List repos = new ArrayList<>(); - var repositories = project.getRepositories(); - if (repositories.isEmpty()) { - // If no project repos are defined, check dependency management on the settings plugin, - // which sadly is inaccessible without internals: https://github.com/gradle/gradle/issues/27260 - repositories = ((GradleInternal) project.getGradle()).getSettings().getDependencyResolutionManagement().getRepositories(); - } - for (var repo : repositories.withType(MavenArtifactRepository.class)) { - var uri = repo.getUrl(); - if (!uri.toString().endsWith("/")) { - uri = URI.create(uri + "/"); + var projectRepos = project.getRepositories(); + if (!projectRepos.isEmpty()) { + for (var repo : projectRepos.withType(MavenArtifactRepository.class)) { + repos.add(repo.getUrl()); } - repos.add(uri); + } else { + // If no project repos are defined, use the repository list we exposed in settings.gradle via an extension + // See the end of settings.gradle for details + Collections.addAll(repos, (URI[]) project.getGradle().getExtensions().getByName("repositoryBaseUrls")); } + + // Ensure all base urls end with a slash + repos.replaceAll(uri -> uri.toString().endsWith("/") ? uri : URI.create(uri + "/")); + return repos; }); } diff --git a/settings.gradle b/settings.gradle index 8ebcc3b576..d176b5c80b 100644 --- a/settings.gradle +++ b/settings.gradle @@ -44,3 +44,8 @@ include ':coremods' // Ensure a unique artifact id within JIJ that does not conflict with net.neoforged:coremods, which is // another external dependency. project(":coremods").name = "neoforge-coremods" + +// Expose the repository base URLs to projects to be able to access them +// See this Gradle issue for tracking a more permanent solution: +// https://github.com/gradle/gradle/issues/27260 +gradle.extensions.add("repositoryBaseUrls", dependencyResolutionManagement.repositories.findAll { it instanceof MavenArtifactRepository }.collect { it.url }.toArray(URI[]::new))