Methods execute before tasks?

I’m new at gradle. I have read the User’s Guide and either missed some behavior or do not understand something fundamental about gradle. I have several methods defined and several tasks. And everything worked perfectly until I removed some directories that I wanted the build script to create at which time the entire script failed in all respects. The script is fairly large so I will start with examples and then if that does not help i will post the entire script.

I have methods

def isGenerated (siteFile, bMyriad, eMyriad) {
String ttext = siteFile.getText()
return ttext.indexOf(bMyriad) > 0 && ttext.indexOf(eMyriad) > 0
}

task doStuff() {

 .....
 .....
if (isGenerated(getYarnSiteFile(yhome), "BEGIN", "END")) {

}

}

I’m using gradle 2.3 (was using 2.1) wrapper…if I do

gradlew clean

The output is

Build file ‘/vagrant/build.gradle’ line: 84

  • What went wrong:
    A problem occurred evaluating root project ‘myriad’.

/usr/local/hadoop/etc/hadoop/yarn-site.xml (No such file or directory)

So the method is being executed on “clean” which is a standard task. In fact the same thing happens no matter what I use as the task.

What am I missing?

Thanks

you’re calling the isGenerated method in the configuration phase of your task which is evaluated on every build invocation. I guess you want to execute this method during the execution phase. in order to do that change your doStuff task:

task doStuff() {
    doLast {
        if (isGenerated(getYarnSiteFile(yhome), "BEGIN", "END")) {
    }
}

Have a look at the build lifecycle in the gradle userguide for the details about configuration phase vs. execution phase: http://gradle.org/docs/current/userguide/userguide_single.html#build_lifecycle

Thanks! I missed that section obviously…fixed the problem.