-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathGenerateLockTask.groovy
297 lines (254 loc) · 15 KB
/
GenerateLockTask.groovy
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
/*
* Copyright 2014-2019 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nebula.plugin.dependencylock.tasks
import nebula.plugin.dependencylock.DependencyLockExtension
import nebula.plugin.dependencylock.DependencyLockTaskConfigurer
import nebula.plugin.dependencylock.DependencyLockWriter
import nebula.plugin.dependencylock.exceptions.DependencyLockException
import nebula.plugin.dependencylock.model.LockKey
import nebula.plugin.dependencylock.model.LockValue
import nebula.plugin.dependencylock.utils.ConfigurationFilters
import nebula.plugin.dependencylock.utils.DependencyLockingFeatureFlags
import org.gradle.api.BuildCancelledException
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.internal.deprecation.DeprecationLogger
import org.gradle.work.DisableCachingByDefault
@DisableCachingByDefault
class GenerateLockTask extends AbstractLockTask {
private static final Logger LOGGER = Logging.getLogger(GenerateLockTask)
@Internal
String description = 'Create a lock file in build/<configured name>'
@Internal
Collection<Configuration> configurations = []
@Internal
Set<String> configurationNames
@Internal
Set<String> skippedConfigurationNames
@Internal
Closure filter = { group, name, version -> true }
@Input
@Optional
Set<String> skippedDependencies = []
@Internal
File dependenciesLock
@Internal
Map<String, String> overrides
@Input
@Optional
Boolean includeTransitives = false
@TaskAction
void lock() {
//TODO: address Invocation of Task.project at execution time has been deprecated.
DeprecationLogger.whileDisabled {
final String WRITE_CORE_LOCK_TASK_TO_RUN = "`./gradlew dependencies --write-locks`"
final String MIGRATE_TO_CORE_LOCK_TASK_NAME = "migrateToCoreLocks"
if (DependencyLockingFeatureFlags.isCoreLockingEnabled()) {
def dependencyLockExtension = project.extensions.findByType(DependencyLockExtension)
def globalLockFile = new File(project.projectDir, dependencyLockExtension.globalLockFile)
if (globalLockFile.exists()) {
throw new BuildCancelledException("Legacy global locks are not supported with core locking.\n" +
"Please remove global locks.\n" +
" - Global locks: ${globalLockFile.absolutePath}")
}
throw new BuildCancelledException("generateLock is not supported with core locking.\n" +
"Please use $WRITE_CORE_LOCK_TASK_TO_RUN\n" +
"or do a one-time migration with `./gradlew $MIGRATE_TO_CORE_LOCK_TASK_NAME` to preserve the current lock state")
}
if (DependencyLockTaskConfigurer.shouldIgnoreDependencyLock(project)) {
throw new DependencyLockException("Dependency locks cannot be generated. The plugin is disabled for this project (dependencyLock.ignore is set to true)")
}
Collection<Configuration> confs = getConfigurations() ?: lockableConfigurations(project, project, getConfigurationNames(), getSkippedConfigurationNames())
Map dependencyMap = new GenerateLockFromConfigurations().lock(confs)
new DependencyLockWriter(getDependenciesLock(), getSkippedDependencies()).writeLock(dependencyMap)
}
}
static Collection<Configuration> lockableConfigurations(Project taskProject, Project project, Set<String> configurationNames, Set<String> skippedConfigurationNamesPrefixes = []) {
Set<Configuration> lockableConfigurations = []
if (configurationNames.empty) {
if (Configuration.class.declaredMethods.any { it.name == 'isCanBeResolved' }) {
lockableConfigurations.addAll project.configurations.findAll {
it.canBeResolved && !ConfigurationFilters.safelyHasAResolutionAlternative(it) &&
// Always exclude compileOnly and build tools configurations to avoid issues with kotlin plugin
!it.name.endsWith("CompileOnly") &&
it.name != "compileOnly" &&
it.name != "kotlinBuildToolsApiClasspath"
}
} else {
lockableConfigurations.addAll project.configurations.asList()
}
} else {
lockableConfigurations.addAll configurationNames.collect { project.configurations.getByName(it) }
}
lockableConfigurations.removeAll {
Configuration configuration -> skippedConfigurationNamesPrefixes.any {
String prefix -> configuration.name.startsWith(prefix)
}
}
return lockableConfigurations
}
static Collection<Configuration> filterNonLockableConfigurationsAndProvideWarningsForGlobalLockSubproject(Project subproject, Set<String> configurationNames, Collection<Configuration> lockableConfigurations) {
if (configurationNames.size() > 0) {
Collection<String> warnings = new HashSet<>()
Collection<Configuration> consumableLockableConfigurations = new ArrayList<>()
lockableConfigurations.each { conf ->
Collection<String> warningsForConfiguration = provideWarningsForConfiguration(conf, subproject)
warnings.addAll(warningsForConfiguration)
if (warningsForConfiguration.isEmpty()) {
consumableLockableConfigurations.add(conf)
}
}
configurationNames.each { nameToLock ->
if (!lockableConfigurations.collect { it.name }.contains(nameToLock)) {
Configuration confThatWillNotBeLocked = subproject.configurations.findByName(nameToLock)
if (confThatWillNotBeLocked == null) {
String message = "Global lock warning: project '${subproject.name}' requested locking a configuration which cannot be locked: '${nameToLock}'"
warnings.add(message)
} else {
warnings.addAll(provideWarningsForConfiguration(confThatWillNotBeLocked, subproject))
}
}
}
if (warnings.size() > 0) {
warnings.add("Requested configurations for global locks must be resolvable, consumable, and without resolution alternatives.\n" +
"You can remove the configuration 'dependencyLock.configurationNames' to stop this customization.\n" +
"If you wish to lock only specific configurations, please update 'dependencyLock.configurationNames' with other configurations.\n" +
"Please read more about this at:\n" +
"- https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management\n" +
"- https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph")
LOGGER.warn('--------------------\n' + warnings.sort().join("\n") + '\n--------------------')
}
return consumableLockableConfigurations
}
return lockableConfigurations
}
private static Collection<String> provideWarningsForConfiguration(Configuration conf, Project subproject) {
Collection<String> errorMessages = new HashSet<>()
if (!ConfigurationFilters.canSafelyBeConsumed(conf)) {
String message = "Global lock warning: project '${subproject.name}' requested locking a configuration which cannot be consumed: '${conf.name}'"
errorMessages.add(message)
}
if (!ConfigurationFilters.canSafelyBeResolved(conf)) {
String message = "Global lock warning: project '${subproject.name}' requested locking a configuration which cannot be resolved: '${conf.name}'"
errorMessages.add(message)
}
if (ConfigurationFilters.safelyHasAResolutionAlternative(conf)) {
String message = "Global lock warning: project '${subproject.name}' requested locking a deprecated configuration '${conf.name}' " +
"which has resolution alternatives: ${conf.getResolutionAlternatives()}"
errorMessages.add(message)
}
return errorMessages
}
class GenerateLockFromConfigurations {
Map<LockKey, LockValue> lock(Collection<Configuration> confs) {
Map<LockKey, LockValue> deps = [:].withDefault { new LockValue() }
// Peers are all the projects in the build to which this plugin has been applied.
def peers = project.rootProject.allprojects.collect { new LockKey(group: it.group, artifact: it.name) }
confs.each { Configuration configuration ->
// Lock the version of each dependency specified in the build script as resolved by Gradle.
def resolvedDependencies = configuration.resolvedConfiguration.firstLevelModuleDependencies
def filteredResolvedDependencies = resolvedDependencies.findAll { ResolvedDependency resolved ->
filter(resolved.moduleGroup, resolved.moduleName, resolved.moduleVersion)
}
filteredResolvedDependencies.each { ResolvedDependency resolved ->
def key = new LockKey(group: resolved.moduleGroup, artifact: resolved.moduleName, configuration: configuration.name)
// If this dependency does not exist in our list of peers, it is a standard dependency. Otherwise, it is
// a project dependency.
if (!isKeyInPeerList(key, peers)) {
deps[key].locked = resolved.moduleVersion
} else {
// Project dependencies don't have a version so they must be treated differently. Record the project
// as an explicit dependency, but do not lock it to a version.
deps[key].project = true
// If we don't include transitive dependencies, then we must lock the first-level "transitive"
// dependencies of each project dependency.
if (!getIncludeTransitives()) {
handleSiblingTransitives(resolved, configuration.name, deps, peers)
}
}
// If requested, lock all the transitive dependencies of the declared top-level dependencies.
if (getIncludeTransitives()) {
deps[key].childrenVisited = true
resolved.children.each { handleTransitive(it, configuration.name, deps, peers, key) }
}
}
}
// Add all the overrides to the locked dependencies and record whether a specified override modified a
// preexisting dependency.
getOverrides().each { String k, String overrideVersion ->
def (overrideGroup, overrideArtifact) = k.tokenize(':')
deps.each { depLockKey, depValue ->
if (depLockKey.group == overrideGroup && depLockKey.artifact == overrideArtifact) {
depValue.viaOverride = overrideVersion
}
}
}
return deps
}
private void handleSiblingTransitives(ResolvedDependency sibling, String configName, Map<LockKey, LockValue> deps, List peers) {
def parent = new LockKey(group: sibling.moduleGroup, artifact: sibling.moduleName, configuration: sibling.configuration)
sibling.children.each { ResolvedDependency dependency ->
def key = new LockKey(group: dependency.moduleGroup, artifact: dependency.moduleName, configuration: configName)
// Record the project[s] from which this dependency originated.
deps[key].firstLevelTransitive << parent
// Lock the transitive dependencies of each project dependency, recursively.
if (isKeyInPeerList(key, peers)) {
deps[key].project = true
// Multiple configurations may specify dependencies on the same project, and multiple projects might
// also be dependent on the same project. We only need to record the top-level transitive dependencies
// once for each project. Flag a project as visited as soon as we encounter it.
if ((dependency.children.size() > 0) && !deps[key].childrenVisited) {
deps[key].childrenVisited = true
handleSiblingTransitives(dependency, configName, deps, peers)
}
} else {
deps[key].locked = dependency.moduleVersion
}
}
}
private void handleTransitive(ResolvedDependency transitive, String configName, Map<LockKey, LockValue> deps, List peers, LockKey parent) {
def key = new LockKey(group: transitive.moduleGroup, artifact: transitive.moduleName, configuration: configName)
// Multiple dependencies may share any subset of their transitive dependencies. Each dependency only needs to be
// visited once so flag it once we visit it.
if (!deps[key].childrenVisited) {
// Lock each dependency and its children, recursively. Don't forget transitive project dependencies.
if (!isKeyInPeerList(key, peers)) {
deps[key].locked = transitive.moduleVersion
} else {
deps[key].project = true
}
if (transitive.children.size() > 0) {
deps[key].childrenVisited = true
}
transitive.children.each { handleTransitive(it, configName, deps, peers, key) }
}
// Record the dependencies from which this artifact originated transitively.
deps[key].transitive << parent
}
private static def isKeyInPeerList(LockKey lockKey, List peers) {
return peers.any {
it.group == lockKey.group && it.artifact == lockKey.artifact
}
}
}
}