Skip to content

Commit 4147c8d

Browse files
committed
Consolidate buildSrc checking logic inside buildSrc/settings.gradle.kts and propagate via System property
1 parent 8d19454 commit 4147c8d

2 files changed

Lines changed: 66 additions & 23 deletions

File tree

buildSrc/build.gradle.kts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,18 @@
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
2018
import java.util.Properties
2119

22-
fun checkMavenCentralAccessibility(testUrl: String): Boolean {
23-
val accessible = try {
24-
val connection = URL(testUrl).openConnection() as HttpURLConnection
25-
connection.requestMethod = "GET"
26-
connection.setRequestProperty("User-Agent", "Gradle")
27-
connection.connectTimeout = 2000
28-
connection.readTimeout = 2000
29-
connection.useCaches = false
30-
connection.responseCode == 200
31-
} catch (e: Exception) {
32-
false
33-
}
34-
return accessible
35-
}
36-
3720
val parentProperties = Properties().apply {
3821
val file = file("../gradle.properties")
3922
if (file.exists()) {
4023
file.inputStream().use { load(it) }
4124
}
4225
}
4326

44-
val mavenCentralTestUrl = parentProperties.getProperty("mavenCentralTestUrl")
4527
val mavenCentralMirrorUrl = parentProperties.getProperty("mavenCentralMirrorUrl")
4628

47-
val isMavenAccessible = if (!mavenCentralTestUrl.isNullOrBlank()) {
48-
checkMavenCentralAccessibility(mavenCentralTestUrl)
49-
} else {
50-
true
51-
}
29+
val isMavenAccessible = System.getProperty("buildSrc.maven.accessible")?.toBoolean() ?: true
5230

5331
val useFallbackMirror = !isMavenAccessible && !mavenCentralMirrorUrl.isNullOrBlank()
5432

buildSrc/settings.gradle.kts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 mavenCentralTestUrl = parentProperties.getProperty("mavenCentralTestUrl")
30+
val mavenCentralMirrorUrl = parentProperties.getProperty("mavenCentralMirrorUrl")
31+
32+
val isMavenAccessible = if (!mavenCentralTestUrl.isNullOrBlank()) {
33+
try {
34+
val connection = java.net.URL(mavenCentralTestUrl).openConnection() as java.net.HttpURLConnection
35+
connection.requestMethod = "GET"
36+
connection.setRequestProperty("User-Agent", "Gradle")
37+
connection.connectTimeout = 2000
38+
connection.readTimeout = 2000
39+
connection.useCaches = false
40+
connection.responseCode == 200
41+
} catch (e: Exception) {
42+
false
43+
}
44+
} else {
45+
true
46+
}
47+
System.setProperty("buildSrc.maven.accessible", isMavenAccessible.toString())
48+
49+
val useFallbackMirror = !isMavenAccessible && !mavenCentralMirrorUrl.isNullOrBlank()
50+
51+
if (isMavenAccessible) {
52+
logger.lifecycle("Maven Central is accessible. Using default repositories for buildSrc plugins.")
53+
} else if (useFallbackMirror) {
54+
logger.lifecycle("Maven Central is not accessible. Falling back to Google Maven Mirror for buildSrc plugins.")
55+
} else {
56+
logger.lifecycle("Maven Central is not accessible and no mirror is configured. Using default repositories for buildSrc plugins.")
57+
}
58+
59+
repositories {
60+
if (useFallbackMirror) {
61+
maven { url = uri(mavenCentralMirrorUrl!!) }
62+
}
63+
gradlePluginPortal()
64+
}
65+
}

0 commit comments

Comments
 (0)