How do I specify a project dependency on a plain text file like setup?json?


(Joe Linoff) #1

I am a complete newbie that is trying to use gradle to automate a non-Java (e.g. JVM) build. As part of the build I need to make sure that certain files are present. It must fail if these files are not present. I configured it like this:

repositories {
    flatDir {
        dir '.'
    }
}
dependencies {
    compile files('setup.json', 'README.md')
}

But when i ran a build, it worked. I was hoping to get an error message that said file does not exist.

What am i doing wrong?

I have read the User Guide and the DSL but it is clear that i don’t fully understand all of the concepts.

Thanks.


(Joe Linoff) #2

I figured it out. This question was based on my noob misunderstanding of the system. Here is an example that shows how to do the pre-condition checks I needed.

// Demonstrate how to do simple pre-condition checks for files.
apply plugin: 'base'

// Verify that the pre-conditions for this run
// are met.
task checkPreConditions {
  outputs.files files('setup.json', 'README.txt')
  doFirst {
    def msg = []
    outputs.files.getFiles().each { f ->
      if (!f.exists()) {
        msg += "ERROR: required file does not exist ${f}."
      } else {
        println "INFO: ${it} okay ${f}"
      }
    }
    if (msg.size()) {
      throw new GradleException(msg.join("\n"))
    }
  }
}

// Use the preCondition files.
task x1(dependsOn: checkPreConditions) {
  doLast {
    project.checkPreConditions.outputs.getFiles().each { f ->
      println "INFO: ${it} found ${f}"
    }
  }
}