How can i provide a default value for sourceSets in a custom plugin that can be overridden of the plugin user

I wrote a custom gradle plugin in groovy that should internal apply the “checkstyle” and “pmd” plugin. A user of this so called CodeChecker plugin can execute “gradle check” and gets all checks defined in the plugin (pmd and checkstyle).

However, when the user of the plugin wants to configure a specific sourceSet only for the checkstyle by adding the following lines into his script

checkstyle {
 sourceSets = [sourceSets.main]

the following error message appears: “Could not find property ‘main’ on source set ‘main’.”

Can someone tell me how to make this work? How can I provide a default value in a custom plugin that can be overridden?

class CodeChecker implements Plugin<Project> {
 void apply(Project project) {
  def shouldIgnoreFailures = true
  def includedSourceSets = [project.sourceSets.main, project.sourceSets.test]
    def checkstyle = project.plugins.apply('checkstyle')
  def pmd = project.plugins.apply('pmd')
    File checkstyleFile = new File(project.rootDir.path + "/checkstyle.xml")
  File pmdFile = new File(project.rootDir.path + "/pmd.xml")
    project.checkstyle {
   configFile = project.file(checkstyleFile)
   ignoreFailures = shouldIgnoreFailures
   sourceSets = includedSourceSets
  project.pmd {
   ruleSetFiles = project.files(pmdFile)
   ignoreFailures = shouldIgnoreFailures
   sourceSets = includedSourceSets

this post may be of use: