gradlebuild.gradle

Gradle keeps getting a "Could not get unknown property for root project" error


As someone new to gradle, I'm trying to have my build step depend on a custom task.

My build.gradle contains this code:

repositories {
  jcenter()
}

apply plugin: 'base'

defaultTasks 'build'
build.dependsOn compileAll

task compileAll {
  doLast {
    println "hello" 
  }
}

If I remove the build.dependsOn compileAll line, this works fine. I think I'm doing something wrong but I'm not sure what.


Solution

  • The problem is that you are creating the dependency between build and compileAll tasks before you actually declare the compileAll task. So Gradle does not know about this task and generates the error Could not get property.... Remember that build scripts are actually real SCRIPTs, the order of instructions/blocks matters.

    This will work:

    // first declare "compileAll" task
    task compileAll {
        doLast {
            println "hello"
        }
    }
    // then you can reference this compileAll task declare above
    build.dependsOn compileAll