Gradle dependencyManagement

时间:2013-06-19 12:37:33

标签: maven groovy gradle

我正在将Maven项目迁移到Gradle。我需要管理依赖项,因此尝试使用resolutionStrategy:

    def dependencyVersions = [
                'org.slf4j:slf4j-api' : '1.7.2', 
                'javax.inject:javax.inject' : '1',
                'com.google.code.findbugs:annotations' : '2.0.1',
                'com.typesafe:config' : '1.0.0',
                'ch.qos.logback:logback-classic' : '1.0.9', 
                'com.google.guava:guava' : '14.0',
                'com.google.inject:guice' : '3.0',
                'com.google.inject.extensions:guice-multibindings' : '3.0',
                'com.google.code.gson:gson' : '2.2.2',
                'joda-time:joda-time' : '2.1',
                'com.thoughtworks.paranamer:paranamer' : '2.5.2',
                'org.codehaus.groovy:groovy-all' : '2.0.6',
                'commons-validator:commons-validator': '1.4.0',
                'org.apache.shiro:shiro-core' : '1.2.1',
                'junit:junit-dep' : '4.10',
                'org.mockito:mockito-core' : '1.9.5',
                'org.hamcrest:hamcrest-core': '1.3',
                'org.hamcrest:hamcrest-library': '1.3',
                'org.unitils:unitils-core': '3.3'
             ]

configurations.all {
    resolutionStrategy {
        eachDependency { DependencyResolveDetails details ->  
        def version = dependencyVersions["$details.requested.group:$details.requested.name"]
        if (version != null)
            details.useVersion version
        }
    }
}

但现在当我尝试Gradle安装(进入本地Maven存储库)时,我收到此错误:

Execution failed for task ':counter-module:install'.
  

无法发布配置'档案'   无法初始化POM pom-default.xml:无法在/home/workspace/counter/counter-module/build/poms/pom-default.xml验证项目lt.counter的POM

2 个答案:

答案 0 :(得分:4)

我仍然可能错过了您问题的一个方面,但我只是注意到了文档中的内容。

// force certain versions of dependencies (including transitive)
//  *append new forced modules:
force 'asm:asm-all:3.3.1', 'commons-io:commons-io:1.4'
//  *replace existing forced modules with new ones:
forcedModules = ['asm:asm-all:3.3.1']

您似乎可以执行以下操作:

def dependencyVersions = [
            'org.slf4j:slf4j-api' : '1.7.2', 
            'javax.inject:javax.inject' : '1',
            'com.google.code.findbugs:annotations' : '2.0.1',
            'com.typesafe:config' : '1.0.0',
            'ch.qos.logback:logback-classic' : '1.0.9', 
            'com.google.guava:guava' : '14.0',
            'com.google.inject:guice' : '3.0',
            'com.google.inject.extensions:guice-multibindings' : '3.0',
            'com.google.code.gson:gson' : '2.2.2',
            'joda-time:joda-time' : '2.1',
            'com.thoughtworks.paranamer:paranamer' : '2.5.2',
            'org.codehaus.groovy:groovy-all' : '2.0.6',
            'commons-validator:commons-validator': '1.4.0',
            'org.apache.shiro:shiro-core' : '1.2.1',
            'junit:junit-dep' : '4.10',
            'org.mockito:mockito-core' : '1.9.5',
            'org.hamcrest:hamcrest-core': '1.3',
            'org.hamcrest:hamcrest-library': '1.3',
            'org.unitils:unitils-core': '3.3'
         ]

force dependencyVersion.collect {k, v -> "$k:$v"}

在我眼里,看起来这将完成两个原则。

  1. 为用户提供一个很好的地图符号,以便他们想要玩得很好并添加一个带有预定版本的dep。
  2. 强制他们在任何时候试图变得棘手时使用预定版本。

答案 1 :(得分:0)

Gradle的默认解析策略是使用最新版本,因此将使用版本N;版本N-1不会。

您没有告诉我们您使用的Gradle版本以及项目的完整结构。你在做多项目构建吗?

另外,我不明白你的自定义解析策略 - 为什么版本会为null?

- 编辑 -

最新版本是默认的解析策略,因此将使用遇到的最高版本。

或许查看Gradle's examples on custom resolution strategies,例如强制使用特定版本。

相关问题