Skip to content

Commit ef869e3

Browse files
committed
Implement support for the dependencyManagement concept as seen in Maven http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management
This allows users to enforce versions of transitive dependencies. Fix for GRAILS-11501
1 parent e654ef9 commit ef869e3

File tree

7 files changed

+202
-47
lines changed

7 files changed

+202
-47
lines changed

grails-aether/src/main/groovy/org/codehaus/groovy/grails/resolve/maven/aether/AetherDependencyManager.groovy

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class AetherDependencyManager implements DependencyManager {
110110
protected Dependency jvmAgent
111111
protected DependencyReport jvmAgentReport
112112
protected List<Dependency> dependencies = []
113+
protected List<Dependency> managedDependencies = []
113114
protected Set <org.codehaus.groovy.grails.resolve.Dependency> grailsPluginDependencies = []
114115
protected List<Dependency> buildDependencies = []
115116
protected Map<String, List<org.codehaus.groovy.grails.resolve.Dependency>> grailsDependenciesByScope = [:].withDefault { [] }
@@ -309,7 +310,7 @@ class AetherDependencyManager implements DependencyManager {
309310
*
310311
* @param callable The DSL definition
311312
*/
312-
void parseDependencies(Closure callable) {
313+
void parseDependencies(@DelegatesTo(AetherDsl) Closure callable) {
313314
AetherDsl dsl = new AetherDsl(this)
314315
dsl.session = session
315316
callable.delegate = dsl
@@ -515,29 +516,42 @@ class AetherDependencyManager implements DependencyManager {
515516
}
516517

517518
protected void manageDependencies(CollectRequest collectRequest) {
519+
def alreadyManagedArtefacts = managedDependencies.collect { Dependency d -> d.artifact }
520+
if(managedDependencies) {
521+
collectRequest.setManagedDependencies(managedDependencies)
522+
}
518523
if(coreDependencies) {
519524

520525
// ensure correct version of Spring is used
521526
for (springDep in ['spring-orm', 'spring-core', 'spring-tx', 'spring-context', 'spring-context-support', 'spring-bean', 'spring-web', 'spring-webmvc', 'spring-jms', 'spring-aop', 'spring-jdbc', 'spring-expression', 'spring-jdbc', 'spring-test']) {
522-
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.springframework:${springDep}:${coreDependencies.springVersion}"), null))
527+
String id = "org.springframework:${springDep}:${coreDependencies.springVersion}"
528+
registerManagedDependency(collectRequest, id, alreadyManagedArtefacts)
523529
}
524530
// ensure correct version of Groovy is used
525-
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.codehaus.groovy:groovy-all:${coreDependencies.groovyVersion}"), null))
526-
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.codehaus.groovy:groovy:${coreDependencies.groovyVersion}"), null))
531+
registerManagedDependency(collectRequest, "org.codehaus.groovy:groovy-all:${coreDependencies.groovyVersion}", alreadyManagedArtefacts)
532+
registerManagedDependency(collectRequest, "org.codehaus.groovy:groovy:${coreDependencies.groovyVersion}", alreadyManagedArtefacts)
527533

528534
// ensure the correct versions of Grails jars are used
529535
for (grailsDep in ['grails-core', 'grails-bootstrap', 'grails-web', 'grails-async', 'grails-test']) {
530-
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.grails:${grailsDep}:${coreDependencies.grailsVersion}"), null))
536+
String id = "org.grails:${grailsDep}:${coreDependencies.grailsVersion}"
537+
registerManagedDependency(collectRequest, id,alreadyManagedArtefacts)
531538
}
532539
for(org.codehaus.groovy.grails.resolve.Dependency d in coreDependencies.compileDependencies) {
533540
if(d.group == 'org.grails') {
534-
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact(d.pattern), null))
541+
registerManagedDependency(collectRequest, d.pattern,alreadyManagedArtefacts)
535542
}
536543
}
537544
}
538545

539546
}
540547

548+
private CollectRequest registerManagedDependency(CollectRequest collectRequest, String id, Collection<Artifact> alreadyManagedArtefacts) {
549+
def artifact = new DefaultArtifact(id)
550+
if(!alreadyManagedArtefacts.contains(artifact)) {
551+
collectRequest.addManagedDependency(new Dependency(artifact, null))
552+
}
553+
}
554+
541555
Proxy addProxy(String proxyHost, String proxyPort, String proxyUser, String proxyPass, String nonProxyHosts) {
542556
Proxy proxy
543557
if (proxyHost && proxyPort ) {
@@ -558,6 +572,16 @@ class AetherDependencyManager implements DependencyManager {
558572
return proxy
559573
}
560574

575+
/**
576+
* Adds a dependency to be used for dependency management. See http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management
577+
*
578+
* @param dependency The dependency to add
579+
* @param configuration The configuration
580+
*/
581+
void addManagedDependency(Dependency dependency) {
582+
managedDependencies << dependency
583+
}
584+
561585
void addDependency(Dependency dependency, DependencyConfiguration configuration = null) {
562586
org.codehaus.groovy.grails.resolve.Dependency grailsDependency = createGrailsDependency(dependency, configuration)
563587
grailsDependencies << grailsDependency

grails-aether/src/main/groovy/org/codehaus/groovy/grails/resolve/maven/aether/config/AetherDsl.groovy

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ class AetherDsl {
111111
void useOrigin(boolean b) {
112112
GrailsConsole.getInstance().warn("BuildConfig: Method [useOrigin] not supported by Aether dependency manager")
113113
}
114+
/**
115+
* Configures the checksum policy to either fail or ignore
116+
*
117+
* @param enable If enabled fail if checksums are invalid
118+
*/
114119
void checksums(boolean enable) {
115120
if (enable) {
116121
aetherDependencyManager.checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_FAIL
@@ -119,10 +124,21 @@ class AetherDsl {
119124
aetherDependencyManager.checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_IGNORE
120125
}
121126
}
122-
void checksums(String checksumConfig) {
123-
aetherDependencyManager.checksumPolicy = checksumConfig
127+
/**
128+
* Uses an explicit checksum policy.
129+
*
130+
* @param checksumPolicy The checksum policy
131+
* @see RepositoryPolicy
132+
*/
133+
void checksums(String checksumPolicy) {
134+
aetherDependencyManager.checksumPolicy = checksumPolicy
124135
}
125136

137+
/**
138+
* Configures the log level to use for Aether
139+
*
140+
* @param level The level, either "warn", "error", "info", "debug" or "verbose"
141+
*/
126142
void log(String level) {
127143
switch(level) {
128144
case "warn":
@@ -138,7 +154,13 @@ class AetherDsl {
138154
}
139155
}
140156

141-
void inherits(String name, Closure customizer = null) {
157+
/**
158+
* Whether to inherit dependenices from the framework or not
159+
*
160+
* @param name The named dependencies to inherit
161+
* @param customizer The customizer to use if excluding dependencies from the framework
162+
*/
163+
void inherits(String name, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
142164
final callable = aetherDependencyManager.inheritedDependencies[name]
143165

144166
if (callable) {
@@ -163,22 +185,49 @@ class AetherDsl {
163185
}
164186
}
165187

166-
void repositories(Closure callable) {
188+
/**
189+
* The repositories to configure
190+
*
191+
* @param callable A closure that defines the repositories
192+
*/
193+
void repositories(@DelegatesTo(RepositoriesConfiguration) Closure callable) {
167194
def rc = new RepositoriesConfiguration(aetherDependencyManager, session)
168195
callable.delegate = rc
169196
callable.call()
170197

171198
this.aetherDependencyManager.repositories.addAll(rc.repositories)
172199
}
173200

174-
void dependencies(Closure callable) {
201+
/**
202+
* Defines the dependencies of the project
203+
*
204+
* @param callable A closure that delegate to {@link DependenciesConfiguration}
205+
*/
206+
void dependencies(@DelegatesTo(DependenciesConfiguration) Closure callable) {
175207
def dc = new DependenciesConfiguration(aetherDependencyManager)
176208
dc.exclusionDependencySelector = exclusionDependencySelector
177209
callable.delegate = dc
178210
callable.call()
179211
}
180212

181-
void plugins(Closure callable) {
213+
/**
214+
* Defines the managed dependencies of the project. See http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management
215+
*
216+
* @param callable A closure that delegate to {@link DependenciesConfiguration}
217+
*/
218+
void management(@DelegatesTo(DependencyManagementConfiguration) Closure callable) {
219+
def dc = new DependencyManagementConfiguration(aetherDependencyManager)
220+
dc.exclusionDependencySelector = exclusionDependencySelector
221+
callable.delegate = dc
222+
callable.call()
223+
}
224+
225+
/**
226+
* Defines the plugin dependencies of the project
227+
*
228+
* @param callable A closure that delegate to {@link PluginConfiguration}
229+
*/
230+
void plugins(@DelegatesTo(PluginConfiguration) Closure callable) {
182231
def dc = new PluginConfiguration(aetherDependencyManager)
183232
callable.delegate = dc
184233
callable.call()

grails-aether/src/main/groovy/org/codehaus/groovy/grails/resolve/maven/aether/config/DependenciesConfiguration.groovy

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,28 @@ class DependenciesConfiguration {
5050
this.dependencyManager = dependencyManager
5151
}
5252

53-
void addDependency(Dependency dependency, Closure customizer = null) {
53+
void addDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
5454
if (exclusionDependencySelector == null || exclusionDependencySelector.selectDependency(dependency)) {
5555
final dependencyConfig = customizeDependency(customizer, dependency)
56-
dependency = dependencyConfig.dependency
57-
dependencyManager.addDependency dependencyConfig.dependency, dependencyConfig
56+
addDependencyToManager(dependencyConfig)
5857
}
5958
}
6059

61-
void addBuildDependency(Dependency dependency, Closure customizer = null) {
60+
protected addDependencyToManager(DependencyConfiguration dependencyConfig) {
61+
dependencyManager.addDependency dependencyConfig.dependency, dependencyConfig
62+
}
63+
64+
void addBuildDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
6265
if (exclusionDependencySelector == null || exclusionDependencySelector.selectDependency(dependency)) {
6366
final dependencyConfig = customizeDependency(customizer, dependency)
64-
dependencyManager.addBuildDependency dependencyConfig.dependency, dependencyConfig
67+
addBuildDependencyToManager(dependencyConfig)
6568
}
6669
}
6770

71+
protected addBuildDependencyToManager(DependencyConfiguration dependencyConfig) {
72+
dependencyManager.addBuildDependency dependencyConfig.dependency, dependencyConfig
73+
}
74+
6875
protected void addDependency(Map<String, String> properties, String scope, Closure customizer = null) {
6976
Dependency d = createDependencyForProperties(properties, scope)
7077
addDependency(d, customizer)
@@ -110,51 +117,51 @@ class DependenciesConfiguration {
110117
dependencyManager.setJvmAgent(new Dependency(new DefaultArtifact(pattern), SCOPE_COMPILE))
111118
}
112119

113-
void build(String pattern, Closure customizer = null) {
120+
void build(String pattern,@DelegatesTo(DependencyConfiguration) Closure customizer = null) {
114121
addBuildDependency new Dependency(new DefaultArtifact(pattern), SCOPE_COMPILE), customizer
115122
}
116123

117-
void build(Map<String, String> properties, Closure customizer = null) {
124+
void build(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
118125
addBuildDependency(properties, SCOPE_COMPILE, customizer)
119126
}
120127

121-
void compile(String pattern, Closure customizer = null) {
128+
void compile(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
122129
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_COMPILE), customizer
123130
}
124131

125-
void compile(Map<String, String> properties, Closure customizer = null) {
132+
void compile(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
126133
addDependency(properties, SCOPE_COMPILE, customizer)
127134
}
128135

129-
void runtime(String pattern, Closure customizer = null) {
136+
void runtime(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
130137
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_RUNTIME), customizer
131138
}
132139

133-
void runtime(Map<String, String> properties, Closure customizer = null) {
140+
void runtime(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
134141
addDependency properties, SCOPE_RUNTIME, customizer
135142
}
136143

137-
void provided(String pattern, Closure customizer = null) {
144+
void provided(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
138145
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_PROVIDED), customizer
139146
}
140147

141-
void provided(Map<String, String> properties, Closure customizer = null) {
148+
void provided(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
142149
addDependency properties, SCOPE_PROVIDED, customizer
143150
}
144151

145-
void optional(String pattern, Closure customizer = null) {
152+
void optional(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
146153
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_OPTIONAL), customizer
147154
}
148155

149-
void optional(Map<String, String> properties, Closure customizer = null) {
156+
void optional(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
150157
addDependency properties, SCOPE_OPTIONAL, customizer
151158
}
152159

153-
void test(String pattern, Closure customizer = null) {
160+
void test(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
154161
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_TEST), customizer
155162
}
156163

157-
void test(Map<String, String> properties, Closure customizer = null) {
164+
void test(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
158165
addDependency properties, SCOPE_TEST, customizer
159166
}
160167

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2014 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+
* http://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+
package org.codehaus.groovy.grails.resolve.maven.aether.config
17+
18+
import org.codehaus.groovy.grails.resolve.maven.aether.AetherDependencyManager
19+
import org.eclipse.aether.artifact.DefaultArtifact
20+
import org.eclipse.aether.graph.Dependency
21+
22+
/**
23+
* Allows configuration of dependency management.
24+
*
25+
* @since 2.4.1
26+
* @author Graeme Rocher
27+
*/
28+
class DependencyManagementConfiguration extends DependenciesConfiguration {
29+
DependencyManagementConfiguration(AetherDependencyManager dependencyManager) {
30+
super(dependencyManager)
31+
}
32+
33+
@Override
34+
protected addDependencyToManager(DependencyConfiguration dependencyConfig) {
35+
dependencyManager.addManagedDependency(dependencyConfig.dependency)
36+
}
37+
38+
@Override
39+
protected addBuildDependencyToManager(DependencyConfiguration dependencyConfig) {
40+
dependencyManager.addManagedDependency(dependencyConfig.dependency)
41+
}
42+
43+
void dependency(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
44+
addDependency new Dependency(new DefaultArtifact(pattern), null), customizer
45+
}
46+
47+
void dependency(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
48+
addDependency(properties, null, customizer)
49+
}
50+
51+
52+
}

grails-aether/src/main/groovy/org/codehaus/groovy/grails/resolve/maven/aether/config/PluginConfiguration.groovy

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,42 @@ class PluginConfiguration extends DependenciesConfiguration {
3232
}
3333

3434
@Override
35-
void addDependency(Dependency dependency, Closure customizer) {
35+
void addDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer) {
3636
super.addDependency(dependency, customizer)
3737
}
3838

3939
@Override
40-
void addBuildDependency(Dependency dependency, Closure customizer) {
40+
void addBuildDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer) {
4141
super.addBuildDependency(dependency, customizer)
4242
}
4343

4444
@Override
45-
void build(String pattern, Closure customizer) {
45+
void build(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
4646
super.build(extractDependencyProperties(pattern), customizer)
4747
}
4848

4949
@Override
50-
void compile(String pattern, Closure customizer) {
50+
void compile(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
5151
super.compile(extractDependencyProperties(pattern), customizer)
5252
}
5353

5454
@Override
55-
void runtime(String pattern, Closure customizer) {
55+
void runtime(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
5656
super.runtime(extractDependencyProperties(pattern), customizer)
5757
}
5858

5959
@Override
60-
void provided(String pattern, Closure customizer) {
60+
void provided(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
6161
super.provided(extractDependencyProperties(pattern), customizer)
6262
}
6363

6464
@Override
65-
void optional(String pattern, Closure customizer) {
65+
void optional(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
6666
super.optional(extractDependencyProperties(pattern), customizer)
6767
}
6868

6969
@Override
70-
void test(String pattern, Closure customizer) {
70+
void test(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
7171
super.test(extractDependencyProperties(pattern), customizer)
7272
}
7373

0 commit comments

Comments
 (0)