0

I am using composite build to manage my plugins. In the same composite build, I have DetektConventionPlugin plugin shown below and I wish to apply it using this pattern: apply<DetektConventionPlugin>(). However, this is not working with composite build but it works for buildSrc but I do not wish to continue using buildSrc.


class DetektConventionPlugin : Plugin<Project> {

    override fun apply(target: Project) {
        with(target) {
            val detektVersion = libs.findVersion("detekt").get().toString()
            pluginManager.apply("io.gitlab.arturbosch.detekt")

            extensions.getByType<DetektExtension>().apply {
                toolVersion = detektVersion
                source = files("$rootDir/")
                parallel = true
                config = files("$rootDir/config/detekt/detekt-config.yml")
                buildUponDefaultConfig = true
                allRules = false
                baseline = file("$rootDir/config/detekt/detekt-baseline.xml")
                // dependencies {
                //     "detektPlugins"(libs.findLibrary("detekt-formatting").get())
                // }a
            }

            tasks.named<Detekt>("detekt").configure {
                description = "Runs Detekt on the whole project at once."
                reports {
                    html.required.set(true)
                    html.outputLocation.set(
                        file("$rootDir/build/reports/detekt.html"),
                    )
                }
                parallel = true
                setSource(projectDir)
                include("**/*.kt", "**/*.kts")
                exclude("**/resources/**", "**/build/**")

                // exclude other custom generated files
            }

            dependencies {
                detekt("io.gitlab.arturbosch.detekt:detekt-cli:$detektVersion")
            }
        }
    }
}

I am not sure what I am doing wrong here. Looks like buildSrc handles plugins differently.

Jerry Okafor
  • 3,710
  • 3
  • 17
  • 26

1 Answers1

0

Jerry Okafor

I hope you're doing well! And I also hope this help!


You have to register your plugins on Gradle.

In this example I'm using Kotlin DSL.

    // 1 - put this in your buildSrc build.gradle.kts root
    gradlePlugin {
        plugins {
            // 2 - give a name to your plugin
            register("myDetektImplementation") {
                // 3 - give a ID to your plugin. You will require your plugin by ID.
                id = "okafor.jerry.detekt"
                // 4 - give a path to your plugin
                // In this sample my plugin file was located at src/main/kotlin
                // So if your plugin class in at some package, you have to describe too.
                implementationClass = "DetektConventionPlugin"
            }
        }
    }

Than you can call your plugin by ID in any file

    plugins {
        id("okafor.jerry.detekt")
    }

I have a project that I use it plugins with Kotlin DSL in my Github.

  • Thanks for your response. Maybe I forgot to state that this approach works well for any other build.gradle.kts that is not the root build.gradle.kts. For detekt and ktlint, I need to apply them to the root and not the module build.gradle.kts. – Jerry Okafor Jul 24 '23 at 20:46