How to tell Gradle to run the 'jar' task on all sub-projects from a plugin


(Renato Athaydes) #1

Hi! I am trying to write a Gradle plugin.

One of my plugin’s added tasks depends on ‘jar’ (from the java plugin). That’s because I expect to use the jar artifact(s) generated by the project, but I was also expecting to be able to use all artifacts generated by the sub-projects, but that’s not being possible because when I run my plugin’s task, Gradle only triggers ‘jar’ for the main project, not for the sub-projects.

Here’s how I declare my task:

@Override
    void apply( Project project ) {
        project.apply( plugin: 'osgi' )
        // other stuff not relevant
        project.task( dependsOn: 'jar', 'myTask' ) << closureWithTheTaskCode
    }

Is there a way to tell Gradle (from my plugin, not on the user’s build file) to run ‘jar’ for all sub-projects before running my task?

Thanks!


(Gary Hale) #2
project.allprojects {
  it.tasks.withType(Jar) { jarTask ->
    myTask.dependsOn jarTask
  }
 }

Alternatively, you can use subprojects instead of allprojects.


(Renato Athaydes) #3

Thanks, pretty simple!