diff --git a/findbugs-init.groovy b/findbugs-init.groovy index 8f9e93c..00a5339 100644 --- a/findbugs-init.groovy +++ b/findbugs-init.groovy @@ -20,7 +20,16 @@ gradle.projectsEvaluated { } } -//Findbugs task +/** + * Findbugs task. + * @deprecated + * Deprecated in favour of spotbugs, see + * https://github.com/gradle/gradle/issues/7028. + * Will be removed when latest Android Gradle plug-in requires a + * version of Gradle that does not support findbugs. + * @param project Project to which a findbugs task will be added. + */ +@Deprecated void addFindBugsTask(final Project project) { project.apply plugin:FINDBUGS project.tasks.create([name:FINDBUGS, diff --git a/spotbugs-init.groovy b/spotbugs-init.groovy new file mode 100644 index 0000000..e6075fd --- /dev/null +++ b/spotbugs-init.groovy @@ -0,0 +1,86 @@ +import org.gradle.api.Project +import org.gradle.api.file.FileCollection + +ext.SPOTBUGS = 'spotbugs' +rootProject { + buildscript { + repositories { + maven { + url 'https://plugins.gradle.org/m2/' + } + } + dependencies { + classpath 'com.github.spotbugs:spotbugs-gradle-plugin:3.0.0' + } + } +} + +gradle.projectsEvaluated { + ext.applySpotBugs = { + if (project.hasProperty('android') && + !project.hasProperty(SPOTBUGS)) { + addSpotBugsTask project + } + } + if (rootProject.subprojects.isEmpty()) { + rootProject applySpotBugs + } else { + rootProject.subprojects applySpotBugs + } +} + +//Spotbugs task +void addSpotBugsTask(final Project project) { + project.apply plugin:'com.github.spotbugs' + project.sourceSets { + main { + java.srcDirs = ['main', 'androidTest', 'test'].collect { + project.android.sourceSets.findByName(it) + }.find { null != it }.collect { it.java.srcDirs } + } + } + project.tasks.matching { it.name.startsWith(SPOTBUGS) }.each { + it.with { + // Find excludes filter + final String CONFIG_NAME = 'findbugs-filter.xml' + if (rootProject.file(CONFIG_NAME).exists()) { + excludeFilter rootProject.file(CONFIG_NAME) + } else { + for (final File dir : startParameter.initScripts) { + if (new File(dir.parentFile, CONFIG_NAME).exists()) { + excludeFilter new File(dir.parentFile, CONFIG_NAME) + break + } + } + } + //excludeFilter script.file(CONFIG_NAME) + classes = getDebugSources(project) + classpath = project.configurations.compile + files(project.android.bootClasspath) + effort = 'max' + reportLevel = 'low' + reports { + // html.enabled = true + xml { + // enabled = false + withMessages = true + } + } + ignoreFailures = true // Don't report error if there are bugs found. + } + } +} + +FileCollection getDebugSources(final Project project) { + FileCollection classes = project.files() + for (final String variantType : ['applicationVariants', 'libraryVariants', 'testVariants']) { + if (project.android.hasProperty(variantType)) { + project.android."${variantType}".all { variant -> + if (variant.buildType.name == 'debug') { + classes += files("${variant.javaCompileProvider.get().destinationDir}") + } + } + } + } + classes +} + diff --git a/static-checker-init.groovy b/static-checker-init.groovy index 4a5df45..b6c83c3 100644 --- a/static-checker-init.groovy +++ b/static-checker-init.groovy @@ -1,6 +1,6 @@ apply from:'checkstyle-init.groovy' apply from:'detekt-init.groovy' -apply from:'findbugs-init.groovy' apply from:'ktlint-init.groovy' apply from:'pmd-init.groovy' apply from:'sonar-init.groovy' +apply from:'spotbugs-init.groovy'