Create sourceset with dependencies in custom plugin

Hello. I’m writing custom plugin which should create number of sourcesets depends on plugin extension. How can I do it in apply method?

My code doesn’t work in both cases:

labels - list from extension
project.sourceSets {
            labels.each { info ->
                "${info.lower}Src" {
                    java.srcDirs = ['src'] + info.srcPostfix.collect { postfix -> "src_custom/${postfix}" }
                }
            }
              main { java.srcDirs = ['src'] + labels.collect { info -> "src_custom/${info.lower}" } }
            test { java.srcDirs = ['test'] + labels.collect { info -> "test_custom/${info.lower}" } }
        }
labels - list from extension
          labels.each { info ->
            SourceSet modelSrc = project.sourceSets.create("${info.lower}Src")
            modelSrc.getJava().setSrcDirs(['src'] + info.srcPostfix.collect { postfix -> "src_custom/${postfix}" })
        }
        SourceSetContainer sourceSets = project.convention.getPlugin(JavaPluginConvention).sourceSets
        SourceSet mainSourceSet = sourceSets.getByName(MAIN_SOURCE_SET_NAME)
        mainSourceSet.getJava().setSrcDirs(['src'] + labels.collect { info -> "src_custom/${info.lower}" })
          SourceSet testSourceSet = sourceSets.getByName(TEST_SOURCE_SET_NAME)
        testSourceSet.getJava().setSrcDirs(['test'] + labels.collect { info -> "test_custom/${info.lower}" })

What exactly does not work in your code snippets? Can you please provide more information.

As the first thing, I’d make sure that the values from the property “labels” are read correctly. It might be that the extension has not been evaluated yet when you iterate over it’s property values.