【问题标题】:Fail the Gradle build when dependencies are not available当依赖项不可用时,Gradle 构建失败
【发布时间】:2018-08-31 12:01:07
【问题描述】:

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 配置为失败?


涉及:

【问题讨论】:

  • 您使用的是哪个版本的 Gradle?我使用 >= 4.8 的版本,如果缺少任何依赖项,Gradle 构建会按预期失败。
  • @M.Ricciuti 我正在使用版本4.8.1
  • 即使在低版本中,Could not resolve all files for configuration ':*****' Could not resolve **** 也应该默认触发
  • @M.Ricciuti 我使用 4.10.3 并且构建没有失败。我打电话给./gradlew dependencies 来检查依赖关系
  • @Davide 任务dependencies 将成功执行,但它将在输出依赖关系图中将丢失/无效的依赖关系标记为FAILED

标签: gradle build.gradle nexus


【解决方案1】:

考虑这个build.gradle注意:故意在dependencies中指定的假罐子):

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

【讨论】:

    猜你喜欢
    • 2018-11-28
    • 2016-07-11
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多