41

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

guenhter
  • 11,255
  • 3
  • 35
  • 66

9 Answers9

30

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")
}
guenhter
  • 11,255
  • 3
  • 35
  • 66
  • 2
    This worked for me too. How did you find this answer? Where is it documented? – Rahul Khimasia May 09 '18 at 13:00
  • 2
    By playing around after the answer from @s1m0nw1 – guenhter May 09 '18 at 13:01
  • 1
    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. – Rahul Khimasia May 29 '18 at 14:20
  • 2
    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") }` – Naruto Sempai Apr 17 '19 at 02:09
  • sourceSets["main"].java { srcDir("$buildDir/generated/openapi/src/main/java/com/globalfinancingsolutions/network") } worked for me – MohammedYakub M. Aug 30 '23 at 14:45
20

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.

joeaniu
  • 319
  • 2
  • 2
19

You should try the following:

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

Hope it's what you need!

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • Thx. Seems not to work. It seems to be a valid but sources are not compiled. – guenhter Sep 27 '17 at 05:22
  • 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` sourceset – guenhter Sep 27 '17 at 06:27
  • On Gradle 6, can't use this anymore, java.sourceSets no longer exists – Vincent Paing Sep 15 '20 at 08:16
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")
}
Andrej Urvantsev
  • 466
  • 4
  • 15
  • 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")`? – t3chb0t Jan 11 '21 at 08:51
9

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")
        }
    }
Abhijith mogaveera
  • 918
  • 10
  • 16
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")
}
CorayThan
  • 17,174
  • 28
  • 113
  • 161
2

As of Gradle 7.5:

sourceSets {
    main {
        java.sourceSets {
            create("gen"){
                java.srcDir("src/gen/java")
            }
        }
    }
}
Max Farsikov
  • 2,451
  • 19
  • 25
1

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")
    }
}}
Dharman
  • 30,962
  • 25
  • 85
  • 135
razalghul
  • 115
  • 2
  • 4
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)
    }
}
philoopher97
  • 772
  • 1
  • 6
  • 18