Jar.doLast() modifies my jar file


(Christian Abegg) #1

I’m working on the gradle-clover-plugin (https://github.com/bmuschko/gradle-clover-plugin) which instruments the classes with clover. I’d like to add the possibility to get jars containing the instrumented classes. I use the following approach:

project.tasks.getByName("jar").each { Task jar ->
  jar.doFirst createInstrumentCodeAction()
  jar.doLast createRestoreClassesAction()
}

The solution behaves as follows: 1) the instrument code action moves the original class files to a backup directory (jar.doFirst) 2) the instrument code action compiles the java files using the clover compiler in the standart class directory (jar.doFirst) 3) the jar is created with the instrumented classes (actual jar task)

  1. the restore classes action deletes the directory with the instrumented classes and moves the backup directory to its original place (jar.doLast) 5) the jar is somehow re-generated and contains now the uninstrumented classes

The jar containing the instrumented classes can be seen when I throw an exception in the restore classes action or when I do not execute any file movements in this action.

How can I achieve the desired behaviour: get the jar with the instrumented classes and restore the original (non-instrumented) classes from the backup directory?


(Christian Abegg) #2

Waiting for the task graph to be ready led to the expected behaviour:

project.gradle.taskGraph.whenReady {
  project.tasks.getByName("jar").each { Task jar ->
    jar.doFirst createInstrumentCodeAction()
    jar.doLast createRestoreClassesAction()
  }
}