Gradle batch task that invokes subproject and othe

2019-04-06 19:21发布

问题:

I am writing gradle 1.4 build file for multimodule project. So there is root build.gradle that defines something like:

subprojects {
    apply plugin: 'java'
    ...

which defines build task for all submodules. Submodules are included in settings.gradle and each module has its build file with defined dependencies.

Everything by-the-book, so far:) Now, in the main build file I've added some additional project-scope tasks, like: aggregateJavadoc (collects all javadocs into one) or bundleJar (creates bundle jar from all classes), etc. Each on works when invoked manually.

Now I need a task release that will

  • build all submodules (as invoked from command line - meaning, i dont want to manually write execute() for each submodule)

  • invoke additional tasks (using execute() I presume).

I tried dependsOn but the order of listed tasks is not followed. Also, dependent modules seems to be executed after release task execution. I tried several other ideas and failed.

Question: what would be the best way to create such batch task, that has to invoke something on all submodules and additionally to perform some more tasks? What would be the best gradle-friendly solution? Thanx!

回答1:

It happened that this can be solved with simple dependency management.

So, we have many modules. Now, lets create additional tasks that depends on modules being build:

task aggregateJavadoc(type: Javadoc) {
    dependsOn subprojects.build

task bundleJar(type: Jar) {
    dependsOn subprojects.build

Finally, our release task would simply look like this:

task release() {
    dependsOn subprojects.build
    dependsOn aggregateJavadoc
    dependsOn bundleJar
    ...
}

This will build subprojects first; not because it is listed first, but because additional tasks depends on building. Order of additional task is not important. This make sense the most to me.

EDIT if one of your subprojects (i.e. modules) is non-java module, then you will have a problem building this project. What I do is to group submodules, like this:

def javaModules() {
    subprojects.findAll {it.name.contains('jodd-')}
}

and then instead to refer to subprojects, use javaModules everywhere! For example:

configure(javaModules()) {
    apply plugin: 'java'
    ...

and

task prj {
    dependsOn javaModules().build
}

btw, I am using this 'dummy' task prj for dependsOn on all those additional projects that depends on building, to prevent repeating.



标签: gradle