最终目标是用不同的版本替换传递依赖项的版本。需要注意的是,替换应该由依赖于构建库的人来接收(我不知道它是标准的Gradle还是插件,但如果我们exclude
是传递依赖,那么生成的ivy.xml
文件将包含该信息。)
实现最终目标的一种可能方法是exclude
有问题的依赖关系然后在以后强制依赖。
exclude
依赖关系的方式是:
dependencies {
compile 'org:name:version' {
exclude(group: 'group', module: 'module')
}
}
force
依赖关系的方式是:
configurations.all {
resolutionStrategy {
eachDependency { DependencyResolveDetails dependencyResolveDetails ->
final requestedDependency = dependencyResolveDetails.requested
if (requestedDependency.group == 'org' && requestedDependency.name == 'name') {
force 'group:module:good_version'
}
}
}
}
为了将两者结合在一起,resolutionStrategy
必须知道哪些依赖关系实际上exclude
d以后将是force
d的传递依赖关系。如何以通用的方式完成(假设有exclude
的通用方法)?如果没有办法将两者结合在一起,是否有不同的方法来实现最终目标?
答案 0 :(得分:0)
首先,抽象出依赖关系,使它们看起来像:
dependencies {
compile dep('org:name')
}
然后可以用照顾自动排除的方式定义dep
函数:
final rev = [
'group:module': 'good_version'
'org:name': 'version']
ext.rev = { m ->
rev[m]
}
final dep = { m ->
"${m}:${ext.rev(m)}"
}
final forcedTransitiveDeps = [
'org:name': [
dep('group:module')]].withDefault({[]})
ext.forcedTransitiveDeps = { m ->
forcedTransitiveDeps[m]
}
ext.dep = { m ->
if (!forcedTransitiveDeps.containsKey(m)) {
project.dependencies.create(dep(m))
} else {
project.configure(
project.dependencies.create(dep(m)),
{
forcedTransitiveDeps[m].each { dependency ->
final dependencyParts = dependency.split(':')
exclude(group: dependencyParts[0], module: dependencyParts[1])
}
})
}
}
最后,重新引入并强制依赖:
subprojects { subproject ->
subproject.afterEvaluate {
subproject.configurations.all { Configuration configuration ->
final dependencies = configuration.getDependencies()
final forcedDependencies = (dependencies.collect { dependency ->
forcedTransitiveDeps("${dependency.group}:${dependency.name}")
}).flatten()
if (!forcedDependencies.isEmpty()) {
logger.info("Forcing ${subproject} dependencies on ${forcedDependencies} as per `ext.forcedTransitiveDeps`.")
dependencies.addAll(forcedDependencies.collect { forcedDependency ->
subproject.dependencies.create(forcedDependency)
})
}
}
}
}