backcab
backcab

Reputation: 668

Execute gradle tasks on sub projects in order

we have a project structure such that

ParentProject
    projA
    projB
    projC
    projD
    ...

We have a bunch of tasks that we run on these projects from the parent project, calling gradle clean build make tag push remove

clean: removes the build directory for each project
build: compiles the source code
make: uses the compiled code to create a docker image (custom task)
tag: tags the docker image (custom task)
push: pushes the tagged docker image to our nexus repo (custom task)
remove: removes the local docker image (custom task)

all the tasks work as intended, but not exactly in the order we want.

our issue is disk space. creating all the docker images (we have many to make) takes several gigabytes of space. Gradle fails because it runs out of disk space before reaching the end. We thought we could solve this issue by simply removing the image after it is pushed and free the disk space, but the way gradle runs, that does not work.

Current, gradle executes in the following manner:

projA:clean
projB:clean
projC:clean
...
projA:build
projB:build
projC:build
...
projA:make
projB:make
projC:make
...

going through like this, everything tries to build before anything gets removed. The way we would like to run gradle is as follows:

projA:clean
projA:build
projA:make
projA:tag
projA:push
projA:remove
projB:clean
projB:build
projB:make
projB:tag
projB:push
projB:remove
...

this way the project cleans itself up and frees disk space before the next project starts building.

is there any way to do this?

edit: There may be times where not every task needs to be run. The image may already built and simply needs to be retagged. Or may need to be built for local testing, but not tagged or pushed.

Upvotes: 2

Views: 2423

Answers (2)

backcab
backcab

Reputation: 668

We found we can create a task of type 'GradleBuild' where we can specify what tasks we want to run within a single task.

task deploy(type: GradleBuild){
  tasks = ['clean','build','make','tag','push','remove']
}

this solution prevents linking of tasks with dependencies, so we can still build sub projects individually without worrying about other tasks being run unnecessarily.

Upvotes: 2

user51
user51

Reputation: 10143

Looks like tasks make, tag, push, remove are custom tasks.

If you want dependency among tasks, for example taskX should be executed whenever taskY is started, then you should use dependsOn property of task.It is also called as task dependencies.

below shows the build.gradle on parent project that make task of projA dependsOn clean task of projA. you should do this for remaining tasks of projA.

project('projA') {
 task make(dependsOn: ':projA:build') {
    doLast {
        println 'make'
    }
 }
}

project('projA') {
task build {
    doLast {
        println 'build'
    }
  }
}

... ...

Then you should continue for remaining projects and remaining tasks. For example, ProjB:clean should dependOn projA:remove

project('projB') {
  task clean(dependsOn: ':projA:remove') {
    doLast {
        println 'clean'
    }
 }
}

continue for other tasks in project B and remaining projects.

For more details please follow this link.

Upvotes: 2

Related Questions