Gradle dependencyManagement

Iow*_*owA 6 groovy gradle maven

我正在将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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Execution failed for task ':counter-module:install'.
Run Code Online (Sandbox Code Playgroud)

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

Jos*_*non 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']
Run Code Online (Sandbox Code Playgroud)

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

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"}
Run Code Online (Sandbox Code Playgroud)

在我看来,这似乎可以实现两个原则。

  1. 为用户提供一个漂亮的地图符号,供他们在想要玩得开心时使用,并使用您预先确定的版本添加依赖项。
  2. 每当他们试图变得棘手时,强迫他们使用预先确定的版本。