无法< Plugin>在根目录build.gradle.kts中使用apply()

nimxete2  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(271)

我使用复合构建来管理我的插件。在同一个复合构建中,我有如下所示的DetektConventionPlugin插件,我希望使用以下模式应用它:apply<DetektConventionPlugin>()。然而,这不适用于复合构建,但适用于buildSrc,但我不希望继续使用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")
            }
        }
    }
}

字符串
我不知道我在这里做错了什么。看起来buildSrc处理插件的方式不同。

wz1wpwve

wz1wpwve1#

杰里·奥卡福
希望你一切都好!我也希望这对你有帮助!
您必须在Gradle上注册您的插件。
在这个例子中,我使用了KotlinDSL。

// 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"
            }
        }
    }

字符串
比你可以调用你的插件的ID在任何文件

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


我有一个项目,我在我的Github中使用KotlinDSL的插件。

*项目=https://github.com/tavieto/movie-library-android
*插件目录= https://github.com/tavieto/movie-library-android/blob/main/build-logic/convention/src/main/java/AndroidJetpackCompose.kt
*插件注册= https://github.com/tavieto/movie-library-android/blob/main/build-logic/convention/build.gradle.kts
*插件使用率= https://github.com/tavieto/movie-library-android/blob/main/feature/main/build.gradle.kts

相关问题