How to change kapt plugin generate output dir path?
Asked Answered
C

1

8

I try to use kapt and vertx-Codegen to generate my service.But the output path is /build/generated/source/kapt/main/. I want /src/main/generated/.

Config the build.gradle.kts,I find generate adoc to /src/main/generated/,but other files no change.

my file

There is build.gradle.kts

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

plugins {
  kotlin("jvm") version "1.3.30"
  kotlin("kapt") version "1.3.30"
  id("io.vertx.vertx-plugin") version "0.8.0"
}
val vertxVersion = "3.8.0"
// ......
tasks.withType<KotlinCompile> {
  kotlinOptions {
    freeCompilerArgs = listOf("-Xjsr305=strict")
    jvmTarget = "1.8"
  }
}
dependencies {
  // ....
    implementation("io.vertx:vertx-codegen")
    kapt("io.vertx:vertx-service-proxy:$vertxVersion:processor")
    kapt("io.vertx:vertx-codegen:$vertxVersion:processor")
  // ....
}

kapt{
  arguments {
    arg("codegen.output", project.file("src/main/generated").path)
    arg("kapt.kotlin.generated", project.file("src/main/generated").path)
  }
  javacOptions {
    option("-AoutputDirectory", project.file("src/main/generated").path)
    option("-Acodegen.output", project.file("src/main/generated").path)
  }
}

I expect the output path is /src/main/generated/, but the actual output path is /build/generated/source/kapt/main/. Can you help me...?

Crampon answered 17/8, 2019 at 4:25 Comment(0)
H
1

Generating Kotlin sources:

I managed to solve the problem for generated Kotlin sources by using javac argument:

val generatedKotlinSources = project.file("src/main/generated").path
kapt {
    javacOptions {
        option("-Akapt.kotlin.generated=$generatedKotlinSources")
    }
}

(Surprisingly option("-Akapt.kotlin.generated", generatedKotlinSources) didn't work.)

This solution works with annotation processors using processingEnv.options["kapt.kotlin.generated"], as described on Annotation Processing with Kotlin page.

Generating Java sources:

However, when generating Java sources, annotation processors use Filer initialized with javac context. When using Java Annotation Processor, you can override this path by using -s dir javac argument, but this value is hardcoded in Kapt to "generated/source/kapt/$sourceSetName" (see source code).

Thus, changing output directory for generated Java doesn't seem possible.

(And yes, "generated/source/kaptKotlin/$sourceSetName" is hardcoded too, but it is used as a default value of "kapt.kotlin.generated" option.)

Hodeida answered 3/1, 2021 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.