如果找不到文件依赖项,如何使Gradle生成失败? [英] How to make Gradle fail the build if a file dependency is not found?
问题描述
我有一个Gradle构建,该构建具有某些形式的依赖项
I have a Gradle build that has some dependencies of the form
compile files('path/to/local/lib.jar')
(内部版本正在迁移-最终将被替换)
(the build is being migrated - eventually these will be replaced)
构建失败,因为错误地指定了这些路径之一.但是由于编译错误而失败-看起来Gradle默默地忽略了缺少的依赖项.
The build failed because one of these paths was incorrectly specified. But it failed due to a compile error - it looked like Gradle silently ignored the missing dependency.
是否存在一个简单的选项或开关,如果无法解决任何依赖性(尤其是本地文件依赖性)(例如,文件丢失),将迫使Gradle使构建失败?
Is there a simple option or switch that will force Gradle to fail the build if any dependency (particularly local file dependencies) cannot be resolved (eg., file missing)?
进一步说明:
如果在配置的存储库中找不到依赖项,则Gradle将在尝试解析它们时按预期方式使构建失败.
If a dependency cannot be found in the configured repositories, Gradle will fail the build when attempting to resolve them, as expected.
但是-如果将依赖项定义为编译文件....",并且在构建时指定的文件不存在,则Gradle将忽略该错误,并尝试进行编译.这似乎是引人注目的错误且不一致的默认行为.
BUT - if a dependency is defined as "compile files ....", and the file specified does not exist at build time, Gradle will IGNORE that error, and attempt compilation anyway. That seems spectacularly wrong-headed and inconsistent default behaviour.
我的问题是-是否可以设置以强制Gradle验证文件依赖项存在的Gradle选项或开关或环境变量或系统属性? (例如,以理性合理的方式行事吗?)
My question is - is there a Gradle option or switch or environment variable or system property that I can set to force Gradle to verify that file dependencies exist? (E.g,, behave in a sane and rational way?)
推荐答案
task ensureDependenciesExist() {
doLast {
configurations.implementation.canBeResolved(true)
DependencySet deps = configurations.implementation.getDependencies()
Set<File> impFiles = configurations.implementation.resolve()
deps.each { d ->
boolean depWasResolved = impFiles.any { impFile -> impFile.name.find(".*${d.name}.*${d.version}") }
if (!depWasResolved) {
println "${d} was not resolved"
assert depWasResolved
}
}
}
}
compileJava.dependsOn ensureDependenciesExist
这篇关于如果找不到文件依赖项,如何使Gradle生成失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!