Creating common task for all subprojects

Hi,

How to create a common task in root build.gradle which can be executed for all modules.

Ex:

root/build.gradle

task findbugs(type: FindBugs) {
ignoreFailures = true
effort = “max"
reportLevel = “low"
excludeFilter = new File(”${rootDir}/…/Tools/findBugs/exclude.xml”)
classes = files("${project.buildDir}/intermediates/classes/debug")
source 'src’
include “/*.java"
exclude "
/gen/**”
reports {
xml.enabled = true
xml.withMessages = true
html.enabled = false
xml {
destination “$project.buildDir/reports/${project.name}.xml”
}
}
classpath = files()
}

task findbugsHtml << {
if (file("${buildDir}/reports/${project.name}.xml").exists()) {
ant.xslt(in: “${buildDir}/reports/${project.name}.xml”,
style:"${rootDir}/findbugs.xsl",
out:"${buildDir}/reports/${project.name}.html"
)
}
}

To run it for App module

gradle clean :App:findbugs

Just wrap your definitions in allprojects (or subprojects), they will be executed in the context of every project then.

subprojects {
    task sayHello {
        doLast { println "Hello from ${project.name}" }
    }
}

// make "delegation to all" task on root project
task sayAllHello
subprojects { pr -> sayAllHello.dependsOn("${pr.path}:sayHello") }
1 Like

Hi

I have already kept the definition in subproject{}
but, I’m getting below error while running gradle clean

A problem occurred configuring project ‘:App’.

Failed to notify project evaluation listener.
Could not find method findbugs() for arguments [{type=class org.gradle.api.plugins.quality.FindBugs}, build_d7dwedg7sz2ompeyuks09tmxx$_run_closure1_closure5_closure14_closure17@113ebb2f] on project ‘:App’.

Oh, It was a silly mistake
Works well now.
Thanks a lot