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
+ }
0 commit comments