如何将 ResolutionStrategies 与 Gradle 混合使用
How to mix ResolutionStrategies with Gradle
假设我的 gradle 构建脚本中包含以下内容:
configurations.all {
resolutionStrategy {
failOnVersionConflict()
force 'com.google.guava:guava:18.0'
}
}
如果找到多个版本的 jar,这将失败,除了 guava,它将强制使用版本 18.0.
现在假设我想为所有外部 jar 设置 failOnVersionConflict()
,并被迫使用 force 子句(所以我知道我在做什么),但我想使用默认的 resolutionStrategy (最新版本)针对某些特定组,例如 com.mycompany
。
这样的事情可能吗?
我正在查看此文档页面:
https://gradle.org/docs/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
我找到了自己的答案...但它涉及到一些 "hacking"...但是,毕竟这正是 gradle 提供的...
def dependencyErrors = 0
configurations.all {
resolutionStrategy {
def thirdPartyPackages = [:]
def forced = [
'com.google.guava:guava' : '18.0'
//Here all forced dependencies...
]
eachDependency { DependencyResolveDetails details ->
if (!details.requested.group.startsWith('com.mycompany')) {
def key = details.requested.group + ":" + details.requested.name
if(!thirdPartyPackages.containsKey(key)) {
if(forced.containsKey(key)) {
details.useVersion forced.get(key)
}
else {
thirdPartyPackages.put(key, details.requested.version);
}
}
else {
def existing = thirdPartyPackages.get(key);
if(existing != details.requested.version) {
logger.error "Conflicting versions for [$key]"
logger.error " [$existing]"
logger.error " [$details.requested.version]"
dependencyErrors++
}
}
}
}
}
}
myTask.doFirst {
//here it might also be doLast, or whatever you need. I just put it before a war task, but it might depend on each need.
if(dependencyErrors > 0) {
ant.fail 'There are ' + dependencyErrors + ' conflicting jar versions in the build.'
}
}
假设我的 gradle 构建脚本中包含以下内容:
configurations.all {
resolutionStrategy {
failOnVersionConflict()
force 'com.google.guava:guava:18.0'
}
}
如果找到多个版本的 jar,这将失败,除了 guava,它将强制使用版本 18.0.
现在假设我想为所有外部 jar 设置 failOnVersionConflict()
,并被迫使用 force 子句(所以我知道我在做什么),但我想使用默认的 resolutionStrategy (最新版本)针对某些特定组,例如 com.mycompany
。
这样的事情可能吗?
我正在查看此文档页面:
https://gradle.org/docs/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html
我找到了自己的答案...但它涉及到一些 "hacking"...但是,毕竟这正是 gradle 提供的...
def dependencyErrors = 0
configurations.all {
resolutionStrategy {
def thirdPartyPackages = [:]
def forced = [
'com.google.guava:guava' : '18.0'
//Here all forced dependencies...
]
eachDependency { DependencyResolveDetails details ->
if (!details.requested.group.startsWith('com.mycompany')) {
def key = details.requested.group + ":" + details.requested.name
if(!thirdPartyPackages.containsKey(key)) {
if(forced.containsKey(key)) {
details.useVersion forced.get(key)
}
else {
thirdPartyPackages.put(key, details.requested.version);
}
}
else {
def existing = thirdPartyPackages.get(key);
if(existing != details.requested.version) {
logger.error "Conflicting versions for [$key]"
logger.error " [$existing]"
logger.error " [$details.requested.version]"
dependencyErrors++
}
}
}
}
}
}
myTask.doFirst {
//here it might also be doLast, or whatever you need. I just put it before a war task, but it might depend on each need.
if(dependencyErrors > 0) {
ant.fail 'There are ' + dependencyErrors + ' conflicting jar versions in the build.'
}
}