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

Update avoid using maven central as CDN

This commit is contained in:
Dreeam
2025-06-26 05:05:34 +08:00
parent 65fe23bcb1
commit d178297806
2 changed files with 55 additions and 24 deletions

View File

@@ -8,13 +8,23 @@ Original project: https://github.com/PaperMC/Paper
https://github.com/PaperMC/Paper/commit/62b7f86dae659deb2fc450285452d7c1439f92dc https://github.com/PaperMC/Paper/commit/62b7f86dae659deb2fc450285452d7c1439f92dc
Dreeam's Note:
We use soft warning which in Paper is directly exception throw,
Only print warning to avoid break plugins directly.
Also see `Configurable LibraryLoader maven repos` patch,
able to match googleapi mirror urls, to replace to repo url we custom defined.
This able to switch back to maven central repo, to prevent
situation that dependencies exist in maven central, but not in mirror.
---
Default LibraryLoader to Google's Maven Central mirror, add MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR, and warn on use of Maven Central with MavenLibraryResolver 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/maven-central-and-the-tragedy-of-the-commons
https://www.sonatype.com/blog/beyond-ips-addressing-organizational-overconsumption-in-maven-central 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 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 index 107705db2d82b7c191e5e625ec888e0bc3b03831..f12c25f35f3d26dc69e05c7965385e66a3d3d545 100644
--- a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java --- 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 +++ b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java
@@ -41,7 +41,7 @@ import org.slf4j.LoggerFactory; @@ -41,7 +41,7 @@ import org.slf4j.LoggerFactory;
@@ -51,22 +61,23 @@ index 107705db2d82b7c191e5e625ec888e0bc3b03831..ebb52c2c8d5fe8ca25513aadae816818
private static final Logger LOGGER = LoggerFactory.getLogger("MavenLibraryResolver"); private static final Logger LOGGER = LoggerFactory.getLogger("MavenLibraryResolver");
private final RepositorySystem repository; private final RepositorySystem repository;
@@ -105,6 +123,14 @@ public class MavenLibraryResolver implements ClassPathLibrary { @@ -105,6 +123,15 @@ public class MavenLibraryResolver implements ClassPathLibrary {
* dependencies from * dependencies from
*/ */
public void addRepository(final RemoteRepository remoteRepository) { public void addRepository(final RemoteRepository remoteRepository) {
+ // Paper start - Avoid and discourage use of Maven Central as a CDN + // Paper start - Avoid and discourage use of Maven Central as a CDN
+ if (MAVEN_CENTRAL_URLS.stream().anyMatch(remoteRepository.getUrl()::startsWith)) { + if (MAVEN_CENTRAL_URLS.stream().anyMatch(remoteRepository.getUrl()::startsWith)) {
+ LOGGER.warn( + try {
+ "Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.", + throw new RuntimeException("Plugin used Maven Central for library resolution");
+ 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);
+ }
+ } + }
+ // Paper end - Avoid and discourage use of Maven Central as a CDN + // Paper end - Avoid and discourage use of Maven Central as a CDN
this.repositories.add(remoteRepository); this.repositories.add(remoteRepository);
} }
@@ -130,4 +156,17 @@ public class MavenLibraryResolver implements ClassPathLibrary { @@ -130,4 +157,17 @@ public class MavenLibraryResolver implements ClassPathLibrary {
store.addLibrary(file.toPath()); store.addLibrary(file.toPath());
} }
} }

View File

