9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00

Port Avoid and discourage use of Maven Central as a CDN from Paper & Cleanup Configurable LibraryLoader maven repos

This commit is contained in:
Dreeam
2025-06-22 03:00:38 +08:00
parent 5311ae81c6
commit 5bbafee8a1
12 changed files with 199 additions and 79 deletions

View File

@@ -0,0 +1,112 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jason Penilla <11360596+jpenilla@users.noreply.github.com>
Date: Wed, 18 Jun 2025 10:47:21 -0700
Subject: [PATCH] Paper: Avoid and discourage use of Maven Central as a CDN
Original license: GPLv3
Original project: https://github.com/PaperMC/Paper
https://github.com/PaperMC/Paper/commit/62b7f86dae659deb2fc450285452d7c1439f92dc
Default LibraryLoader to Google's Maven Central mirror, add MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR, and warn on use of Maven Central with MavenLibraryResolver
https://www.sonatype.com/blog/maven-central-and-the-tragedy-of-the-commons
https://www.sonatype.com/blog/beyond-ips-addressing-organizational-overconsumption-in-maven-central
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 107705db2d82b7c191e5e625ec888e0bc3b03831..ebb52c2c8d5fe8ca25513aadae8168180a3d426e 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
@@ -41,7 +41,7 @@ import org.slf4j.LoggerFactory;
* MavenLibraryResolver resolver = new MavenLibraryResolver();
* resolver.addDependency(new Dependency(new DefaultArtifact("org.jooq:jooq:3.17.7"), null));
* resolver.addRepository(new RemoteRepository.Builder(
- * "central", "default", "https://repo1.maven.org/maven2/"
+ * "central", "default", MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR // Paper - Avoid and discourage use of Maven Central as a CDN
* ).build());
* }</pre>
* <p>
@@ -50,6 +50,24 @@ import org.slf4j.LoggerFactory;
@NullMarked
public class MavenLibraryResolver implements ClassPathLibrary {
+ // Paper start - Avoid and discourage use of Maven Central as a CDN
+ /**
+ * The default Maven Central mirror, configurable through the {@code PAPER_DEFAULT_CENTRAL_REPOSITORY} environment
+ * variable. Use this instead of Maven Central directly when you do not have your own mirror, as using
+ * Maven Central as a CDN is against the Maven Central Terms of Service, and you will cause users to hit
+ * rate limits.
+ *
+ * <p>This repository is also used by the legacy {@link org.bukkit.plugin.java.LibraryLoader}.</p>
+ */
+ public static final String MAVEN_CENTRAL_DEFAULT_MIRROR = getDefaultMavenCentralMirror();
+ private static final List<String> MAVEN_CENTRAL_URLS = List.of(
+ "https://repo1.maven.org/maven2",
+ "http://repo1.maven.org/maven2",
+ "https://repo.maven.apache.org/maven2",
+ "http://repo.maven.apache.org/maven2"
+ );
+ // Paper end - Avoid and discourage use of Maven Central as a CDN
+
private static final Logger LOGGER = LoggerFactory.getLogger("MavenLibraryResolver");
private final RepositorySystem repository;
@@ -105,6 +123,14 @@ public class MavenLibraryResolver implements ClassPathLibrary {
* dependencies from
*/
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)) {
+ LOGGER.warn(
+ "Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.",
+ new RuntimeException("Plugin used Maven Central for library resolution")
+ );
+ }
+ // Paper end - Avoid and discourage use of Maven Central as a CDN
this.repositories.add(remoteRepository);
}
@@ -130,4 +156,17 @@ public class MavenLibraryResolver implements ClassPathLibrary {
store.addLibrary(file.toPath());
}
}
+
+ // Paper start - Avoid and discourage use of Maven Central as a CDN
+ private static String getDefaultMavenCentralMirror() {
+ String central = System.getenv("PAPER_DEFAULT_CENTRAL_REPOSITORY");
+ if (central == null) {
+ central = System.getProperty("org.bukkit.plugin.java.LibraryLoader.centralURL");
+ }
+ if (central == null) {
+ central = "https://maven-central.storage-download.googleapis.com/maven2";
+ }
+ return central;
+ }
+ // Paper end - Avoid and discourage use of Maven Central as a CDN
}
diff --git a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
index 211c093ce2253e918cd40725ebf1ef172d1b9bdf..998a5278f39d13ed6b0a6d03514658f28b0d420f 100644
--- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
@@ -49,6 +49,12 @@ public class LibraryLoader
public static java.util.function.BiFunction<URL[], ClassLoader, URLClassLoader> LIBRARY_LOADER_FACTORY; // Paper - rewrite reflection in libraries
public static java.util.function.Function<List<java.nio.file.Path>, List<java.nio.file.Path>> REMAPPER; // Paper - remap libraries
+ // Paper start - Avoid and discourage use of Maven Central as a CDN
+ private static List<RemoteRepository> getRepositories() {
+ 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
+
public LibraryLoader(@NotNull Logger logger)
{
this.logger = logger;
@@ -79,7 +85,7 @@ public class LibraryLoader
session.setSystemProperties( System.getProperties() );
session.setReadOnly();
- this.repositories = repository.newResolutionRepositories( session, Arrays.asList( new RemoteRepository.Builder( "central", "default", "https://repo.maven.apache.org/maven2" ).build() ) );
+ this.repositories = repository.newResolutionRepositories( session, getRepositories()); // Paper - Avoid and discourage use of Maven Central as a CDN
}
@Nullable

View File

@@ -1,78 +0,0 @@
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
TODO - Dreeam: Support multi maven repos for lib downloading.
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 107705db2d82b7c191e5e625ec888e0bc3b03831..77a58fc7c173b1724d44b0eeaf23b4a1b22b5fcb 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
@@ -105,7 +105,7 @@ public class MavenLibraryResolver implements ClassPathLibrary {
* dependencies from
*/
public void addRepository(final RemoteRepository remoteRepository) {
- this.repositories.add(remoteRepository);
+ this.repositories.add(org.dreeam.leaf.plugin.loader.MavenCentralMirror.getCentralRepo(remoteRepository)); // Leaf - 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 211c093ce2253e918cd40725ebf1ef172d1b9bdf..096140a91f19eb31a10631b949f4402e37e1c601 100644
--- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
@@ -79,7 +79,19 @@ public class LibraryLoader
session.setSystemProperties( System.getProperties() );
session.setReadOnly();
- this.repositories = repository.newResolutionRepositories( session, Arrays.asList( new RemoteRepository.Builder( "central", "default", "https://repo.maven.apache.org/maven2" ).build() ) );
+ // Leaf start - Configurable LibraryLoader maven repos
+ this.repositories = repository.newResolutionRepositories(
+ session,
+ List.of(org.dreeam.leaf.plugin.loader.MavenCentralMirror.getCentralRepo("https://repo.maven.apache.org/maven2"))
+ );
+ /* // Dreeam TODO
+ this.repositories = repository.newResolutionRepositories(session, List.of(
+ new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build(),
+ new RemoteRepository.Builder("aliyun", "default", "https://maven.aliyun.com/repository/public").build(),
+ new RemoteRepository.Builder("tencentclound", "default", "https://mirrors.cloud.tencent.com/nexus/repository/maven-public/").build(),
+ new RemoteRepository.Builder("huaweicloud", "default", "https://repo.huaweicloud.com/repository/maven/").build()
+ ));*/
+ // Leaf end - Configurable LibraryLoader maven repos
}
@Nullable
diff --git a/src/main/java/org/dreeam/leaf/plugin/loader/MavenCentralMirror.java b/src/main/java/org/dreeam/leaf/plugin/loader/MavenCentralMirror.java
new file mode 100644
index 0000000000000000000000000000000000000000..95534cb6d771a0fe288e7c843c41b0036bdc7095
--- /dev/null
+++ b/src/main/java/org/dreeam/leaf/plugin/loader/MavenCentralMirror.java
@@ -0,0 +1,24 @@
+package org.dreeam.leaf.plugin.loader;
+
+import org.eclipse.aether.repository.RemoteRepository;
+
+public class MavenCentralMirror {
+
+ public static final String MAVEN_CENTRAL_MIRROR_REPO = System.getProperty("Leaf.library-download-repo");
+
+ public static RemoteRepository getCentralRepo(RemoteRepository repo) {
+ if (MAVEN_CENTRAL_MIRROR_REPO != null && repo.getUrl().contains("repo.maven.apache.org/maven2")) {
+ repo = new RemoteRepository.Builder("central", "default", MAVEN_CENTRAL_MIRROR_REPO).build();
+ }
+
+ return repo;
+ }
+
+ public static RemoteRepository getCentralRepo(String repo) {
+ if (MAVEN_CENTRAL_MIRROR_REPO != null) {
+ repo = MAVEN_CENTRAL_MIRROR_REPO;
+ }
+
+ return new RemoteRepository.Builder("central", "default", repo).build();
+ }
+}

View File

@@ -9,7 +9,7 @@ Original project: https://github.com/Cryptite/Slice
Co-authored-by: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com>
diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java
index 2a8abee51e8fad62b0aa58a47eadfbac2bf51fdf..716f99b4d09ffd415f8a53a90031c92d8931f125 100644
index 69f7a2fa54b667f2bb97454d6be9f6322a9aa43d..c92e0bb60504900fa1d1e7c86534a46578c91799 100644
--- a/src/main/java/org/bukkit/entity/Player.java
+++ b/src/main/java/org/bukkit/entity/Player.java
@@ -3709,6 +3709,33 @@ public interface Player extends HumanEntity, Conversable, OfflinePlayer, PluginM

View File

@@ -0,0 +1,86 @@
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 ebb52c2c8d5fe8ca25513aadae8168180a3d426e..006a86da6afa1b7b80df5df073ebd236e27cd2b5 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,16 @@ public class MavenLibraryResolver implements ClassPathLibrary {
* <p>This repository is also used by the legacy {@link org.bukkit.plugin.java.LibraryLoader}.</p>
*/
public static final String MAVEN_CENTRAL_DEFAULT_MIRROR = getDefaultMavenCentralMirror();
- private static final List<String> MAVEN_CENTRAL_URLS = List.of(
+ // Leaf start - Configurable LibraryLoader maven repos
+ @org.jspecify.annotations.Nullable
+ public static final RemoteRepository MAVEN_CENTRAL_MIRROR_REPO = getCentralMirrorRepo();
+ 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"
- );
+ };
+ // 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,12 +128,21 @@ 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)) {
- LOGGER.warn(
- "Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.",
- new RuntimeException("Plugin used Maven Central for library resolution")
- );
+ // Leaf start - Configurable LibraryLoader maven repos
+ for (String url : MAVEN_CENTRAL_URLS) {
+ if (remoteRepository.getUrl().startsWith(url)) {
+ RemoteRepository mirrorRepo = MAVEN_CENTRAL_MIRROR_REPO;
+ if (mirrorRepo != null) {
+ this.repositories.add(mirrorRepo);
+ return;
+ }
+ LOGGER.warn(
+ "Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.",
+ new RuntimeException("Plugin used Maven Central for library resolution")
+ );
+ }
}
+ // Leaf end - Configurable LibraryLoader maven repos
// Paper end - Avoid and discourage use of Maven Central as a CDN
this.repositories.add(remoteRepository);
}
@@ -169,4 +182,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 getCentralMirrorRepo() {
+ String mirrorAddr = System.getProperty("Leaf.library-download-repo");
+ if (mirrorAddr != null) {
+ 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 998a5278f39d13ed6b0a6d03514658f28b0d420f..2b4f6f305d3e4418b912e36a6b603c412111947f 100644
--- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
@@ -51,6 +51,7 @@ public class LibraryLoader
// Paper start - Avoid and discourage use of Maven Central as a CDN
private static List<RemoteRepository> getRepositories() {
+ if (io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver.MAVEN_CENTRAL_MIRROR_REPO != null) return List.of(io.papermc.paper.plugin.loader.library.impl.MavenLibraryResolver.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