Replacing tasks (overwrite: true) does not work in 1.5


(drf) #1

Using the following sample build.gradle script that worked in gradle 1.4, but fails in 1.5:

apply plugin: 'java'
  task compileJava(type: MyCompileJavaTask, overwrite: true) {
  sourceSet sourceSets.main
  configuration configurations.compile
  println "### USING MyCompileJavaTask instead of default compileJava"
}
    class MyCompileJavaTask extends DefaultTask {
  def sourceSet
      @Input
  def configuration
    public void setConfiguration(configuration) {
    this.configuration = configuration
    dependsOn configuration.getTaskDependencyFromProjectDependency(true, "jar")
  }
    @InputFiles
  public FileCollection getClasspath() {
    sourceSet.compileClasspath
  }
    @InputFiles
  public FileCollection getSources() {
    sourceSet.java
  }
    @OutputDirectory
  public File getDestinationDir() {
    sourceSet.output.classesDir
  }
      @TaskAction
  def compile() {
    println "### TODO Executing my own Java compilation task"
  }
     }
  task printCompileJavaType << {
  println("### compileJava type now: ${compileJava.getClass().toString()}")
}
  compileJava.dependsOn printCompileJavaType

If you run task

compileJava

, then the output is

### USING MyCompileJavaTask instead of default compileJava
:task-overwrite:printCompileJavaType
### compileJava type now: class MyCompileJavaTask_Decorated
:task-overwrite:compileJava
### TODO Executing my own Java compilation task
  BUILD SUCCESSFUL

as expected. If you run task

classes

(or any other task that depends on the replaced

compileJava

task directly or indirectly), then the output is

### USING MyCompileJavaTask instead of default compileJava
:task-overwrite:compileJava
:task-overwrite:processResources UP-TO-DATE
:task-overwrite:classes
  BUILD SUCCESSFUL

which is incorrect. The dependant tasks originally created by the java plugin still use the original

compileJava

task.

Are there any ways of working around this, other than manually setting the dependencies of all the directly dependant tasks (horrible solution)? Any help is greatly appreciated, -feri


(Peter Niederwieser) #2

As things are today, ‘overwrite: true’ doesn’t give any guarantees about what happens to task dependencies on the original task. A potential alternative is to only overwrite the task action. Why are you overwriting ‘compileJava’ in the first place?


(drf) #3

Hello, and thanks for your reply.

Our problem, of course, is the AspectJ problem already reported elsewhere http://forums.gradle.org/gradle/topics/aspectj_plugin_does_not_work_with_gradle_1_5 .

How does one overwrite task actions?


(drf) #4

Hm, I guess Task.setActions should suffice?


(Peter Niederwieser) #5

Yes. In a build script, you would do something like ‘compileJava { actions = []; doLast { … } }’.


(drf) #6

Okay, overwriting task action works, thanks.

A small suggestion though: can that task overwrite feature be deprecated / removed from API & documentation then?


(Peter Niederwieser) #7

A discussion has been started here: http://gradle.1045684.n5.nabble.com/Long-term-plan-for-replacing-tasks-td5711257.html