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


(steve) #1

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
            options.define(
                    compilerArgs: [
                            "-nowarn",
                            "-proc:only",
                            "-encoding", "UTF-8",
                            "-processor", "org.jboss.logging.processor.apt.LoggingToolsProcessor",
                            "-s", "$sourceSets.main.generatedLoggingSrcDir.absolutePath",
                            "-AloggingVersion=3.0",
                            "-source", "1.6",
                            "-target", "1.6",
                            "-AtranslationFilesPath=${project.rootDir}/src/main/resources"
                      ]
            );
            outputs.dir sourceSets.main.generatedLoggingSrcDir;
            doFirst {
//
              source = sourceSets.main.originalJavaSrcDirs
                sourceSets.main.generatedLoggingSrcDir.mkdirs()
            }
        }

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.


(Peter Niederwieser) #2

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


(steve) #3

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.


(Peter Niederwieser) #4

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?


(steve) #5

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


(Peter Niederwieser) #6

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


(Peter Niederwieser) #7

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.


(steve) #8

Your suggestion seems to have done it. Thanks!