Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Gradle fail the build if a file dependency is not found?

Tags:

java

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)

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.

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)?

Edit: to clarify further:

If a dependency cannot be found in the configured repositories, Gradle will fail the build when attempting to resolve them, as expected.

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.

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?)

like image 856
David Avatar asked Mar 01 '17 09:03

David


People also ask

Why does Gradle build fail?

Sometimes due to any issue or after formatting of your pc. Some of the Gradle files may get deleted unexpectedly. So when you will start building your apps you will get to see an error in Android studio as 'Error running android: Gradle project sync failed.

How do I resolve Gradle dependencies?

Given a required dependency, with a version, Gradle attempts to resolve the dependency by searching for the module the dependency points at. Each repository is inspected in order. Depending on the type of repository, Gradle looks for metadata files describing the module ( .

How do you avoid transitive dependencies in Gradle?

You can tell Gradle to disable transitive dependency management for a dependency by setting ModuleDependency. setTransitive(boolean) to false . As a result only the main artifact will be resolved for the declared dependency.

How do you exclude test cases in build Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build.


2 Answers

This is a bit of an old thread, but given that none of the currently proposed solutions actually works, and the solution appears to be trivial (collating two of them), I am leaving it here for future reference.

The point here is that we simply want to ensure that the files do exist, so we can just use the exists() method of the File class:

task ensureDepsExist() {
    doLast {
        configurations.implementation.canBeResolved(true)
        Set<File> impFiles = configurations.implementation.resolve()

        impFiles.forEach { f ->
            if (!f.exists()) {
                ant.fail "${f} could not be found"
            }
        }
    }
}

compileJava.dependsOn ensureDepsExist

The canBeResolved() call is required, or Gradle will complain that configurations dependencies cannot be resolved.

like image 147
Marco Massenzio Avatar answered Sep 22 '22 07:09

Marco Massenzio


Here's how you can check transitive dependencies using Gradle 7.3 (example: Fail if the project depends on log4j directly or transitively).

Kotlin DSL

configurations {
  all {
    relsolutionStrategy {
      eachDependency {
        if (requested.name == "log4j") {
          throw RuntimeException("Project depends on log4j")
        }
      }
    }
  }
}

Groovy DSL

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.name == 'log4j') {
            throw new RuntimeException("Project depends on log4j")
        }
    }
}
like image 34
MrcJkb Avatar answered Sep 20 '22 07:09

MrcJkb