Proper up-to-date check settings for -proc:only Compile tasks to avoid unnecessary execution?

In Hibernate build we have a number of tasks that run annotation-processing tools. The JDJ “AP API” is sadly not really an API even, it just uses javac. So from Gradle, this means using a highly customized Compile task. We have a hard time getting these tasks set up correctly for UP-TO-DATE checks. Here is one as an example:

task generateMainLoggingClasses(type: Compile) {
            classpath = compileJava.classpath + configurations.jbossLoggingTool
            source = sourceSets.main.originalJavaSrcDirs
            destinationDir = aptDumpDir
                    compilerArgs: [
                            "-encoding", "UTF-8",
                            "-processor", "org.jboss.logging.processor.apt.LoggingToolsProcessor",
                            "-s", "$sourceSets.main.generatedLoggingSrcDir.absolutePath",
                            "-source", "1.6",
                            "-target", "1.6",
            outputs.dir sourceSets.main.generatedLoggingSrcDir;
            doFirst {
              source = sourceSets.main.originalJavaSrcDirs

This task seems to always run, even when nothing has changed. Which in turn means that now compileJava has to run because it depends on the above task, and so on.

What all do I need to set up to have this task execute only when needed?

It is important to note that javac requires a destination directory be specified, but it is literally irrelevant here. The real output directory is specified by ‘-s’ javac option to the AP tool.

Is ‘aptDumpDir’ guaranteed to be always empty? Otherwise the task will rerun whenever the contents of that directory changes.

Its a general dump dir that we use for all the APT tasks. Depends if javac spews any garbage there. Not sure I can gaurentee it will be empty.

This might explain why the tasks rerun. I’d try the following:

  • Set up one of the tasks with its own directory, say ‘loggingDumpDir’ * Add ‘doLast { loggingDumpDir.deleteDir() }’ to make sure that the directory is always left behind empty

Does this improve the up-to-date behavior for the task in question?

Conversely, couldn’t I just add aptDumpDir.deleteDir ion doFirst {} ?

Oh, and turn on info logging to see why the tasks are out-of-date.

Deleting in ‘doLast’ will perform better (because you will snapshot/compare empty directories), but as long as you give each task its own directory, it won’t really matter if and when you delete.

Your suggestion seems to have done it. Thanks!