I am trying to update an older custom plugin to Gradle 7.4, with the goal to simplify it at the same time.
The plugin was written with several classes derived from RuleSource.
On the page Rule based model configuration I found this comment:
“Rule based configuration will be deprecated. New plugins should not use this concept.”
I am not very familiar with Gradle, even less with writing plugins myself. So I was wondering what I should use as a replacement for rules. Can anybody give some general directions what type of construct should be used with 7.4 instead of rules?
The preferred language would be Kotlin.
Here is an example of an existing rule that I want to replace with a future-proof solution based on 7.4 technology:
import org.gradle.api.Task
import org.gradle.api.plugins.Convention
import org.gradle.api.plugins.ExtensionContainer
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.model.ModelMap
import org.gradle.model.Mutate
import org.gradle.model.Path
import org.gradle.model.RuleSource
import org.gradle.testing.jacoco.tasks.JacocoReport
public class JacocoTestReportRule extends RuleSource {
@Mutate
void configureJacocoTestReportTasks(@Path("tasks") ModelMap<Task> tasks, ExtensionContainer extensions) {
JavaPluginConvention javaPluginConvention = ((Convention) extensions).getPlugin(JavaPluginConvention.class)
tasks.jacocoTestReport.configure {
reports {
xml.enabled false
csv.enabled false
html.destination new File("${task.project.buildDir.path}/jacocoHtml")
}
}
tasks.jacocoTestReport.dependsOn tasks.test
if (tasks.get("integrationTest")) {
tasks.create("jacocoIntegrationTestReport", JacocoReport.class) {
description = 'Generates code coverage report for the integrationTest task.'
group = 'verification'
}
tasks.jacocoIntegrationTestReport.configure {
sourceSets javaPluginConvention.sourceSets.main
executionData tasks.integrationTest
reports {
xml.enabled = false
csv.enabled = false
html.destination new File("${task.project.buildDir.path}/jacocoHtml")
}
}
tasks.jacocoIntegrationTestReport.dependsOn tasks.integrationTest
}
}
}
I might add that I am not sure whether there was a compelling reason to use rules to implement the above in the first place. Maybe there would have been a simpler way to begin with. The developer who wrote the custom plugin also started pretty much with zero knowledge (and has left the team since).