1

在我的 gradle 依赖项中,我希望能够使用“默认”版本并自动映射到特定版本的库。

例如,我的 build.gradle 中可能有以下内容:

dependencies {
    compile (group: 'org.hibernate', module: 'hibernate-core', version 'default')
}

在分辨率上,我希望这给我相当于

dependencies {
    compile (group: 'org.hibernate', module: 'hibernate-core', version '4.3.0.Beta4')
}

映射在一个文件中指定,该文件包含类似的键/值对

org.hibernate.hibernate-core=4.3.0.Beta4

它被读入项目中名为 versionMap 的 HashMap 类型的扩展属性。

我可以使用 build.gradle 文件中的以下内容让这个映射非常愉快地工作

configurations {
    all {
        resolutionStrategy {
            eachDependency { DependencyResolveDetails details ->
                if (details.requested.getVersion() == "default") {
                    def key = details.requested.getGroup() + "." + details.requested.getName() + ".version.default"
                    details.useVersion(versionMap.get(key))
                }
            }
        }
    }
}

(我需要感谢 Peter Niederwieser 的帮助让我走到了这一步)

由于我在我的许多项目中都需要相同的东西,所以我想把它做成一个插件,所以我创建了一个插件项目,它都构建得很好,但是当我尝试运行它时,我无法访问 resolutionStrategy 来设置逻辑。我的插件代码如下所示:

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.DependencyResolveDetails;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.ResolutionStrategy;

class DefaultVersionsPlugin implements Plugin<Project> {

    @Override
    public void apply(Project project) {

        def strategy = 
            { DependencyResolveDetails details ->
                if (details.requested.getVersion() == "default") {
                    def key = details.requested.getGroup() + "." + details.requested.getName()
                    details.useVersion(ext.versionMap.get(key))
                }
            }

        ConfigurationContainer configurations = project.getConfigurations();
        configurations.each { config ->
            println "config: " + config + " - " + config.resolutionStrategy
            config.resoultionStrategy.eachDependency{ strategy }
        }
    }
}

上面的 println 给出了有效的输出,但下面的行给出了错误。运行这个插件的输出是。

config: configuration ':alerting:archives' - org.gradle.api.internal.artifacts.ivyservice.resolutionstrategy.DefaultResolutionStrategy_Decorated@e2d0ab

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\trunk\build.gradle' line: 52

* What went wrong:
A problem occurred evaluating root project 'txlibs'.
> Could not find property 'resoultionStrategy' on configuration ':alerting:archives'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 9.975 secs

有没有办法以编程方式设置解决策略?我已经尝试了各种组合来设置它,但它们都给出了相同的错误。

4

2 回答 2

1

I have now fixed this problem by changing the

ConfigurationContainer configurations = project.getConfigurations();
configurations.each { config ->
    println "config: " + config + " - " + config.resolutionStrategy
    config.resoultionStrategy.eachDependency{ strategy }
}

to

project.getConfigurations().all { config ->
    config.resolutionStrategy.eachDependency{ DependencyResolveDetails details ->
        if (details.requested.getVersion() == "default") {
            def key = details.requested.getGroup() + "." + details.requested.getName() + ".version.default"
            details.useVersion(project.ext.versionMap.get(key))
         }
    }
}

and moving the closure definition inline (not sure if the inline made any impact or not).

于 2013-09-25T16:24:06.410 回答
0

是不是里面的错别字

config.resoultionStrategy

还是只是一个例子而不是真实的东西?

于 2013-09-25T15:40:55.407 回答