How to add new sourceset with gradle kotlin-dsl
Asked Answered
C

11

47

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

Chiefly answered 26/9, 2017 at 7:5 Comment(0)
U
20

You should try the following:

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

Hope it's what you need!

Unbonnet answered 26/9, 2017 at 19:21 Comment(3)
Thx. Seems not to work. It seems to be a valid but sources are not compiled.Chiefly
Hm, after having the solution for my problem it turns out, that I don't need a new sourceset (your answer is correct to add a new sourceset), but to have a new source-folder within the existing main sourcesetChiefly
On Gradle 6, can't use this anymore, java.sourceSets no longer existsOrphanage
C
34

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")
}
Chiefly answered 27/9, 2017 at 6:24 Comment(5)
This worked for me too. How did you find this answer? Where is it documented?Risky
By playing around after the answer from @s1m0nw1Chiefly
The eclipse kotlin editor and plugin are not complete and they are buggy. They do not provide code completion hints. I have stopped using eclipse and now switched to IntelliJ Idea, which does let me see the internals of Gradle-Kotlin-DSL implementation API so I can discover the API calls available to me. The lack of proper documentation has been frustrating, but now hopefully with IntelliJ's code completion hints; that will not be a big limitation.Risky
I'm using a submodule as a kotlin library in my kotlin project. Leaving off the first part java worked for me. ie sourceSets["main"].java { srcDir("src/main/kotlin") }Concise
sourceSets["main"].java { srcDir("$buildDir/generated/openapi/src/main/java/com/globalfinancingsolutions/network") } worked for mePinchhit
C
22

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.

Crackbrain answered 12/10, 2019 at 11:25 Comment(0)
U
20

You should try the following:

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

Hope it's what you need!

Unbonnet answered 26/9, 2017 at 19:21 Comment(3)
Thx. Seems not to work. It seems to be a valid but sources are not compiled.Chiefly
Hm, after having the solution for my problem it turns out, that I don't need a new sourceset (your answer is correct to add a new sourceset), but to have a new source-folder within the existing main sourcesetChiefly
On Gradle 6, can't use this anymore, java.sourceSets no longer existsOrphanage
A
15

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")
}
Asternal answered 8/10, 2018 at 12:21 Comment(1)
Do you have an idea why I get the error The SourceSet 'libs' is not recognized by the Android Gradle Plugin. when I call create("libs")?Whitley
S
11

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")
        }
    }
Selfcontained answered 26/3, 2022 at 9:7 Comment(0)
K
6

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")
}
Kelm answered 31/5, 2018 at 23:16 Comment(0)
P
4

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")
    }
}}
Pronation answered 21/8, 2021 at 15:28 Comment(0)
S
2

As of Gradle 7.5:

sourceSets {
    main {
        java.sourceSets {
            create("gen"){
                java.srcDir("src/gen/java")
            }
        }
    }
}
Series answered 11/8, 2022 at 21:51 Comment(0)
C
2

When using the "main" build, the Kotlin build.gradle.kts needs the following format to add subfolders for Android layout .xml files (for cleaning up the project folders):

from https://developer.android.com/studio/write/add-resources#kts

//if you add another subfolder in the res/layouts/... folder, you need
//to add it to this section, relative to the build.gradle.kts file:
//so, to add subfolder 'foo', add the line:
//      res.srcDir("/src/main/res/layouts/foo/")
//don't forget to add the "layout" (no 's') as a subfolder within that
//to properly populate the resource lists for any R.layout.[...] call!
sourceSets {
    getByName("main") {
        java {
            res.srcDir("/src/main/res/layouts/xxxx/")
            res.srcDir("/src/main/res/layouts/yyyy/")
            res.srcDir("/src/main/res/layouts/zzzz/")
            res.srcDir("/src/main/res/layouts/aaaa/")
            res.srcDir("/src/main/res/layouts/bbbb/")
            res.srcDir("/src/main/res/layouts/cccc/")
            res.srcDir("/src/main/res/layouts/dddd/")
            res.srcDir("/src/main/res/layouts/eeee/")
            res.srcDir("/src/main/res/layouts/")
            res.srcDir("/src/main/res/")

        }
    }
}
Classis answered 27/3 at 20:27 Comment(0)
D
0

If you're wondering how to do in Jetpack Compose + Jvm application:

plugins {
    kotlin("jvm")
    id("org.jetbrains.compose") version "1.4.1"
}

kotlin {
    sourceSets {
        main {
            kotlin.srcDir("src/jvmMain/kotlin")
            resources.srcDir("src/jvmMain/res")
        }
    }

    dependencies {
        implementation(compose.desktop.currentOs)
    }
}
Downstate answered 13/7, 2023 at 14:20 Comment(0)
D
0

Bets way os to use jvm-test-suite plugin e.g.:


plugins {
    // ..
    `jvm-test-suite`
}

testing {
    suites {
        register("konsistTest", JvmTestSuite::class) {
            dependencies {
                // Add 'main' source set dependency (optional)
                implementation(project())

                // Add Source set specific dependencies
                implementation("com.lemonappdev:konsist:0.11.0")
            }
        }
    }
}

The konsistTest is the name of the new source set. Now just create directory with the same name in IDE:

enter image description here

Disrespectful answered 6/9, 2023 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.