'How to add new sourceset with gradle kotlin-dsl

I want to add a sourceset src/gen/java. With groovy this is rather easy and already described in https://discuss.gradle.org/t/how-to-use-gradle-with-generated-sources/9401/5

sourceSets {
   gen {
        java.srcDir "src/gen/java"
    }
}

But I stuck with the kotlin-dsl to add a new one. All I've got is:

java {
    sourceSets {

    }
}

Can anyone help here to



Solution 1:[1]

You should try the following:

java.sourceSets.create("src/gen/java")

Hope it's what you need!

Solution 2:[2]

The answer of @s1m0nw1 is correct to add a new sourceset. But to just add a new source-folder in an existing sourceset, this can be used:

java.sourceSets["main"].java {
    srcDir("src/gen/java")
}

Solution 3:[3]

Worked for me on Gradle 4.10.2:

sourceSets.create("integrationTest") {
    java.srcDir("src/integrationTest/java")
    java.srcDir("build/generated/source/apt/integrationTest")
    resources.srcDir("src/integrationTest/resources")
}

Solution 4:[4]

Worked for me on Gradle 4.10.2:

sourceSets.getByName("main") {
    java.srcDir("src/main/java")
    java.srcDir("src/main/kotlin")
}
sourceSets.getByName("test") {
    java.srcDir("src/test/java")
    java.srcDir("src/test/kotlin")
}

The codes above can also be used in subprojects block.

Solution 5:[5]

I wanted to add a source set with the name "test-integration" and the source directory src/test-integration/kotlin. I was able to accomplish that by combining the two pre-existing answers:

java.sourceSets.create("test-integration").java {
    srcDir("src/test-integration/kotlin")
}

Solution 6:[6]

kotlin-dsl

sourceSets {
        this.getByName("androidTest"){
            //Adds the given source directory to this set.
            this.java.srcDir("src/mock/java")
        }
        this.getByName("test"){
            this.java.srcDir("src/mock/java")
        }
    }

Solution 7:[7]

This is what I had before:

main.kotlin.srcDirs = main.java.srcDirs = ['src']
test.kotlin.srcDirs = test.java.srcDirs = ['test']
main.resources.srcDirs = ['resources']
test.resources.srcDirs = ['testresources']

Above now translates to:

sourceSets {
main {
    java {
        srcDirs("src")
    }
    resources {
        srcDirs("resources")
    }
}
test {
    java {
        srcDirs("test")
    }
    resources {
        srcDirs("testresources")
    }
}}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 guenhter
Solution 3 Andrej Urvantsev
Solution 4 joeaniu
Solution 5 CorayThan
Solution 6 Abhijith mogaveera
Solution 7 Dharman