Skip to content

Commit 5eaef88

Browse files
committed
Attempt to fix recent hiccup of official maven repo
1 parent 930b94c commit 5eaef88

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

buildSrc/build.gradle.kts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18+
import java.net.HttpURLConnection
19+
import java.net.URL
20+
21+
fun checkMavenCentralAccessibility(): Boolean {
22+
val testUrl = "https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.2/slf4j-api-1.7.2.pom"
23+
return try {
24+
val connection = URL(testUrl).openConnection() as HttpURLConnection
25+
connection.requestMethod = "HEAD"
26+
connection.connectTimeout = 2000
27+
connection.readTimeout = 2000
28+
connection.useCaches = false
29+
connection.responseCode == 200
30+
} catch (e: Exception) {
31+
false
32+
}
33+
}
34+
35+
val isMavenAccessible = checkMavenCentralAccessibility()
36+
val mavenCentralMirrorUrl = "https://maven-central.storage-api.googleapis.com/repos/central/data/"
1837

1938
// Plugins for configuring _this build_ of the module
2039
plugins {
@@ -25,7 +44,13 @@ plugins {
2544

2645
// Define the set of repositories required to fetch and enable plugins.
2746
repositories {
28-
maven { url = uri("https://plugins.gradle.org/m2/") }
47+
if (isMavenAccessible) {
48+
maven { url = uri("https://plugins.gradle.org/m2/") }
49+
} else {
50+
logger.lifecycle("Maven Central is not accessible. Falling back to Google Maven Mirror for buildSrc.")
51+
maven { url = uri(mavenCentralMirrorUrl) }
52+
maven { url = uri("https://plugins.gradle.org/m2/") }
53+
}
2954
maven {
3055
url = uri("https://repo.spring.io/plugins-release/")
3156
content { includeGroup("io.spring.gradle") }

buildSrc/src/main/groovy/org/apache/beam/gradle/Repositories.groovy

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,32 @@
1919
package org.apache.beam.gradle
2020

2121
import org.gradle.api.Project
22+
import java.net.HttpURLConnection
23+
import java.net.URL
2224

2325
class Repositories {
2426

27+
private static Boolean isMavenAccessible = null
28+
29+
static synchronized boolean checkMavenCentral() {
30+
if (isMavenAccessible != null) {
31+
return isMavenAccessible
32+
}
33+
def testUrl = "https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.2/slf4j-api-1.7.2.pom"
34+
try {
35+
def url = new URL(testUrl)
36+
def connection = (HttpURLConnection) url.openConnection()
37+
connection.requestMethod = "HEAD"
38+
connection.connectTimeout = 2000
39+
connection.readTimeout = 2000
40+
connection.useCaches = false
41+
isMavenAccessible = (connection.responseCode == 200)
42+
} catch (Exception e) {
43+
isMavenAccessible = false
44+
}
45+
return isMavenAccessible
46+
}
47+
2548
static void register(Project project) {
2649

2750
project.repositories {
@@ -36,7 +59,12 @@ class Repositories {
3659
return
3760
}
3861

39-
mavenCentral()
62+
if (checkMavenCentral()) {
63+
mavenCentral()
64+
} else {
65+
project.logger.lifecycle "Maven Central is not accessible. Redirecting to Google Maven Mirror."
66+
maven { url "https://maven-central.storage-api.googleapis.com/repos/central/data/" }
67+
}
4068
mavenLocal()
4169

4270
// For Confluent Kafka dependencies
@@ -68,8 +96,12 @@ class Repositories {
6896
project.offlineDependencies {
6997
repositories {
7098
mavenLocal()
71-
mavenCentral()
72-
maven { url "https://plugins.gradle.org/m2/" }
99+
if (checkMavenCentral()) {
100+
mavenCentral()
101+
maven { url "https://plugins.gradle.org/m2/" }
102+
} else {
103+
maven { url "https://maven-central.storage-api.googleapis.com/repos/central/data/" }
104+
}
73105
maven { url "https://repo.spring.io/plugins-release" }
74106
maven { url "https://packages.confluent.io/maven/" }
75107
maven { url project.offlineRepositoryRoot }

settings.gradle.kts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@
1818
import com.gradle.enterprise.gradleplugin.internal.extension.BuildScanExtensionWithHiddenFeatures
1919

2020
pluginManagement {
21+
val isMavenAccessible = try {
22+
val connection = java.net.URL("https://repo.maven.apache.org/maven2/org/slf4j/slf4j-api/1.7.2/slf4j-api-1.7.2.pom").openConnection() as java.net.HttpURLConnection
23+
connection.requestMethod = "HEAD"
24+
connection.connectTimeout = 2000
25+
connection.readTimeout = 2000
26+
connection.useCaches = false
27+
connection.responseCode == 200
28+
} catch (e: Exception) {
29+
false
30+
}
31+
val mavenCentralMirrorUrl = "https://maven-central.storage-api.googleapis.com/repos/central/data/"
32+
33+
repositories {
34+
if (isMavenAccessible) {
35+
gradlePluginPortal()
36+
} else {
37+
maven { url = uri(mavenCentralMirrorUrl) }
38+
gradlePluginPortal()
39+
}
40+
}
2141
plugins {
2242
id("org.javacc.javacc") version "4.0.3" // enable the JavaCC parser generator
2343
}

0 commit comments

Comments
 (0)