From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Dreeam <61569423+Dreeam-qwq@users.noreply.github.com> Date: Sat, 3 Feb 2024 18:45:53 -0500 Subject: [PATCH] Configurable LibraryLoader maven repos Add JVM flag `-DLeaf.library-download-repo=link` to choose library download repo link. e.g. `-DLeaf.library-download-repo=https://maven.aliyun.com/repository/public` diff --git a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java index f12c25f35f3d26dc69e05c7965385e66a3d3d545..f5fb8c3214a421c4d4f681176335dc92cf59834a 100644 --- a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java +++ b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java @@ -60,12 +60,25 @@ public class MavenLibraryResolver implements ClassPathLibrary { *

This repository is also used by the legacy {@link org.bukkit.plugin.java.LibraryLoader}.

*/ public static final String MAVEN_CENTRAL_DEFAULT_MIRROR = getDefaultMavenCentralMirror(); - private static final List MAVEN_CENTRAL_URLS = List.of( + // Leaf start - Configurable LibraryLoader maven repos + @org.jspecify.annotations.Nullable + public static final RemoteRepository CUSTOM_MAVEN_CENTRAL_MIRROR_REPO = getCustomCentralMirrorRepo(); + private static final String[] MAVEN_CENTRAL_URLS = new String[]{ "https://repo1.maven.org/maven2", "http://repo1.maven.org/maven2", "https://repo.maven.apache.org/maven2", "http://repo.maven.apache.org/maven2" - ); + }; + // From https://storage-download.googleapis.com/maven-central/index.html + private static final String[] MAVEN_CENTRAL_GOOGLE_MIRROR_URLS = new String[]{ + "https://maven-central.storage-download.googleapis.com/maven2", + "http://maven-central.storage-download.googleapis.com/maven2", + "https://maven-central-eu.storage-download.googleapis.com/maven2", + "http://maven-central-eu.storage-download.googleapis.com/maven2", + "https://maven-central-asia.storage-download.googleapis.com/maven2", + "http://maven-central-asia.storage-download.googleapis.com/maven2" + }; + // Leaf end - Configurable LibraryLoader maven repos // Paper end - Avoid and discourage use of Maven Central as a CDN private static final Logger LOGGER = LoggerFactory.getLogger("MavenLibraryResolver"); @@ -124,13 +137,32 @@ public class MavenLibraryResolver implements ClassPathLibrary { */ public void addRepository(final RemoteRepository remoteRepository) { // Paper start - Avoid and discourage use of Maven Central as a CDN - if (MAVEN_CENTRAL_URLS.stream().anyMatch(remoteRepository.getUrl()::startsWith)) { - try { - throw new RuntimeException("Plugin used Maven Central for library resolution"); - } catch (RuntimeException e) { - LOGGER.error("Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.", e); + // Leaf start - Configurable LibraryLoader maven repos + for (String url : MAVEN_CENTRAL_URLS) { + if (remoteRepository.getUrl().startsWith(url)) { + RemoteRepository mirrorRepo = CUSTOM_MAVEN_CENTRAL_MIRROR_REPO; + if (mirrorRepo != null) { + this.repositories.add(mirrorRepo); + return; + } + try { + throw new RuntimeException("Plugin used Maven Central for library resolution"); + } catch (RuntimeException e) { + LOGGER.error("Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.", e); + } } } + // Match google api maven central mirror urls + for (String url : MAVEN_CENTRAL_GOOGLE_MIRROR_URLS) { + if (remoteRepository.getUrl().startsWith(url)) { + RemoteRepository mirrorRepo = CUSTOM_MAVEN_CENTRAL_MIRROR_REPO; + if (mirrorRepo != null) { + this.repositories.add(mirrorRepo); + return; + } + } + } + // Leaf end - Configurable LibraryLoader maven repos // Paper end - Avoid and discourage use of Maven Central as a CDN this.repositories.add(remoteRepository); } @@ -170,4 +202,15 @@ public class MavenLibraryResolver implements ClassPathLibrary { return central; } // Paper end - Avoid and discourage use of Maven Central as a CDN + + // Leaf start - Configurable LibraryLoader maven repos + @org.jspecify.annotations.Nullable + private static RemoteRepository getCustomCentralMirrorRepo() { + String mirrorAddr = System.getProperty("Leaf.library-download-repo"); + if (mirrorAddr != null) { + return new RemoteRepository.Builder("central", "default", mirrorAddr).build(); + } + return null; + } + // Leaf end - Configurable LibraryLoader maven repos } diff --git a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java index 1f2e86e9a830b990bbba3704889671465816ea1c..fce316374a0ae06a34acbce0907dfde52b7905e8 100644 --- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java +++ b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java @@ -49,6 +49,7 @@ public class LibraryLoader { // Paper start - Avoid and discourage use of Maven Central as a CDN private static List getRepositories() { + if (io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver.CUSTOM_MAVEN_CENTRAL_MIRROR_REPO != null) return List.of(io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver.CUSTOM_MAVEN_CENTRAL_MIRROR_REPO); // Leaf - Configurable LibraryLoader maven repos return List.of(new RemoteRepository.Builder("central", "default", io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR).build()); } // Paper end - Avoid and discourage use of Maven Central as a CDN