Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle batch task that invokes subproject and other tasks in order

Tags:

gradle

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!

like image 373
igr Avatar asked Jan 30 '13 12:01

igr


People also ask

Does Gradle run tasks in order?

In Gradle terms this means that you can define tasks and dependencies between tasks. Gradle guarantees that these tasks are executed in the order of their dependencies, and that each task is executed only once.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.

What is Buildscript in Gradle?

buildscript: This block is used to configure the repositories and dependencies for Gradle. Note: Don't include dependencies here. ( those will be included in the module-level build.gradle) dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project. Java.


1 Answers

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.

like image 83
igr Avatar answered Sep 28 '22 21:09

igr