Skip to content

Commit 88f33ae

Browse files
committed
Implement Maven Central mirroring in CI environments
1 parent 930b94c commit 88f33ae

5 files changed

Lines changed: 97 additions & 4 deletions

File tree

buildSrc/build.gradle.kts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18+
import java.util.Properties
19+
20+
val parentProperties = Properties().apply {
21+
val file = file("../gradle.properties")
22+
if (file.exists()) {
23+
file.inputStream().use { load(it) }
24+
}
25+
}
26+
27+
val mavenCentralMirrorUrl = parentProperties.getProperty("mavenCentralMirrorUrl")
28+
val isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
29+
val useMirror = isCi && !mavenCentralMirrorUrl.isNullOrBlank()
1830

1931
// Plugins for configuring _this build_ of the module
2032
plugins {
@@ -25,6 +37,13 @@ plugins {
2537

2638
// Define the set of repositories required to fetch and enable plugins.
2739
repositories {
40+
if (useMirror) {
41+
logger.lifecycle("Running in CI. Mirroring Maven Central repositories via Google Maven Mirror for buildSrc.")
42+
}
43+
44+
if (useMirror) {
45+
maven { url = uri(mavenCentralMirrorUrl!!) }
46+
}
2847
maven { url = uri("https://plugins.gradle.org/m2/") }
2948
maven {
3049
url = uri("https://repo.spring.io/plugins-release/")

buildSrc/settings.gradle.kts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.util.Properties
20+
21+
pluginManagement {
22+
val parentProperties = java.util.Properties().apply {
23+
val file = file("../gradle.properties")
24+
if (file.exists()) {
25+
file.inputStream().use { load(it) }
26+
}
27+
}
28+
29+
val mavenCentralMirrorUrl = parentProperties.getProperty("mavenCentralMirrorUrl")
30+
val isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
31+
val useMirror = isCi && !mavenCentralMirrorUrl.isNullOrBlank()
32+
33+
if (useMirror) {
34+
logger.lifecycle("Running in CI. Mirroring Maven Central repositories via Google Maven Mirror for buildSrc plugins.")
35+
}
36+
37+
repositories {
38+
if (useMirror) {
39+
maven { url = uri(mavenCentralMirrorUrl!!) }
40+
}
41+
gradlePluginPortal()
42+
}
43+
}

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@
1919
package org.apache.beam.gradle
2020

2121
import org.gradle.api.Project
22-
2322
class Repositories {
2423

2524
static void register(Project project) {
25+
def mirrorUrl = project.findProperty("mavenCentralMirrorUrl")
26+
boolean isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
27+
def useMirror = isCi && mirrorUrl
28+
29+
if (useMirror) {
30+
project.logger.lifecycle("Running in CI. Mirroring Maven Central repositories via Google Maven Mirror.")
31+
}
2632

2733
project.repositories {
2834
maven { url project.offlineRepositoryRoot }
@@ -36,7 +42,11 @@ class Repositories {
3642
return
3743
}
3844

39-
mavenCentral()
45+
if (!useMirror) {
46+
mavenCentral()
47+
} else {
48+
maven { url mirrorUrl }
49+
}
4050
mavenLocal()
4151

4252
// For Confluent Kafka dependencies
@@ -60,15 +70,19 @@ class Repositories {
6070
// Apply a plugin which provides the 'updateOfflineRepository' task that creates an offline
6171
// repository. This offline repository satisfies all Gradle build dependencies and Java
6272
// project dependencies. The offline repository is placed within $rootDir/offline-repo
63-
// but can be overridden by specifying '-PofflineRepositoryRoot=/path/to/repo'.
73+
// but can be overridden by specifying '-Pareas/offline-repo'.
6474
// Note that parallel build must be disabled when executing 'updateOfflineRepository'
6575
// by specifying '--no-parallel', see
6676
// https://github.com/mdietrichstein/gradle-offline-dependencies-plugin/issues/3
6777
project.apply plugin: "io.pry.gradle.offline_dependencies"
6878
project.offlineDependencies {
6979
repositories {
7080
mavenLocal()
71-
mavenCentral()
81+
if (!useMirror) {
82+
mavenCentral()
83+
} else {
84+
maven { url mirrorUrl }
85+
}
7286
maven { url "https://plugins.gradle.org/m2/" }
7387
maven { url "https://repo.spring.io/plugins-release" }
7488
maven { url "https://packages.confluent.io/maven/" }

gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ flink_versions=1.17,1.18,1.19,1.20,2.0
4444
spark_versions=3,4
4545
# supported python versions
4646
python_versions=3.10,3.11,3.12,3.13,3.14
47+
48+
# Maven Central fallback mirror URL
49+
mavenCentralMirrorUrl=https://maven-central.storage-download.googleapis.com/maven2/

settings.gradle.kts

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

2020
pluginManagement {
21+
val mavenCentralMirrorUrl = settings.providers.gradleProperty("mavenCentralMirrorUrl").orNull
22+
val isCi = System.getenv("GITHUB_ACTIONS") != null || System.getenv("JENKINS_HOME") != null
23+
val useMirror = isCi && !mavenCentralMirrorUrl.isNullOrBlank()
24+
25+
if (useMirror) {
26+
logger.lifecycle("Running in CI. Mirroring Maven Central repositories via Google Maven Mirror.")
27+
}
28+
29+
repositories {
30+
if (useMirror) {
31+
maven { url = uri(mavenCentralMirrorUrl!!) }
32+
}
33+
gradlePluginPortal()
34+
}
2135
plugins {
2236
id("org.javacc.javacc") version "4.0.3" // enable the JavaCC parser generator
2337
}

0 commit comments

Comments
 (0)