Skip to content

Commit

Permalink
Implement support for the dependencyManagement concept as seen in Maven
Browse files Browse the repository at this point in the history
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
  • Loading branch information
graemerocher committed Jun 13, 2014
1 parent e654ef9 commit ef869e3
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 47 deletions.
Expand Up @@ -110,6 +110,7 @@ class AetherDependencyManager implements DependencyManager {
protected Dependency jvmAgent
protected DependencyReport jvmAgentReport
protected List<Dependency> dependencies = []
protected List<Dependency> managedDependencies = []
protected Set <org.codehaus.groovy.grails.resolve.Dependency> grailsPluginDependencies = []
protected List<Dependency> buildDependencies = []
protected Map<String, List<org.codehaus.groovy.grails.resolve.Dependency>> grailsDependenciesByScope = [:].withDefault { [] }
Expand Down Expand Up @@ -309,7 +310,7 @@ class AetherDependencyManager implements DependencyManager {
*
* @param callable The DSL definition
*/
void parseDependencies(Closure callable) {
void parseDependencies(@DelegatesTo(AetherDsl) Closure callable) {
AetherDsl dsl = new AetherDsl(this)
dsl.session = session
callable.delegate = dsl
Expand Down Expand Up @@ -515,29 +516,42 @@ class AetherDependencyManager implements DependencyManager {
}

protected void manageDependencies(CollectRequest collectRequest) {
def alreadyManagedArtefacts = managedDependencies.collect { Dependency d -> d.artifact }
if(managedDependencies) {
collectRequest.setManagedDependencies(managedDependencies)
}
if(coreDependencies) {

// ensure correct version of Spring is used
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']) {
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.springframework:${springDep}:${coreDependencies.springVersion}"), null))
String id = "org.springframework:${springDep}:${coreDependencies.springVersion}"
registerManagedDependency(collectRequest, id, alreadyManagedArtefacts)
}
// ensure correct version of Groovy is used
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.codehaus.groovy:groovy-all:${coreDependencies.groovyVersion}"), null))
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.codehaus.groovy:groovy:${coreDependencies.groovyVersion}"), null))
registerManagedDependency(collectRequest, "org.codehaus.groovy:groovy-all:${coreDependencies.groovyVersion}", alreadyManagedArtefacts)
registerManagedDependency(collectRequest, "org.codehaus.groovy:groovy:${coreDependencies.groovyVersion}", alreadyManagedArtefacts)

// ensure the correct versions of Grails jars are used
for (grailsDep in ['grails-core', 'grails-bootstrap', 'grails-web', 'grails-async', 'grails-test']) {
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact("org.grails:${grailsDep}:${coreDependencies.grailsVersion}"), null))
String id = "org.grails:${grailsDep}:${coreDependencies.grailsVersion}"
registerManagedDependency(collectRequest, id,alreadyManagedArtefacts)
}
for(org.codehaus.groovy.grails.resolve.Dependency d in coreDependencies.compileDependencies) {
if(d.group == 'org.grails') {
collectRequest.addManagedDependency(new Dependency(new DefaultArtifact(d.pattern), null))
registerManagedDependency(collectRequest, d.pattern,alreadyManagedArtefacts)
}
}
}

}

private CollectRequest registerManagedDependency(CollectRequest collectRequest, String id, Collection<Artifact> alreadyManagedArtefacts) {
def artifact = new DefaultArtifact(id)
if(!alreadyManagedArtefacts.contains(artifact)) {
collectRequest.addManagedDependency(new Dependency(artifact, null))
}
}

Proxy addProxy(String proxyHost, String proxyPort, String proxyUser, String proxyPass, String nonProxyHosts) {
Proxy proxy
if (proxyHost && proxyPort ) {
Expand All @@ -558,6 +572,16 @@ class AetherDependencyManager implements DependencyManager {
return proxy
}

/**
* Adds a dependency to be used for dependency management. See http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management
*
* @param dependency The dependency to add
* @param configuration The configuration
*/
void addManagedDependency(Dependency dependency) {
managedDependencies << dependency
}

void addDependency(Dependency dependency, DependencyConfiguration configuration = null) {
org.codehaus.groovy.grails.resolve.Dependency grailsDependency = createGrailsDependency(dependency, configuration)
grailsDependencies << grailsDependency
Expand Down
Expand Up @@ -111,6 +111,11 @@ class AetherDsl {
void useOrigin(boolean b) {
GrailsConsole.getInstance().warn("BuildConfig: Method [useOrigin] not supported by Aether dependency manager")
}
/**
* Configures the checksum policy to either fail or ignore
*
* @param enable If enabled fail if checksums are invalid
*/
void checksums(boolean enable) {
if (enable) {
aetherDependencyManager.checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_FAIL
Expand All @@ -119,10 +124,21 @@ class AetherDsl {
aetherDependencyManager.checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_IGNORE
}
}
void checksums(String checksumConfig) {
aetherDependencyManager.checksumPolicy = checksumConfig
/**
* Uses an explicit checksum policy.
*
* @param checksumPolicy The checksum policy
* @see RepositoryPolicy
*/
void checksums(String checksumPolicy) {
aetherDependencyManager.checksumPolicy = checksumPolicy
}

/**
* Configures the log level to use for Aether
*
* @param level The level, either "warn", "error", "info", "debug" or "verbose"
*/
void log(String level) {
switch(level) {
case "warn":
Expand All @@ -138,7 +154,13 @@ class AetherDsl {
}
}

void inherits(String name, Closure customizer = null) {
/**
* Whether to inherit dependenices from the framework or not
*
* @param name The named dependencies to inherit
* @param customizer The customizer to use if excluding dependencies from the framework
*/
void inherits(String name, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
final callable = aetherDependencyManager.inheritedDependencies[name]

if (callable) {
Expand All @@ -163,22 +185,49 @@ class AetherDsl {
}
}

void repositories(Closure callable) {
/**
* The repositories to configure
*
* @param callable A closure that defines the repositories
*/
void repositories(@DelegatesTo(RepositoriesConfiguration) Closure callable) {
def rc = new RepositoriesConfiguration(aetherDependencyManager, session)
callable.delegate = rc
callable.call()

this.aetherDependencyManager.repositories.addAll(rc.repositories)
}

void dependencies(Closure callable) {
/**
* Defines the dependencies of the project
*
* @param callable A closure that delegate to {@link DependenciesConfiguration}
*/
void dependencies(@DelegatesTo(DependenciesConfiguration) Closure callable) {
def dc = new DependenciesConfiguration(aetherDependencyManager)
dc.exclusionDependencySelector = exclusionDependencySelector
callable.delegate = dc
callable.call()
}

void plugins(Closure callable) {
/**
* Defines the managed dependencies of the project. See http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management
*
* @param callable A closure that delegate to {@link DependenciesConfiguration}
*/
void management(@DelegatesTo(DependencyManagementConfiguration) Closure callable) {
def dc = new DependencyManagementConfiguration(aetherDependencyManager)
dc.exclusionDependencySelector = exclusionDependencySelector
callable.delegate = dc
callable.call()
}

/**
* Defines the plugin dependencies of the project
*
* @param callable A closure that delegate to {@link PluginConfiguration}
*/
void plugins(@DelegatesTo(PluginConfiguration) Closure callable) {
def dc = new PluginConfiguration(aetherDependencyManager)
callable.delegate = dc
callable.call()
Expand Down
Expand Up @@ -50,21 +50,28 @@ class DependenciesConfiguration {
this.dependencyManager = dependencyManager
}

void addDependency(Dependency dependency, Closure customizer = null) {
void addDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
if (exclusionDependencySelector == null || exclusionDependencySelector.selectDependency(dependency)) {
final dependencyConfig = customizeDependency(customizer, dependency)
dependency = dependencyConfig.dependency
dependencyManager.addDependency dependencyConfig.dependency, dependencyConfig
addDependencyToManager(dependencyConfig)
}
}

void addBuildDependency(Dependency dependency, Closure customizer = null) {
protected addDependencyToManager(DependencyConfiguration dependencyConfig) {
dependencyManager.addDependency dependencyConfig.dependency, dependencyConfig
}

void addBuildDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
if (exclusionDependencySelector == null || exclusionDependencySelector.selectDependency(dependency)) {
final dependencyConfig = customizeDependency(customizer, dependency)
dependencyManager.addBuildDependency dependencyConfig.dependency, dependencyConfig
addBuildDependencyToManager(dependencyConfig)
}
}

protected addBuildDependencyToManager(DependencyConfiguration dependencyConfig) {
dependencyManager.addBuildDependency dependencyConfig.dependency, dependencyConfig
}

protected void addDependency(Map<String, String> properties, String scope, Closure customizer = null) {
Dependency d = createDependencyForProperties(properties, scope)
addDependency(d, customizer)
Expand Down Expand Up @@ -110,51 +117,51 @@ class DependenciesConfiguration {
dependencyManager.setJvmAgent(new Dependency(new DefaultArtifact(pattern), SCOPE_COMPILE))
}

void build(String pattern, Closure customizer = null) {
void build(String pattern,@DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addBuildDependency new Dependency(new DefaultArtifact(pattern), SCOPE_COMPILE), customizer
}

void build(Map<String, String> properties, Closure customizer = null) {
void build(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addBuildDependency(properties, SCOPE_COMPILE, customizer)
}

void compile(String pattern, Closure customizer = null) {
void compile(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_COMPILE), customizer
}

void compile(Map<String, String> properties, Closure customizer = null) {
void compile(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency(properties, SCOPE_COMPILE, customizer)
}

void runtime(String pattern, Closure customizer = null) {
void runtime(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_RUNTIME), customizer
}

void runtime(Map<String, String> properties, Closure customizer = null) {
void runtime(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency properties, SCOPE_RUNTIME, customizer
}

void provided(String pattern, Closure customizer = null) {
void provided(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_PROVIDED), customizer
}

void provided(Map<String, String> properties, Closure customizer = null) {
void provided(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency properties, SCOPE_PROVIDED, customizer
}

void optional(String pattern, Closure customizer = null) {
void optional(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_OPTIONAL), customizer
}

void optional(Map<String, String> properties, Closure customizer = null) {
void optional(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency properties, SCOPE_OPTIONAL, customizer
}

void test(String pattern, Closure customizer = null) {
void test(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency new Dependency(new DefaultArtifact(pattern), SCOPE_TEST), customizer
}

void test(Map<String, String> properties, Closure customizer = null) {
void test(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency properties, SCOPE_TEST, customizer
}

Expand Down
@@ -0,0 +1,52 @@
/*
* Copyright 2014 the original author or authors.
*
* 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 org.codehaus.groovy.grails.resolve.maven.aether.config

import org.codehaus.groovy.grails.resolve.maven.aether.AetherDependencyManager
import org.eclipse.aether.artifact.DefaultArtifact
import org.eclipse.aether.graph.Dependency

/**
* Allows configuration of dependency management.
*
* @since 2.4.1
* @author Graeme Rocher
*/
class DependencyManagementConfiguration extends DependenciesConfiguration {
DependencyManagementConfiguration(AetherDependencyManager dependencyManager) {
super(dependencyManager)
}

@Override
protected addDependencyToManager(DependencyConfiguration dependencyConfig) {
dependencyManager.addManagedDependency(dependencyConfig.dependency)
}

@Override
protected addBuildDependencyToManager(DependencyConfiguration dependencyConfig) {
dependencyManager.addManagedDependency(dependencyConfig.dependency)
}

void dependency(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency new Dependency(new DefaultArtifact(pattern), null), customizer
}

void dependency(Map<String, String> properties, @DelegatesTo(DependencyConfiguration) Closure customizer = null) {
addDependency(properties, null, customizer)
}


}
Expand Up @@ -32,42 +32,42 @@ class PluginConfiguration extends DependenciesConfiguration {
}

@Override
void addDependency(Dependency dependency, Closure customizer) {
void addDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.addDependency(dependency, customizer)
}

@Override
void addBuildDependency(Dependency dependency, Closure customizer) {
void addBuildDependency(Dependency dependency, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.addBuildDependency(dependency, customizer)
}

@Override
void build(String pattern, Closure customizer) {
void build(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.build(extractDependencyProperties(pattern), customizer)
}

@Override
void compile(String pattern, Closure customizer) {
void compile(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.compile(extractDependencyProperties(pattern), customizer)
}

@Override
void runtime(String pattern, Closure customizer) {
void runtime(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.runtime(extractDependencyProperties(pattern), customizer)
}

@Override
void provided(String pattern, Closure customizer) {
void provided(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.provided(extractDependencyProperties(pattern), customizer)
}

@Override
void optional(String pattern, Closure customizer) {
void optional(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.optional(extractDependencyProperties(pattern), customizer)
}

@Override
void test(String pattern, Closure customizer) {
void test(String pattern, @DelegatesTo(DependencyConfiguration) Closure customizer) {
super.test(extractDependencyProperties(pattern), customizer)
}

Expand Down

0 comments on commit ef869e3

Please sign in to comment.