nix9
nix9

Reputation: 638

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:

Upvotes: 6

Views: 3099

Answers (1)

Michael Easter
Michael Easter

Reputation: 24468

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

Upvotes: 5

Related Questions