guenhter
guenhter

Reputation: 12177

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

Upvotes: 48

Views: 67183

Answers (12)

RedRainbowGuy
RedRainbowGuy

Reputation: 185

I came across the need to add resources for my openapi generation via the composeApp/build/generate-resources/main/src/commonMain/kotlin path to the composeApp/src/commonMain module.

Everything turned out to be simple:

plugins {
    ...
    id("org.openapi.generator") version "7.10.0"
}

kotlin {
    jvm()

    sourceSets {
        val commonMain by getting {
            kotlin.srcDirs("build/generate-resources/main/src/$name/kotlin")

            dependencies {
                implementation(compose.runtime)
                ...
            }
        }

        val commonTest by getting {...}
    }
}

compose.desktop {...}

openApiGenerate {
    generatorName = "kotlin"
    packageName = group.toString()
    inputSpec = "${projectDir.path}/src/commonMain/resources/api.yaml"
    typeMappings.put("string+date-time", "Instant")
    importMappings.put("Instant", "kotlinx.datetime.Instant")
    additionalProperties.set(
        mapOf(
            "library" to "multiplatform",
            "dateLibrary" to "kotlinx-datetime",
            "enumPropertyNaming" to "UPPERCASE",
            "useCoroutines" to "true",
            "generateApiTests" to "false",
            "generateModelTests" to "false",
            "skipOperationModel" to "true",
            "overwrite" to "true",
        )
    )
}

tasks.withType<KotlinCompile>().all {
    dependsOn("openApiGenerate")
}

project view

Upvotes: 0

Uabaz
Uabaz

Reputation: 21

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/")

        }
    }
}

Upvotes: 2

Igor
Igor

Reputation: 2089

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

Upvotes: 0

Shahriar Zaman
Shahriar Zaman

Reputation: 938

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)
    }
}

Upvotes: 0

Max Farsikov
Max Farsikov

Reputation: 2763

As of Gradle 7.5:

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

Upvotes: 2

Abhijith mogaveera
Abhijith mogaveera

Reputation: 1314

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")
        }
    }

Upvotes: 11

razalghul
razalghul

Reputation: 155

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")
    }
}}

Upvotes: 4

joeaniu
joeaniu

Reputation: 369

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.

Upvotes: 25

Andrej Urvantsev
Andrej Urvantsev

Reputation: 461

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")
}

Upvotes: 15

Nelaina
Nelaina

Reputation: 17845

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")
}

Upvotes: 6

s1m0nw1
s1m0nw1

Reputation: 82117

You should try the following:

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

Hope it's what you need!

Upvotes: 19

guenhter
guenhter

Reputation: 12177

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")
}

Upvotes: 36

Related Questions