Programatically defining plugin extension DSL in custom plugin

I am looking to create a custom plugin that configures Jacoco.

In my project build.gradle I apply the jaococ plugin and configure Jacoco (JacocoTaskExtension) for the ‘test’ task:

apply plugin:"jacoco"

test {
    jacoco {
        enabled = true
        output = JacocoTaskExtension.Output.TCP_SERVER

This works fine. Now I want to move this configuration into a stand-alone custom plugin I am developing that can be applied to other projects.

public class MyPlugin implements Plugin<Project> {
    public void apply(Project project) {
        // TODO how do I define the jacoco DSL within the test task before applying the jacoco plugin?
       // i.e. jacoco { enabled = true etc. }

I can’t figure out how to programatically define the jacoco (JacocoTaskExtension) extension DSL for the ‘test’ task. i.e. setting the jacoco.enabled and jacoco.output properties before the jacoco plugin is applied.

I tried updating the JacocoTaskExtension after the jacoco plugin was applied but it seems it’s too late at that point as the JacocoTaskExtension configuration has already generated the jacoco agent JVM args when the jacoco plugin is applied to the task.

Any ideas?


I apply the jacoco plugin and afterwards configure the JacocoReport task, like:


project.tasks.withType(JacocoReport).configureEach { JacocoReport report ->

    report.reports.html.with { DirectoryReport directoryReport ->
        directoryReport.required =
        directoryReport.outputLocation = project.objects.directoryProperty().convention(

I do use this code in my own custom plugin and for me this works.
Maybe you can adopt this.