5
votes

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

3

3 Answers

0
votes

To fail the build you can:

ant.fail('message why it failed')

Then you can craft a condition then fail the build with nice message ;)

I would suggest to create a task that will bring the file to the project first with a condition to check if the file is available etc if not then throw a Gradle exception and fail the build with a message, and execute the task first in the execution phase.

I have no chance to test it now but it could be something like this, correct me if any syntax is wrong - but you should get the idea.

def yourDep = $/\path\to\your\depdendency/$

task bringDeps << {

  if (yourDep.exists()){
    copy {
      from yourDep
      into $projectDir/depsOrSmthg
    }
  } else{
  ant.fail('message why it failed')
  }

}
0
votes

You could do something as shown below. It is not a built-in Gradle function but does not require code to check each dependency specifically (it checks all in the compile configuration):

apply plugin: 'java'

dependencies {
    compile files('lib/abc.jar')
    compile files('lib/def.jar')
}

task checkDependencies() {
    doLast {
        configurations.compile.each { file ->
            assert file.exists() 
        }
    }
}

compileJava.dependsOn checkDependencies
0
votes
task ensureDependenciesExist() {
    doLast {
        configurations.implementation.canBeResolved(true)
        DependencySet deps = configurations.implementation.getDependencies()
        Set<File> impFiles = configurations.implementation.resolve()
        deps.each { d ->
            boolean depWasResolved = impFiles.any { impFile -> impFile.name.find(".*${d.name}.*${d.version}") }
            if (!depWasResolved) {
                println "${d} was not resolved"
                assert depWasResolved
            }
        }
    }
}

compileJava.dependsOn ensureDependenciesExist