How do you update the kotlin srcDir with a propert...
# gradle
h
How do you update the kotlin srcDir with a property? I want to allow the user of this task to update outputFolder, which should update kotlin.srcDir too. IntelliJ marks the initial folder as source folder, but the folder isn't added to the compiler as input.
Copy code
@get:OutputDirectory
abstract val outputFolder: DirectoryProperty

init {
  outputFolder.convention(project.layout.buildDirectory.dir("generated/foo"))
  
  project.plugins.withId("org.jetbrains.kotlin.jvm") {
    val srcSet = project.extensions.findByType(SourceSetContainer::class.java)!!.getByName("main")
     val kotlin = srcSet.extensions.getByName("kotlin") as SourceDirectorySet
     kotlin.srcDir(outputFolder.dir("kotlin"))
  }
}
t
FYI: There was some related changes in 1.7.20 Answering your question - try:
Copy code
val kotlinExtension = project.extensions.getByName("kotlin") as KotlinProjectExtension

kotlinExtension.sourceSets.named("main") {
    kotlin.srcDir(...)
}
h
Should it work when the task is defined in a custom Gradle plugin? I guess, my gradle does not use 1.7.20, but 1.6.21 (embedded version from Gradle). Sure, the consumer of the plugin uses 1.7.20-Beta:
Copy code
@Test fun srcDir() {
        val tmp = Files.createTempDirectory("testing").toFile().apply {
            deleteOnExit()
        }
        val buildFile = File(tmp, "build.gradle.kts")
        buildFile.writeText(
            """
            plugins {
                kotlin("jvm") version "1.7.20-Beta" 
                id("my.plugin")
            }

            repositories {
                mavenCentral()
            }
        """.trimIndent()
        )

        val kotlinSrc = (tmp.toPath() / "src" / "main" / "kotlin" / "hello").toFile()
        kotlinSrc.mkdirs()
        File(kotlinSrc, "Test.kt").writeText(
            """
            package hello
            
            fun foo() {
              main()
            }
        """.trimIndent()
        )

        val result = GradleRunner.create()
            .withProjectDir(tmp)
            .withArguments("myCodeGeneratorTask", "assemble", "--stacktrace")
            .forwardOutput()
            .withPluginClasspath()
            .build()

        assertEquals(TaskOutcome.SUCCESS, result.task(":myCodeGeneratorTask")?.outcome)
        assertEquals(TaskOutcome.SUCCESS, result.task(":assemble")?.outcome)
    }
t
Should work and on pre kotlin 1.7.20 versions
h
Thanks, I guess I found my error:
Copy code
plugins {
    `java-gradle-plugin`
    id("com.github.johnrengelman.shadow")
}

gradlePlugin {
    plugins {
        create("myPlugin") {
            id = "my.plugin"
            implementationClass = "app.softwork.MyGradlePlugin"
        }
    }
}

dependencies {
    compileOnly("org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.7.20-Beta")

    testImplementation(kotlin("test"))
    testImplementation(gradleTestKit())
}
If I switch to
implementation
, it works. But does it work, if my consumer uses an older Kotlin plugin?
t
write tests 🙂
h
I did 😄 Had some troubles with shadowing, but I found the error: "compileKotlin" needs to depend on this new task because the srcDir is not persistently set but only when executing the custom task. Executing "myTask" and then "compileKotlin" does not work.