Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail the Gradle build when dependencies are not available

build.gradle (unnecessary parts were ommited):

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")
}

Assume that the defined dependency is missing in configured Maven repository. When ./gradlew clean build tasks are executed the application is built successfully, although the required dependencies are missing.

Is there a way to configure Gradle to fail if there are unresolved dependencies?


Relates to:

  • How to make Gradle fail the build if a file dependency is not found? - Solution provided there is not applicable.
like image 401
nix9 Avatar asked Aug 31 '18 12:08

nix9


1 Answers

Consider this build.gradle (note: intentionally bogus jar specified in 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

example output:

$ 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
like image 112
Michael Easter Avatar answered Mar 06 '23 09:03

Michael Easter