How to replace TaskInternal.execute() in compileJava.doLast for postprocessing classes in place?

After upgrading from 4.2 to 4.4.1 I noticed a new warning: ‘The TaskInternal.execute() method has been deprecated and is scheduled to be removed in Gradle 5.0.’, related to

I use this to postprocess the compiled classes in place. How can I do this without using execute()?

(Un?)related question: this postprocessing uses ASM, after it has been compiled. In other words, this code postprocesses itself. Currently I solve this circularity issue by compiling the code twice, see Is there a way to avoid this double compilation? I guess this requires to not do the post processing in place (which could solve the first question), but I could not find an easy way to do this. The best I could get, which is not fully working and does not seem well designed, is something like this:


sourceSets.main.output.classesDir = 'build/raw_classes/java/main'
sourceSets.test.compileClasspath -= files('build/raw_classes/java/main')
sourceSets.test.compileClasspath += files('build/classes/java/main')
sourceSets.test.runtimeClasspath -= files('build/raw_classes/java/main')
sourceSets.test.runtimeClasspath += files('build/classes/java/main')

task postprocess(type: JavaExec) {
  classpath project(':tools:retrofitter').tasks.jar.outputs.files
  main = ''
  args 'build/raw_classes/java/main', 'build/classes/java/main'

jar {
  exclude { files('build/raw_classes/java/main') }
  include { files('build/classes/java/main') }

postprocess.dependsOn compileJava, ':tools:retrofitter:build'
classes.dependsOn postprocess
1 Like

Did you ever find a solution to this?

I’m finding that using TaskInternal.execute() ends up crashing gradle unless i use --console=plain due to the incorrect progress reporting that results from calling TaskInternal.execute(). (you may see progress at >100% and then a crash)

Would rather fix this than force everyone to use --console=plain – plus have an upgrade path to Gradle 5

I replaced the task with some equivalent Java code in compileJava.doLast(), see