@@ -7,17 +7,17 @@ Add JVM flag `-DLeaf.library-download-repo=link` to choose library download repo
e.g. `-DLeaf.library-download-repo=https://maven.aliyun.com/repository/public` 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 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 index f12c25f35f3d26dc69e05c7965385e66a3d3d545..768b60e13435dc8eca6fc343081a663a37a6d0e7 100644
--- a/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java --- 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 +++ b/src/main/java/io/papermc/paper/plugin/loader/library/impl/MavenLibraryResolver.java
@@ -60,12 +60,16 @@ public class MavenLibraryResolver implements ClassPathLibrary { @@ -60,12 +60,25 @@ public class MavenLibraryResolver implements ClassPathLibrary {
* <p>This repository is also used by the legacy {@link org.bukkit.plugin.java.LibraryLoader}.</p> * <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(); public static final String MAVEN_CENTRAL_DEFAULT_MIRROR = getDefaultMavenCentralMirror();
- private static final List<String> MAVEN_CENTRAL_URLS = List.of( - private static final List<String> MAVEN_CENTRAL_URLS = List.of(
+ // Leaf start - Configurable LibraryLoader maven repos + // Leaf start - Configurable LibraryLoader maven repos
+ @org.jspecify.annotations.Nullable + @org.jspecify.annotations.Nullable
+ public static final RemoteRepository MAVEN_CENTRAL_MIRROR_REPO = getCentralMirrorRepo(); + public static final RemoteRepository CUSTOM_MAVEN_CENTRAL_MIRROR_REPO = getCustomCentralMirrorRepo();
+ private static final String[] MAVEN_CENTRAL_URLS = new String[]{ + private static final String[] MAVEN_CENTRAL_URLS = new String[]{
"https://repo1.maven.org/maven2", "https://repo1.maven.org/maven2",
"http://repo1.maven.org/maven2", "http://repo1.maven.org/maven2",
@@ -25,45 +25,65 @@ index ebb52c2c8d5fe8ca25513aadae8168180a3d426e..006a86da6afa1b7b80df5df073ebd236
"http://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 + // Leaf end - Configurable LibraryLoader maven repos
// Paper end - Avoid and discourage use of Maven Central as a CDN // Paper end - Avoid and discourage use of Maven Central as a CDN
private static final Logger LOGGER = LoggerFactory.getLogger("MavenLibraryResolver"); private static final Logger LOGGER = LoggerFactory.getLogger("MavenLibraryResolver");
@@ -124,12 +128,21 @@ public class MavenLibraryResolver implements ClassPathLibrary { @@ -124,13 +137,32 @@ public class MavenLibraryResolver implements ClassPathLibrary {
*/ */
public void addRepository(final RemoteRepository remoteRepository) { public void addRepository(final RemoteRepository remoteRepository) {
// Paper start - Avoid and discourage use of Maven Central as a CDN // Paper start - Avoid and discourage use of Maven Central as a CDN
- if (MAVEN_CENTRAL_URLS.stream().anyMatch(remoteRepository.getUrl()::startsWith)) { - if (MAVEN_CENTRAL_URLS.stream().anyMatch(remoteRepository.getUrl()::startsWith)) {
- LOGGER.warn( - try {
- "Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.", - throw new RuntimeException("Plugin used Maven Central for library resolution");
- 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 + // Leaf start - Configurable LibraryLoader maven repos
+ for (String url : MAVEN_CENTRAL_URLS) { + for (String url : MAVEN_CENTRAL_URLS) {
+ if (remoteRepository.getUrl().startsWith(url)) { + if (remoteRepository.getUrl().startsWith(url)) {
+ RemoteRepository mirrorRepo = MAVEN_CENTRAL_MIRROR_REPO; + RemoteRepository mirrorRepo = CUSTOM_MAVEN_CENTRAL_MIRROR_REPO;
+ if (mirrorRepo != null) { + if (mirrorRepo != null) {
+ this.repositories.add(mirrorRepo); + this.repositories.add(mirrorRepo);
+ return; + return;
+ } + }
+ LOGGER.warn( + try {
+ "Use of Maven Central as a CDN is against the Maven Central Terms of Service. Use MavenLibraryResolver.MAVEN_CENTRAL_DEFAULT_MIRROR instead.", + throw new RuntimeException("Plugin used Maven Central for library resolution");
+ 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 + // Leaf end - Configurable LibraryLoader maven repos
// Paper end - Avoid and discourage use of Maven Central as a CDN // Paper end - Avoid and discourage use of Maven Central as a CDN
this.repositories.add(remoteRepository); this.repositories.add(remoteRepository);
} }
@@ -169,4 +182,15 @@ public class MavenLibraryResolver implements ClassPathLibrary { @@ -170,4 +202,15 @@ public class MavenLibraryResolver implements ClassPathLibrary {
return central; return central;
} }
// Paper end - Avoid and discourage use of Maven Central as a CDN // Paper end - Avoid and discourage use of Maven Central as a CDN
+ +
+ // Leaf start - Configurable LibraryLoader maven repos + // Leaf start - Configurable LibraryLoader maven repos
+ @org.jspecify.annotations.Nullable + @org.jspecify.annotations.Nullable
+ private static RemoteRepository getCentralMirrorRepo() { + private static RemoteRepository getCustomCentralMirrorRepo() {
+ String mirrorAddr = System.getProperty("Leaf.library-download-repo"); + String mirrorAddr = System.getProperty("Leaf.library-download-repo");
+ if (mirrorAddr != null) { + if (mirrorAddr != null) {
+ return new RemoteRepository.Builder("central", "default", mirrorAddr).build(); + return new RemoteRepository.Builder("central", "default", mirrorAddr).build();
@@ -73,14 +93,14 @@ index ebb52c2c8d5fe8ca25513aadae8168180a3d426e..006a86da6afa1b7b80df5df073ebd236
+ // Leaf end - Configurable LibraryLoader maven repos + // 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 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 index 998a5278f39d13ed6b0a6d03514658f28b0d420f..330553fe8e6e4f23681df98d2519f10ebb0cfe5d 100644
--- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java --- a/src/main/java/org/bukkit/plugin/java/LibraryLoader.java
+++ b/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 @@ -51,6 +51,7 @@ public class LibraryLoader
// Paper start - Avoid and discourage use of Maven Central as a CDN // Paper start - Avoid and discourage use of Maven Central as a CDN
private static List<RemoteRepository> getRepositories() { 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 + 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()); 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 // Paper end - Avoid and discourage use of Maven Central as a CDN