Skip to content

Commit 9836011

Browse files
committed
Create extension to configure Spring maven repositories
Create a small Groovy script that can be used in `settings.gradle` files to extend `repositories` to support the various maven repositories required for our build. See spring-projectsgh-42333
1 parent df73191 commit 9836011

File tree

25 files changed

+450
-75
lines changed

25 files changed

+450
-75
lines changed

build.gradle

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ defaultTasks 'build'
99

1010
allprojects {
1111
group "org.springframework.boot"
12+
}
13+
14+
subprojects {
15+
apply plugin: "org.springframework.boot.conventions"
1216

1317
repositories {
1418
mavenCentral()
19+
spring.mavenRepositories()
1520
if (version.contains('-')) {
1621
maven { url "https://repo.spring.io/milestone" }
1722
}
@@ -25,6 +30,3 @@ allprojects {
2530
}
2631
}
2732

28-
subprojects {
29-
apply plugin: "org.springframework.boot.conventions"
30-
}
+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
//
18+
// This script can be used in the `pluginManagement` block of a `settings.gradle` file to provide
19+
// support for spring maven repositories.
20+
//
21+
// To use the script add the following as the first line in the `pluginManagement` block:
22+
//
23+
// evaluate(new File("${rootDir}/buildSrc/SpringRepositorySupport.groovy")).apply(this)
24+
//
25+
// You can then use `spring.mavenRepositories()` to add the Spring repositories required for the
26+
// version being built.
27+
//
28+
29+
def apply(settings) {
30+
def version = property(settings, 'version')
31+
def buildType = property(settings, 'spring.build-type')
32+
SpringRepositoriesExtension.addTo(settings.pluginManagement.repositories, version, buildType)
33+
settings.gradle.allprojects {
34+
SpringRepositoriesExtension.addTo(repositories, version, buildType)
35+
}
36+
}
37+
38+
private def property(settings, name) {
39+
def parentValue = settings.gradle.parent?.rootProject?.findProperty(name)
40+
return (parentValue != null) ? parentValue : settings.ext[name]
41+
}
42+
43+
return this
44+
45+
class SpringRepositoriesExtension {
46+
47+
private final def repositories
48+
private final def version
49+
private final def buildType
50+
private final def environment
51+
52+
@javax.inject.Inject
53+
SpringRepositoriesExtension(repositories, version, buildType) {
54+
this(repositories, version, buildType, System::getenv)
55+
}
56+
57+
SpringRepositoriesExtension(repositories, version, buildType, environment) {
58+
this.repositories = repositories
59+
this.version = version
60+
this.buildType = buildType
61+
this.environment = environment
62+
}
63+
64+
def mavenRepositories() {
65+
addRepositories { }
66+
}
67+
68+
def mavenRepositories(condition) {
69+
if (condition) addRepositories { }
70+
}
71+
72+
def mavenRepositoriesExcludingBootGroup() {
73+
addRepositories { maven ->
74+
maven.content { content ->
75+
content.excludeGroup("org.springframework.boot")
76+
}
77+
}
78+
}
79+
80+
private void addRepositories(action) {
81+
addCommercialRepository("release", "/spring-enterprise-maven-prod-local", action)
82+
if (this.version.contains("-")) {
83+
addOssRepository("milestone", "/milestone", action)
84+
}
85+
if (this.version.endsWith("-SNAPSHOT")) {
86+
addCommercialRepository("snapshot", "/spring-enterprise-maven-dev-local", action)
87+
addOssRepository("snapshot", "/snapshot", action)
88+
}
89+
}
90+
91+
private void addOssRepository(id, path, action) {
92+
def name = "spring-oss-" + id
93+
def url = "https://repo.spring.io" + path
94+
addRepository(name, url, action)
95+
}
96+
97+
private void addCommercialRepository(id, path, action) {
98+
if (!"commercial".equalsIgnoreCase(this.buildType)) return
99+
def name = "spring-commercial-" + id
100+
def url = fromEnv("COMMERCIAL_%SREPO_URL", id, "https://usw1.packages.broadcom.com" + path)
101+
def username = fromEnv("COMMERCIAL_%SREPO_USERNAME", id)
102+
def password = fromEnv("COMMERCIAL_%SREPO_PASSWORD", id)
103+
addRepository(name, url, { maven ->
104+
maven.credentials { credentials ->
105+
credentials.setUsername(username)
106+
credentials.setPassword(password)
107+
}
108+
action(maven)
109+
})
110+
}
111+
112+
private void addRepository(name, url, action) {
113+
this.repositories.maven { maven ->
114+
maven.setName(name)
115+
maven.setUrl(url)
116+
action(maven)
117+
}
118+
}
119+
120+
private String fromEnv(template, id) {
121+
return fromEnv(template, id, null)
122+
}
123+
124+
private String fromEnv(template, id, defaultValue) {
125+
String value = this.environment.apply(template.formatted(id.toUpperCase() + "_"))
126+
value = (value != null) ? value : this.environment.apply(template.formatted(""))
127+
return (value != null) ? value : defaultValue
128+
}
129+
130+
static def addTo(repositories, version, buildType) {
131+
repositories.extensions.create("spring", SpringRepositoriesExtension.class, repositories, version, buildType)
132+
}
133+
134+
}

buildSrc/build.gradle

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ java {
1515
targetCompatibility = 17
1616
}
1717

18-
19-
if ("${springFrameworkVersion}".contains("-")) {
20-
repositories {
21-
maven { url "https://repo.spring.io/milestone" }
22-
maven { url "https://repo.spring.io/snapshot" }
23-
}
18+
repositories {
19+
spring.mavenRepositories("${springFrameworkVersion}".contains("-"))
2420
}
2521

2622
checkstyle {
@@ -51,6 +47,7 @@ dependencies {
5147
testImplementation("org.assertj:assertj-core:${assertjVersion}")
5248
testImplementation("org.hamcrest:hamcrest:${hamcrestVersion}")
5349
testImplementation("org.junit.jupiter:junit-jupiter:${junitJupiterVersion}")
50+
testImplementation("org.mockito:mockito-core:${mockitoVersion}")
5451
testImplementation("org.springframework:spring-test")
5552

5653
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

buildSrc/settings.gradle

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
pluginManagement {
2+
new File(rootDir.parentFile, "gradle.properties").withInputStream {
3+
def properties = new Properties()
4+
properties.load(it)
5+
properties.forEach(settings.ext::set)
6+
gradle.rootProject {
7+
properties.forEach(project.ext::set)
8+
}
9+
}
10+
evaluate(new File("${rootDir}/SpringRepositorySupport.groovy")).apply(this)
211
repositories {
312
mavenCentral()
413
gradlePluginPortal()
514
}
615
}
7-
8-
gradle.rootProject((project) -> {
9-
new File(rootDir.parentFile, "gradle.properties").withInputStream {
10-
def properties = new Properties()
11-
properties.load(it)
12-
properties.forEach(project.ext::set)
13-
}
14-
});

0 commit comments

Comments
 (0)