forked from line/centraldogma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon-dependencies.gradle
More file actions
370 lines (325 loc) · 14.1 KB
/
Copy pathcommon-dependencies.gradle
File metadata and controls
370 lines (325 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import com.github.benmanes.gradle.versions.reporter.*
import com.github.benmanes.gradle.versions.reporter.result.*
import org.yaml.snakeyaml.Yaml
buildscript {
repositories {
def mavenCentralMirror = providers.gradleProperty('mavenCentralMirror').getOrNull()
if (mavenCentralMirror) {
maven { url = mavenCentralMirror }
} else {
mavenCentral()
}
gradlePluginPortal()
}
dependencies {
// These should be the only dependencies that need hard-coded versions.
classpath 'com.github.ben-manes:gradle-versions-plugin:0.53.0'
classpath 'org.yaml:snakeyaml:1.33'
}
}
allprojects { p ->
ext {
// Add managedVersions() for backward compatibility with dependencies.yml
managedVersions = getManagedVersions(p.rootProject)
findLibrary = this.&findLibrary.curry(p.rootProject)
findPlugin = this.&findPlugin.curry(p.rootProject)
failOnVersionConflict = this.&failOnVersionConflict.curry(p)
}
}
def dependencyManagementProjects = projectsWithFlags('dependencyManagement')
assert !dependencyManagementProjects.isEmpty() // Guaranteed by settings-flags.gradle
def dependencyManagementProject = dependencyManagementProjects[0]
configure(dependencyManagementProject) {
apply plugin: 'java-platform'
repositories {
google()
// Since we manage plugin versions here too.
gradlePluginPortal()
def mavenCentralMirror = providers.gradleProperty('mavenCentralMirror').getOrNull()
if (mavenCentralMirror) {
maven { url = mavenCentralMirror }
} else {
mavenCentral()
}
}
javaPlatform {
allowDependencies()
}
dependencies {
rootProject.ext.dependenciesTomlBoms.each { alias ->
def library = rootProject.ext.findLibrary(alias)
if (library != null) {
api platform(library)
}
}
}
}
configure(rootProject) {
apply plugin: com.github.benmanes.gradle.versions.VersionsPlugin
tasks {
dependencyUpdates {
revision = 'release'
resolutionStrategy {
componentSelection { rules ->
rules.all { ComponentSelection selection ->
boolean rejected = ['alpha', 'beta', 'ea', 'rc',
'cr', 'm', 'preview'].any { qualifier ->
selection.candidate.version ==~ /(?i).*[.-]${qualifier}[.\d-]*/
}
if (rejected) {
selection.reject('Release candidate')
}
}
}
}
checkConstraints = true
outputFormatter = result -> {
File filename = new File(outputDir, 'report.txt')
project.file(outputDir).mkdirs()
File outputFile = project.file(filename)
def reporter = new GentlePlainTextReporter(project, "release", "release-candidate")
reporter.write(System.out, result)
outputFile.withOutputStream { os ->
reporter.write(os, result)
}
logger.lifecycle '\nGenerated report file ' + filename
}
}
}
}
// Publish version catalog so that other projects can easily align transitive versions of the project by
// importing the published catalog.
// https://docs.gradle.org/current/userguide/platforms.html#sec:version-catalog-plugin
configure(projectsWithFlag('version-catalog')) { catalogProject ->
apply plugin: 'version-catalog'
apply plugin: 'maven-publish'
publishing {
publications {
maven(MavenPublication) {
from components.versionCatalog
}
}
}
catalog {
versionCatalog {
def catalogs = rootProject.extensions.getByType(VersionCatalogsExtension).named('libs')
catalogs.libraryAliases.forEach { alias ->
def dep = catalogs.findLibrary(alias).get().get()
if (!dep.versionConstraint.requiredVersion.isEmpty()) {
library(alias, dep.toString())
}
}
catalogs.pluginAliases.forEach { alias ->
def plug = catalogs.findPlugin(alias).get().get()
plugin(alias, plug.pluginId).version(plug.version.requiredVersion)
}
catalogs.versionAliases.forEach { alias ->
def ver = catalogs.findVersion(alias).get()
version(alias, ver.toString())
}
catalogs.bundleAliases.forEach { alias ->
def aliases = catalogs.findBundle(alias).get().get()
bundle(alias, aliases.toString())
}
afterEvaluate {
projectsWithFlag('publish').each { proj ->
if (proj != catalogProject) {
String alias = proj.ext.artifactId
// Normalize the alias since Gradle prefixes `get` for '.' or '_' followed by a number.
// Replace "armeria-scala_2.12" with "armeria-scala_v2.12"
alias = alias.replaceAll('_(\\d)', '_v$1')
// Replace "armeria-scala_v2.12" with "armeria-scala_v212"
// or "armeria-thrift0.13" with "armeria-thrift013"
alias = alias.replaceAll('\\.(\\d)', '$1')
library(alias, proj.group, proj.ext.artifactId).version(proj.version)
}
}
}
}
}
}
configure(projectsWithFlags('java')) {
configurations.configureEach { configuration ->
configuration.dependencies.whenObjectAdded { dep ->
if (dep instanceof org.gradle.api.artifacts.ModuleDependency) {
rootProject.ext.exclusions["${dep.group}:${dep.name}"].each { list ->
list.each { exclude it }
}
}
}
}
configurations.create("dependencyManagement") {
visible = false
canBeConsumed = false
canBeResolved = false
}
configurations.configureEach {
["Classpath", "AnnotationProcessor", "kapt"].each { name ->
if (it.name.containsIgnoreCase(name)) {
it.extendsFrom(configurations.dependencyManagement)
}
}
}
dependencies {
dependencyManagement(platform(dependencyManagementProject))
}
// We need to use afterEvaluate because there is no way to guarantee configuration.canBeResolved and
// canBeConsumed are set otherwise.
afterEvaluate {
// From Boot 2.3.0, productionRuntimeClasspath is newly added and its canBeConsumed is set to true
// so change it to false. Otherwise, Gradle can't resolve the dependency in bootJar task
// because the version will not be added in the following `Add to resolvable configurations`.
// https://github.com/spring-projects/spring-boot/blob/v2.3.0.RELEASE/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java#L175-L178
def springBootGradlePlugin = plugins.findPlugin('org.springframework.boot')
def bootJarTask = tasks.findByName('bootJar')
def productionRuntimeClasspathConfig = configurations.findByName('productionRuntimeClasspath')
if (springBootGradlePlugin != null && bootJarTask != null && productionRuntimeClasspathConfig != null) {
productionRuntimeClasspathConfig.canBeConsumed(false)
}
}
}
// Create a new configuration called 'allDependencies'.
rootProject.configurations {
allDependencies {
visible = false
transitive = false
}
}
configure(rootProject) {
task managedVersions(
group: 'Build',
description: 'Generates the file that contains dependency versions.') {
def f = file("${project.buildDir}/managed_versions.yml")
outputs.file(f)
doLast {
f.parentFile.mkdir()
def managedVersions = [:]
def catalogs = project.extensions.getByType(VersionCatalogsExtension)
catalogs.catalogNames.forEach { name ->
def catalog = catalogs.named(name)
def versions = [:].withDefault { [] }
catalog.libraryAliases.forEach { alias ->
def library = catalog.findLibrary(alias).get().get()
versions["libraries"].add([alias: alias, module: library.toString()])
}
catalog.pluginAliases.forEach { alias ->
def plugin = catalog.findPlugin(alias).get().get()
versions["plugins"].add([alias: alias, plugin: plugin.toString()])
}
catalog.versionAliases.forEach { alias ->
def version = catalog.findVersion(alias).get()
versions["versions"].add([alias: alias, version: version.toString()])
}
catalog.bundleAliases.forEach { alias ->
def bundle = catalog.findBundle(alias).get().get()
versions["bundles"].add([alias: alias, bundle: bundle.toString()])
}
managedVersions[name] = versions
}
f.withWriter('UTF-8') {
new Yaml().dump(managedVersions, it)
}
}
}
}
static def findLibrary(Project rootProject, String alias) {
def catalogs = rootProject.extensions.getByType(VersionCatalogsExtension)
def catalog = catalogs.named("libs")
def library = catalog.findLibrary(alias)
if (library.isPresent()) {
return library.get()
} else {
return null
}
}
static def findPlugin(Project rootProject, String alias) {
def catalogs = rootProject.extensions.getByType(VersionCatalogsExtension)
def catalog = catalogs.named("libs")
def plugin = catalog.findPlugin(alias)
if (plugin.isPresent()) {
return plugin.get()
} else {
return null
}
}
/**
* Returns the managed versions that associates a module with a version.
* This method is added to support backward compatibility with 'dependencies.yml'
* Note that it is not recommended to use `managedVersions` with the module defined multiple times with different
* aliases. Because if a module is declared with different versions, the version returned by `managedVersions`
* is determined by how the version catalogs are indexed.
*/
static def getManagedVersions(Project rootProject) {
def managedVersions = [:]
def catalogs = rootProject.extensions.getByType(VersionCatalogsExtension)
catalogs.catalogNames.forEach { name ->
def catalog = catalogs.named(name)
catalog.libraryAliases.forEach {
def library = catalog.findLibrary(it).get().get()
def module = library.module
managedVersions["${module.group}:${module.name}"] = library.versionConstraint.requiredVersion
}
}
return managedVersions
}
final class GentlePlainTextReporter implements Reporter {
private final Reporter delegate
GentlePlainTextReporter(Project project, String revision, String gradleReleaseChannel) {
delegate = new PlainTextReporter(project, revision, gradleReleaseChannel)
}
void write(OutputStream os, Result result) {
PrintStream ps = (os instanceof PrintStream) ? (PrintStream) os
: new PrintStream(os, true, "UTF-8")
delegate.write(ps, updateProjectUrl(result))
}
private static Result updateProjectUrl(Result result) {
DependenciesGroup<DependencyOutdated> outdated = result.outdated
def updated = outdated.dependencies.collect { old ->
String quickSearchLink = "https://central.sonatype.com/artifact/${old.group}/${old.name}/versions\n"
String projectUrl
if (old.projectUrl == null) {
projectUrl = quickSearchLink
} else {
projectUrl = "${old.projectUrl}\n $quickSearchLink"
}
return new DependencyOutdated(old.group, old.name, old.version, projectUrl, old.userReason, old.available)
} as SortedSet
DependenciesGroup<DependencyOutdated> outdatedWithNote = new DependenciesGroup<>(outdated.count, updated)
return new Result(result.count, result.current, outdatedWithNote, result.exceeded, result.undeclared,
result.unresolved, result.gradle)
}
String getFileExtension() {
return delegate.getFileExtension()
}
}
/**
* A custom version of failOnVersionConflict which can limit which dependencies should be checked for conflict.
* Heavily inspired by https://github.com/gradle/gradle/issues/8813.
*/
static def failOnVersionConflict(Project project, ProviderConvertible<MinimalExternalModuleDependency> providerConvertible) {
return failOnVersionConflict(project, providerConvertible.asProvider())
}
static def failOnVersionConflict(Project project, Provider<MinimalExternalModuleDependency> dependencyProvider) {
if (!dependencyProvider.isPresent()) {
return
}
def targetDependency = dependencyProvider.get()
project.configurations.configureEach { config ->
incoming.afterResolve {
resolutionResult.allComponents {ResolvedComponentResult result ->
if (selectionReason.conflictResolution && moduleVersion != null) {
// we don't care if the selected version is the one specified in dependencies.toml
if (targetDependency.module == moduleVersion.module && targetDependency.version != moduleVersion.version) {
def msg = "Project '${project.name}:${config.name}' resolution failed " +
"for '${targetDependency.module}' with '${getSelectionReason()}"
if (project.rootProject.hasProperty('debugDeps')) {
project.logger.lifecycle(msg)
} else {
throw new IllegalStateException(msg)
}
}
}
}
}
}
}