Gradle clean all projects

I have a multi-project setup and would like to find a way how to invoke ‘clean’ and ‘cleanEclipse’ for all projects, even if I am in a child project (not in root).

If I invoke “gradle clean” in root-project: Works

If I invoke “gradle :clean” in sub-project:
Task ‘clean’ not found in root project ‘server’.

Inspired by http://stackoverflow.com/questions/14604266/gradle-batch-task-that-invokes-subproject-and-other-tasks-in-order, I tried to implement a task in root build.gradle which does what i want. But the subprojects clean tasks will not be invoked when i run this.

def cleanTasks() {
	def ret = []
    subprojects.findAll {
    	def task = it.getTasks().findByPath('clean')
    	if(task!=null){
    		ret << task.getPath()
    	}
    	it
    }
    ret
}


task cleanAll() {
	   doLast{
    	print 'tasks:'
    	print cleanTasks()
    	print '\ndependsOn:'
    	print getDependsOn()
    }
}

cleanAll.dependsOn cleanTasks()

Output:

:cleanAll
tasks:[:subproject1:clean, :subproject2:clean]
dependsOn:[[], file collection]
BUILD SUCCESSFUL

And yes: I know about --rerun-tasks, but it’s not what i want (sometimes old artifacts remain in build dir, i want to clean all up).

If what you want to do is to create a clean task on the root project that triggers clean for all subprojects, and that not all subprojects define a clean task, you can still create it in the root project:

task clean {
    subprojects.each {
        it.afterEvaluate {
            def cleanTask = it.tasks.findByName('clean')
            if (cleanTask) {
                dependsOn(cleanTask)
            }
        }
    }
}
1 Like

I’d do this…

task clean

subprojects {
  rootProject.clean dependsOn tasks.matching { it.name == "clean" }
}

Thanks mate, worked!
luke_daley’s solution did not work for me however (Could not find property ‘dependsOn’ on project ‘:core’.)

I use this now for my multi-project setup:

task clean {
    subprojects.each {
        it.afterEvaluate {
            def cleanTask = it.tasks.findByName('clean')
            if (cleanTask) {
                dependsOn(cleanTask)
            }
        }
    }
}

task cleanEclipse(dependsOn: clean) {
    subprojects.each {
        it.afterEvaluate {
            def cleanTask = it.tasks.findByName('cleanEclipse')
            if (cleanTask) {
                dependsOn(cleanTask)
            }
        }
    }
}

task assemble(dependsOn: clean) {
    subprojects.each {
        it.afterEvaluate {
            def cleanTask = it.tasks.findByName('assemble')
            if (cleanTask) {
                dependsOn(cleanTask)
            }
        }
    }
}

assemble.mustRunAfter cleanEclipse

task eclipse(dependsOn: ['assemble', 'cleanEclipse']) {
    subprojects.each {
        it.afterEvaluate {
            def cleanTask = it.tasks.findByName('eclipse')
            if (cleanTask) {
                dependsOn(cleanTask)
            }
        }
    }
}

That’s because there a small typo
it’s
rootProject.clean.dependsOn tasks.matching { it.name == “clean” }
or
rootProject.clean{ dependsOn tasks.matching { it.name == “clean” } }