Groovy + Java + kotlin dsl

Hello,

I use groovy and java together a lot in my projects. In the past when I was using the groovy gradle DSL I had something that looked like this for my sourcesets

sourceSets {
   main {
      groovy.srcDirs += java.srcDirs
      java.srcDirs = []
   }
   groovyTest {
      compileClasspath = test.compileClasspath
      runtimeClasspath = output + test.runtimeClasspath
      groovy {
         srcDir 'src/test/groovy/'
      }
   }
}

I have a strong desire to convert my projects over to using the kotlin dsl, but I can’t quite crack this nut. So far I have something like this, but I think it is wrong. Any advice would be very much appreciated.

sourceSets {
    val groovy = convention.getPlugin(GroovySourceSet::class)
    main {
        groovy.groovy.srcDirs.addAll(java.srcDirs)
        java.srcDirs("")
    }

    getByName("groovyTest") {
        compileClasspath = getByName("test").compileClasspath + getByName("main").compileClasspath
        runtimeClasspath = output + getByName("test").runtimeClasspath + getByName("main").runtimeClasspath
    }
}

The first part is actually covered 1:1 in Gradle Kotlin DSL Primer.

The complete snippet should probably be like this (untested):

sourceSets {
    main {
        withConvention(GroovySourceSet::class) {
            groovy.srcDirs += java.srcDirs
        }
        java.setSrcDirs(emptyList<String>())
    }
    val groovyTest by creating {
        compileClasspath = test.get().compileClasspath
        runtimeClasspath = output + test.get().runtimeClasspath
        withConvention(GroovySourceSet::class) {
            groovy {
                srcDir("src/test/groovy/")
            }
        }
    }
}

This put me on the right path! Thanks!