我正在开发一个多模块应用程序,该应用程序使用 Gradle 约定插件来简化其 Gradle 构建脚本。我们有许多插件可以毫无问题地应用。所以基本结构应该是正确的。
我现在正在尝试添加一个插件来设置 Jacoco,但我正在努力弄清楚如何做到这一点。我有一个非常简单的纯 JVM 模块插件,可以按预期工作:
open class JvmJacocoReportPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.run {
pluginManager.apply(JACOCO_PLUGIN)
configureJacoco()
}
}
private fun Project.configureJacoco() {
tasks.withType<JacocoReport>().configureEach {
dependsOn(tasks.withType<Test>())
reports {
xml.required.set(true)
csv.required.set(false)
html.required.set(true)
html.outputLocation.set(layout.buildDirectory.dir(REPORT_DIRECTORY_NAME))
}
}
}
companion object {
const val JACOCO_PLUGIN = "org.gradle.jacoco"
const val REPORT_DIRECTORY_NAME = "jacoco-report"
}
}
我略有不同的 Android 模块插件如下所示:
class JacocoReportPlugin : JvmJacocoReportPlugin() {
override fun apply(target: Project) {
super.apply(target)
target.run {
// if I don't register it for Android modules, the task is not present
target.tasks.register<JacocoReport>("jacocoTestReport") {
dependsOn(tasks.withType<Test>())
}
if (target.isLibrary()) {
target.extensions.getByType<LibraryExtension>().apply {
buildTypes {
getByName("debug") {
enableUnitTestCoverage = true
enableAndroidTestCoverage = false
}
}
}
}
if (target.isApplication()) {
target.extensions.getByType<ApplicationExtension>().apply {
buildTypes {
getByName("debug") {
enableUnitTestCoverage = true
enableAndroidTestCoverage = false
}
}
}
}
}
}
private fun Project.isApplication() = plugins.hasPlugin("com.android.application")
private fun Project.isLibrary() = plugins.hasPlugin("com.android.library")
}
我们有一个用于 Android 库和应用程序的约定插件,以及一个用于纯 JVM 库的插件,这些插件的应用方式如下:
class AndroidLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) = target.run {
(...)
pluginManager.apply(JacocoReportPlugin::class.java)
}
(...)
}
这适用于纯 JVM 模块。生成报告。 但是对于 Android 库或应用程序模块,我可以设法创建任务,但我没有收到任何报告。
关于使用这种插件结构设置 Jacoco 的在线信息非常少,所以我在这里不知所措。 我在这里错过了什么?
你如何设置约定插件?我遇到了麻烦。 一旦我创建了一个构建逻辑目录并在 settings.gradle.kts 中添加了 includeBuild(":build-logic") ,它就会给我一个错误 “包含的构建构建逻辑不存在。”而且我无法构建构建逻辑模块