gpt4 book ai didi

gradle - 当两个依赖项之间存在版本冲突且其中一个没有指定组时,我能否使 gradle 构建失败

转载 作者:行者123 更新时间:2023-12-03 02:59:59 26 4
gpt4 key购买 nike

我试图弄清楚如何让 gradle 在两个依赖项之间的版本冲突时失败,当其中一个依赖项没有指定组时。

例如,我有一个项目依赖于配置的平面目录存储库中的 jar(对于供应商)

编译 ':guava:r09'

以及指向从 maven 存储库中检索到的模块的传递依赖

编译 'com.google.guava:guava:13.0.1'

我了解根据设计 failOnVersionConflict 解决策略不会引发上述示例的冲突。我无法确定平面目录中 jars 的组名,因此想引发冲突,开发人员可以相应地强制其中一个模块。感谢您花时间按问题阅读。

更新:

根据 Ben 的评论,我附上了用于引发自定义冲突的代码片段。但这对我的情况没有帮助,因为我无法利用强制解决策略来解决冲突。我可以排除传递依赖或完全删除直接依赖。希望它对其他人有用。

gradle.afterProject {
project.configurations.each { conf ->
def map = new HashMap<String, List<Dependency>>()
//println "\tConfiguration- ${project.name}:${conf.name}"
conf.allDependencies.each { dep ->
//println "\t\t${dep.group}:${dep.name}:${dep.version}"
ArrayList<Dependency> dependencies = null
if(map.containsKey(dep.name))
{
dependencies = map.get(dep.name)
}
else
{
dependencies = new ArrayList<>()
map.put(dep.name, dependencies)
dependencies.add(dep)
}

if(dep.group == null || dep.group.equals("unspecified"))
{
for(Dependency depInMap : dependencies) {

if(depInMap.version == null && dep.version == null)
continue;

if(depInMap.version != null && depInMap.version.equals(dep.version))
continue;

throw new GradleException("Customized Conflict: A conflict was found in " +
"${project.name}:${conf.name} between the following modules:" +
"\n- ${dep.group}:${dep.name}:${dep.version}" +
"\n- ${depInMap.group}:${depInMap.name}:${depInMap.version}")

}
}

dependencies.add(dep);
}
}

最佳答案

这是我想出的解决方案。下面给出的是可能用​​过它的其他人的代码片段。代码不整洁但功能齐全。欢迎提出任何建议。



private static boolean isGroupEmpty(DependencyResolveDetails details){
if (details.requested.group == null) {
return true;
} else if (details.requested.group == "unspecified"){
return true;
} else if(details.requested.group.isEmpty()) {
return true;
} else {
return false;
}
}

configurations.all {

def dependencyMap = [:]
def forcedModules = resolutionStrategy.forcedModules
resolutionStrategy.eachDependency { DependencyResolveDetails details ->

def targetToUse = null
for (forcedModule in forcedModules) {
if(forcedModule.name == details.requested.name &&
(forcedModule.group == details.requested.group || isGroupEmpty(details))) {
targetToUse = "${forcedModule.group}:${forcedModule.name}:${forcedModule.version}"
}
}

if(targetToUse != null) {
println "Forcing: " + targetToUse
details.useTarget targetToUse
}
else {
if(dependencyMap.containsKey(details.requested.name)) {
DependencyResolveDetails prevDetails = dependencyMap.get(details.requested.name);

boolean groupMatches = false
if(isGroupEmpty(prevDetails) || isGroupEmpty(details) ||
prevDetails.requested.group == details.requested.group){
groupMatches = true
}

if(groupMatches)
{
boolean versionMatches = false
if(prevDetails.requested.version == details.requested.version) {
versionMatches = true
}

if(!versionMatches)
{
//If versions don't match throw an exception.
throw new GradleException("Custom Conflict: A conflict was found in " +
"${project.name} between the following modules:" +
"\n- ${prevDetails.requested.group}:${prevDetails.requested.name}:" +
"${prevDetails.requested.version}" +
"\n- ${details.requested.group}:${details.requested.name}:" +
"${details.requested.version}")


}
else {
//Use either one (in this case I force the one with empty group)
DependencyResolveDetails repl
DependencyResolveDetails pref
if(isGroupEmpty(prevDetails)) {
pref = prevDetails
repl = details
}
else {
pref = details
repl = prevDetails

dependencyMap.put(details.requested.name, details)
}

repl.useTarget "unspecified:${pref.requested.name}:${pref.requested.version}"

println "Replacing module " +
"\n - ${repl.requested.group}:${repl.requested.name}:${repl.requested.version}" +
"\n with " +
"\n - ${pref.requested.group}:${pref.requested.name}:${pref.requested.version} ."

}
}
}
else {
dependencyMap.put(details.requested.name, details)
}
}
}
}

关于gradle - 当两个依赖项之间存在版本冲突且其中一个没有指定组时,我能否使 gradle 构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30983663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com