如何在自定义Gradle插件中配置Jacoco?

发布于 2025-01-24 18:17:39 字数 731 浏览 1 评论 0原文

我正在编写一个自定义的Gradle插件,该插件应从我的自定义Gradle配置中统一抽象,该配置分布在多个Java项目中。为此,我通常会尝试找到

因此,我到了我想移动到插件。从build.gradle摘录看起来像这样:

jacocoTestReport {
    reports {
        xml.required = true
    }
}

第一部分是可以管理的:检查插件是否已加载。

project.getPlugins().withType(JacocoPlugin.class, jacocoPlugin -> {
  // configure it
})

但是,我坚持如何通过扩展方法实际配置插件。可用的唯一扩展名似乎是jacocopluginextension。从那里开始,我看不到一种方法来添加Reports从build.gradle添加部分。

除了我错过的扩展外,还有其他机制吗?

I am writing a custom Gradle plugin which shall unifiedly abstract from my custom gradle configurations which are spread across multiple Java projects. For this purpose, I generally try to find fitting extensions for the various tasks that need custom configuration.

So, I got to the point where I wanted to move my JaCoCo configuration to the plugin. The excerpt from build.gradle looks like this:

jacocoTestReport {
    reports {
        xml.required = true
    }
}

The first part is manageable: check if the plugin is loaded.

project.getPlugins().withType(JacocoPlugin.class, jacocoPlugin -> {
  // configure it
})

However, I am stuck with how to actually configure the plugin via an extension method. The only extension that is available seems to be JacocoPluginExtension. From there, I don't see a way how to add the reports part from build.gradle.

Is there some other mechanism besides extensions that I missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2025-01-31 18:17:39

“ jacocotestReport”实际上是一项任务,因此您可以像这样进行配置:

    proj.tasks.getByName("jacocoTestReport", {
        reports {
            xml.required = true
        }
    })

如果您想深入挖掘,则是

'jacocoTestReport' is actually a task, so you can configure it like this:

    proj.tasks.getByName("jacocoTestReport", {
        reports {
            xml.required = true
        }
    })

If you'd like to dig deeper, it's class is JacocoReport, which has a
JacocoReportsContainer reports property, so you may use type safe property getters / setters from there, but IMHO the above solution is more like build.gradle, so it's more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文