Add sources generated by a plugin to my project

Hi. This forum was a great help so far, so I have a hope :slight_smile:

I’m trying to write a basic plugin that will perform code generation for my kotlin project.

Here’s the code:

private open class GenerateFromJsonTask : DefaultTask() {
    fun generate() {
        CodeGenerator().apply {
            baseDirectoryName = "${project.layout.buildDirectory.get()}/generated/fromSchema"
            basePackageName = "com.example"

open class GenerateFromJsonPlugin : Plugin<Project> {
    override fun apply(project: Project) {
        val genCodeTask = project.tasks.create("generateCode",


And it does generate the code. My problem is: I need to add the generated code directory as a source to my project so I could use it.
But how to do it? I’ve tried all the variants of sourceSets for JavaPlugin and Kotlin*Plugins, but it didn’t work.

Please direct me to the right doc or help with a code snippet.

this is my buildSrc/build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {

gradlePlugin {
    plugins {
        register("json-schema-codegen") {
            id = "json-schema-codegen"
            implementationClass = "com.example.gradle.GenerateFromJsonPlugin"

version = "0.0.1-SNAPSHOT"

repositories {

dependencies {
    implementation(kotlin("gradle-plugin", "1.9.23"))

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs += "-Xjsr305=strict"

upd: in the project that uses the plugin it can be done this way:

sourceSets.named("main").configure {

but I’m looking for a way to do it from the plugin, so no additional configuration (other than the plugin configuration) is needed

What hinders you to do the exact same thing in the plugin?

Besides that, you should not configure paths.
User srcDir(theGenerationTask) and make sure the task properly declares its input and outputs.
Then the outputs are considered source files and you automatically have the necessary task dependency from all tasks consuming sources to your generation task.