From 10cec19d35d4884e34e2f8dfb5322325fff458e7 Mon Sep 17 00:00:00 2001 From: Sarah Haggarty Date: Wed, 19 Nov 2025 12:03:48 +0100 Subject: [PATCH 1/3] feat: register generated sources in Gradle --- .../topics/gradle/gradle-configure-project.md | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/topics/gradle/gradle-configure-project.md b/docs/topics/gradle/gradle-configure-project.md index ffe918a1eb3..2ac1efa4e6d 100644 --- a/docs/topics/gradle/gradle-configure-project.md +++ b/docs/topics/gradle/gradle-configure-project.md @@ -1367,6 +1367,41 @@ dependencyResolutionManagement { Any declared repositories in subprojects override repositories declared centrally. For more information on how to control this behavior and what options are available, see [Gradle's documentation](https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:centralized-repository-declaration). +## Register generated sources + + +To help IDEs, third-party plugins, or other tools distinguish between generated code and regular source files, register +the generated sources using the [`KotlinSourceSet`](https://kotlinlang.org/api/kotlin-gradle-plugin/kotlin-gradle-plugin-api/org.jetbrains.kotlin.gradle.plugin/-kotlin-source-set/) interface. + +To register a directory that contains Kotlin or Java files, use the `generatedKotlin` property with the [`SourceDirectorySet`](https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.file/-source-directory-set/index.html) +type in your `build.gradle(.kts)` file. For example: + +```kotlin +val generatorTask = project.tasks.register("generator") { + val outputDirectory = project.layout.projectDirectory.dir("src/main/kotlinGen") + outputs.dir(outputDirectory) + doLast { + outputDirectory.file("generated.kt").asFile.writeText( + // language=kotlin + """ + fun printHello() { + println("hello") + } + """.trimIndent() + ) + } +} + +kotlin.sourceSets.getByName("main").generatedKotlin.srcDir(generatorTask) +``` + +This example creates a new task `"generator"` with an output directory of `"src/main/kotlinGen"`. When the task runs, +the `doLast {}` block creates a `generated.kt` file in the output directory. Finally, the example registers the task's +output as a generated source. + +The `allKotlinSources` property provides access to all sources registered in the `KotlinSourceSet.kotlin` and +`KotlinSourceSet.generatedKotlin` properties. + ## What's next? Learn more about: From df88a0dfe8323a7821654841ee7b9aecbebbd3b6 Mon Sep 17 00:00:00 2001 From: Sarah Haggarty Date: Mon, 24 Nov 2025 10:52:42 +0100 Subject: [PATCH 2/3] Yahor review --- docs/topics/gradle/gradle-configure-project.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/topics/gradle/gradle-configure-project.md b/docs/topics/gradle/gradle-configure-project.md index 2ac1efa4e6d..2cd851ad778 100644 --- a/docs/topics/gradle/gradle-configure-project.md +++ b/docs/topics/gradle/gradle-configure-project.md @@ -1373,7 +1373,7 @@ this behavior and what options are available, see [Gradle's documentation](https To help IDEs, third-party plugins, or other tools distinguish between generated code and regular source files, register the generated sources using the [`KotlinSourceSet`](https://kotlinlang.org/api/kotlin-gradle-plugin/kotlin-gradle-plugin-api/org.jetbrains.kotlin.gradle.plugin/-kotlin-source-set/) interface. -To register a directory that contains Kotlin or Java files, use the `generatedKotlin` property with the [`SourceDirectorySet`](https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.file/-source-directory-set/index.html) +To register a directory that contains Kotlin files, use the `generatedKotlin` property with the [`SourceDirectorySet`](https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.file/-source-directory-set/index.html) type in your `build.gradle(.kts)` file. For example: ```kotlin @@ -1396,7 +1396,7 @@ kotlin.sourceSets.getByName("main").generatedKotlin.srcDir(generatorTask) ``` This example creates a new task `"generator"` with an output directory of `"src/main/kotlinGen"`. When the task runs, -the `doLast {}` block creates a `generated.kt` file in the output directory. Finally, the example registers the task's +the `doLast {}` task action creates a `generated.kt` file in the output directory. Finally, the example registers the task's output as a generated source. The `allKotlinSources` property provides access to all sources registered in the `KotlinSourceSet.kotlin` and From 5e60741b1d2bde83842733770fd21218bbb6c446 Mon Sep 17 00:00:00 2001 From: Sarah Haggarty Date: Tue, 25 Nov 2025 17:34:55 +0100 Subject: [PATCH 3/3] Andrey review part 1 --- docs/topics/gradle/gradle-configure-project.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/topics/gradle/gradle-configure-project.md b/docs/topics/gradle/gradle-configure-project.md index 2cd851ad778..1facb53ba1a 100644 --- a/docs/topics/gradle/gradle-configure-project.md +++ b/docs/topics/gradle/gradle-configure-project.md @@ -1370,11 +1370,12 @@ this behavior and what options are available, see [Gradle's documentation](https ## Register generated sources -To help IDEs, third-party plugins, or other tools distinguish between generated code and regular source files, register -the generated sources using the [`KotlinSourceSet`](https://kotlinlang.org/api/kotlin-gradle-plugin/kotlin-gradle-plugin-api/org.jetbrains.kotlin.gradle.plugin/-kotlin-source-set/) interface. +Register generated sources to help IDEs, third-party plugins, and other tools distinguish between generated code and regular source files. +This helps tools like IDEs highlight generated code differently in the UI and trigger generation tasks when importing the project. +Use the [`KotlinSourceSet`](https://kotlinlang.org/api/kotlin-gradle-plugin/kotlin-gradle-plugin-api/org.jetbrains.kotlin.gradle.plugin/-kotlin-source-set/) interface to register generated sources. To register a directory that contains Kotlin files, use the `generatedKotlin` property with the [`SourceDirectorySet`](https://docs.gradle.org/current/kotlin-dsl/gradle/org.gradle.api.file/-source-directory-set/index.html) -type in your `build.gradle(.kts)` file. For example: +type in your `build.gradle.kts` file. For example: ```kotlin val generatorTask = project.tasks.register("generator") { @@ -1395,7 +1396,7 @@ val generatorTask = project.tasks.register("generator") { kotlin.sourceSets.getByName("main").generatedKotlin.srcDir(generatorTask) ``` -This example creates a new task `"generator"` with an output directory of `"src/main/kotlinGen"`. When the task runs, +This example creates a new task `generator` with an output directory of `"src/main/kotlinGen"`. When the task runs, the `doLast {}` task action creates a `generated.kt` file in the output directory. Finally, the example registers the task's output as a generated source.