2

I have a custom gradle task to run detekt only on files passed as argument.

tasks.register("detektCustom", io.gitlab.arturbosch.detekt.Detekt.class) { detektTask ->
    detektTask.config.from("$rootDir/app/config/detekt/detekt.yml")
    detektTask.jvmTarget = "1.8"
    detektTask.classpath.setFrom(project.configurations.getByName("detekt"))
    detektTask.reports {
        txt {
            enabled = true
            destination = file("${project.buildDir}/reports/detekt/detekt.txt")
        }
        xml {
            enabled = true
            destination = file("${project.buildDir}/reports/detekt/detekt.xml")
        }
        html {
            enabled = false
            destination = file("${project.buildDir}/reports/detekt/detekt.html")
        }
    }

    if (project.hasProperty("kotlinFiles")) {
        def kotlinFiles = project.property("kotlinFiles")
        def listOfFiles = kotlinFiles.split(",")
        detektTask.source = files(listOfFiles)
    }
}

But the custom detekt rules that uses type resolution doesn't work properly with this gradle task.

I have read that passing proper classpath and jvmTarget should work. Am I missing something in the gradle task above?

Sandesh Baliga
  • 579
  • 5
  • 20

1 Answers1

2

Update:

I found a way to do it. Add this inside the detekt task configuration.

For android modules:

def baseExtension = project.extensions.findByType(com.android.build.gradle.BaseExtension.class)
    def bootClasspath = project.files(baseExtension.bootClasspath)
    baseExtension.applicationVariants.all { variant ->
        if (variant.name.contains("buildFlavorNameHere")) {
            classpath.setFrom(variant.getCompileClasspath(null).filter { it.exists() } + bootClasspath)
        }
    }

For library modules:

def baseExtension = project.extensions.findByType(com.android.build.gradle.BaseExtension.class)
        def bootClasspath = project.files(baseExtension.bootClasspath)
        baseExtension.libraryVariants.all { variant ->
            if (variant.name.contains("buildFlavorNameHere")) {
                classpath.setFrom(variant.getCompileClasspath(null).filter { it.exists() } + bootClasspath)
            }
        }

Only difference is baseExtension.applicationVariants vs baseExtension. libraryVariants

Sandesh Baliga
  • 579
  • 5
  • 20