本文介绍了从插件访问配置ResolutionStrategy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的gradle依赖关系中,我希望能够使用'default'版本并自动映射到特定版本的库。



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

 依赖项{
compile(group:'org.hibernate ','hibernate-core','default')

code

解决方案

我希望这给我相当于

pre code> dependencies {
compile(group:'org.hibernate',模块:'hibernate-core',版本'4.3.0.Beta4')
}

映射在一个文件中指定,该文件包含类似于

  org.hibernate.hibernate-core = 4.3之类的键/值对.0 .Beta4 

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

我可以让这个映射与fo相当愉快地工作放在我的build.gradle文件中

 配置{
全部{
resolutionStrategy {
eachDependency {DependencyResolveDetails详细信息 - >
if(details.requested.getVersion()==default){
def key = details.requested.getGroup()+。 details.useVersion(versionMap.get(key))
}
}
}
}

$ / code>

(我需要承认Peter Niederwieser帮助我达到这个目标)

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

  import org.gradle.api.Plugin; 
import org.gradle.api.Project;
导入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.useVersion(ext.versionMap.get(key))
}
}

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

上面的println给出了有效的输出,但是下面的行给出了一个错误。

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

失败:构建失败,出现异常。

*其中:
构建文件'C:\trunk\build.gradle'行:52

*出错:
A问题发生在评估根项目'txlibs'。
>在配置中找不到属性'resoultionStrategy':alerting:archives'。

*尝试:
使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。

建造失败

总时间:9.975秒



解决方案

改变

  ConfigurationContainer配置= project.getConfigurations(); 
configurations.each {config - >
printlnconfig:+ config + - + config.resolutionStrategy
config.resoultionStrategy.eachDependency {strategy}
}
$ b $ p

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

并且内联移动闭包定义(不确定内联是否产生了影响)。


In my gradle dependencies I want to be able to use a version of 'default' and have that automatically map to a specific version of a library.

As an example I may have the following in my build.gradle:

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

on resolution I want this to give me the equivalent of

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

The mapping is specified in a file that contains key/value pairs of something like

org.hibernate.hibernate-core=4.3.0.Beta4

which is read into an extended property of type HashMap on the project called versionMap.

I can get this mapping to work quite happily with the following in my build.gradle file

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))
                }
            }
        }
    }
}

(I need to acknowledge the help of Peter Niederwieser for getting me this far)

As i need the same thing in a number of my projects I want to make this a plugin, so I have created a plugin project and it all build nicely but when I try and run it I cannot access the resolutionStrategy to set the logic. My plugin code looks like this:

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 }
        }
    }
}

The println above gives valid output but the line following gives an error. The output from running this plugin is.

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

Is there any way to set the resolution strategy programatically? I have tried all manner of combinations to setting this but they all give the same error.

解决方案

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).

这篇关于从插件访问配置ResolutionStrategy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:03