Delay plugin build until after javaCompile


(Glen Miller) #1

I am writing a custom Java plugin to generate a report on annotated methods, using reflection. Some of the annotations I need to use are in our code, and will therefore not available as imports in the plugin until the compileJava task has run and the .jar files are built. I have defined my task as “dependsOn” the compileJava task, but gradle fails as it appears to try to build my task before the compileJava step runs, and fails on the missing import.

/buildSrc/build.gradle

dependencies {
compile ‘org.reflections:reflections:0.9.11’
compile fileTree(dir: ‘…\build\libs’, include: ‘*.jar’)
}

/buildSrc/src/main/java/my/plugins/MyPlugin.java

public class MyPlugin implements Plugin {
@Override
public void apply(Project project) {
MyTask myTask = project.getTasks().create(“mytask”, MyTask.class);
Task javaCompileTask = project.getTasks().getByName(“compileJava”);
myTask.dependsOn(javaCompileTask);
}
}

Is there a way to do this?


(James Justinic) #2

It is not possible to have the buildSrc project depend on build artifacts of the same build. The entire build essentially has a dependency on buildSrc, so this will always be an impossible cycle. In order for something like this to work, the annotations need to be isolated such that both buildSrc and the consuming projects of your current project can depend on the annotation project.


(Glen Miller) #3

Makes sense. Thank you.