I am generating some Kotlin code using KotlinPoet....
# build-tools
d
I am generating some Kotlin code using KotlinPoet. How would I best mark the target directory as a code-source using Gradle? The following works, but marks the directory as a regular source root in IntelliJ, instead of "Generated Sources Root"
Copy code
sourceSets {
    main.kotlin.srcDirs += 'generated/kotlin'
}
u
Maybe using the
idea
plugin like this (but I haven't tried this and don't really know if it works)?
Copy code
apply plugin: 'idea'

idea {
    module {
        generatedSourceDirs += file('generated/kotlin')
    }
}
From https://docs.gradle.org/current/dsl/org.gradle.plugins.ide.idea.model.IdeaModule.html
d
Wouldn't that only add the directly to IDEA? And not add it as a source for gradle?
u
Right. Looks like I misunderstood what you’re looking for :(
d
No worries, all good.
a
What if you mix both options?
v
hey, did you figure this one out btw?
d
I took a break from this project for a while and no, I did not. I am using kapt at the moment which makes this problem go away, but surfaces others.
v
Are you doing the annotation processing or some other type of code generation? I’m looking into non-annotation processing code generation atm (the input for generator is a json model) and I can’t quite figure out if kapt would work for me in this scenario.
d
I am doing annotation processing. I don't think you need kapt if you don't need access to the existing source code. Just write a normal kotlin program using Kotlin Poet.
v
Yeah, that’s what I’m doing, the problem I’m trying to solve now is delivery i.e. I want clients of the library to add dependency on generator do something like
Copy code
generator {
  model_path "/path/to/model"
  target_package "'org.package"
}
in their gradle config and get the model processed properly, code generated and put into the generated source/etc. So I want gradle plugin of kapt and not the annotation preprocessing 🙂 Otherwise I’d need to write my own gradle plugin or something like that, as I understand..
d
Yeah, you'd need to write your own gradle plugin.
🙁 1
v
didn’t manage to make it work for Kotlin code 😞
207 Views