当依赖项不可用时 Gradle 构建失败
Fail the Gradle build when dependencies are not available
build.gradle
(省略不必要的部分):
apply plugin: 'java'
repositories {
mavenCentral()
maven {
credentials {
username "$mavenUser"
password "$mavenPassword"
}
url "http://localhost:8081/nexus/content/groups/public"
}
}
dependencies {
compile("com.example:some-lib:1.0.0-RELEASE")
}
假设配置的 Maven 存储库中缺少定义的依赖项。 ./gradlew clean build
任务执行后,应用程序构建成功,尽管缺少所需的依赖项。
有没有办法配置 Gradle 在存在未解决的依赖项时失败?
涉及:
- How to make Gradle fail the build if a file dependency is not found? - 提供的解决方案不适用。
考虑这个build.gradle
(注意:故意在dependencies
中指定的伪造jar):
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile("junit:junit:4.12")
compile("junit:junitxyz:51.50")
}
task checkDependencies() {
doLast {
configurations.compile.each { file ->
println "TRACER checking: " + file.name
assert file.exists()
}
}
}
compileJava.dependsOn checkDependencies
示例输出:
$ gradle -q clean compileJava
FAILURE: Build failed with an exception.
[snip]
* What went wrong:
Execution failed for task ':checkDependencies'.
> Could not resolve all files for configuration ':compile'.
> Could not find junit:junitxyz:51.50.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/junit/junitxyz/51.50/junitxyz-51.50.pom
- https://repo.maven.apache.org/maven2/junit/junitxyz/51.50/junitxyz-51.50.jar
build.gradle
(省略不必要的部分):
apply plugin: 'java'
repositories {
mavenCentral()
maven {
credentials {
username "$mavenUser"
password "$mavenPassword"
}
url "http://localhost:8081/nexus/content/groups/public"
}
}
dependencies {
compile("com.example:some-lib:1.0.0-RELEASE")
}
假设配置的 Maven 存储库中缺少定义的依赖项。 ./gradlew clean build
任务执行后,应用程序构建成功,尽管缺少所需的依赖项。
有没有办法配置 Gradle 在存在未解决的依赖项时失败?
涉及:
- How to make Gradle fail the build if a file dependency is not found? - 提供的解决方案不适用。
考虑这个build.gradle
(注意:故意在dependencies
中指定的伪造jar):
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile("junit:junit:4.12")
compile("junit:junitxyz:51.50")
}
task checkDependencies() {
doLast {
configurations.compile.each { file ->
println "TRACER checking: " + file.name
assert file.exists()
}
}
}
compileJava.dependsOn checkDependencies
示例输出:
$ gradle -q clean compileJava
FAILURE: Build failed with an exception.
[snip]
* What went wrong:
Execution failed for task ':checkDependencies'.
> Could not resolve all files for configuration ':compile'.
> Could not find junit:junitxyz:51.50.
Searched in the following locations:
- https://repo.maven.apache.org/maven2/junit/junitxyz/51.50/junitxyz-51.50.pom
- https://repo.maven.apache.org/maven2/junit/junitxyz/51.50/junitxyz-51.50.jar