我刚开始使用科林诗人,我一直在阅读文档,它似乎是一个很棒的库,但我找不到解决问题的例子。
我有一个依赖项lib-domain-0.1.jar,其中我有业务对象,例如:
package pe.com.business.domain
data class Person(val id: Int? = null, val name: String? = null)
...
..
package pe.com.business.domain
data class Departament(val id: Int? = null, val direction: String? = null)
...
..
.我希望构建一个名为lib-domain-fx-0-1.jar的新依赖项,其中它具有相同的域,但具有JavaFx属性(例如,使用tornadofx):
package pe.com.business.domainfx
import tornadofx.*
class Person {
val idProperty = SimpleIntegerProperty()
var id by idProperty
val nameProperty = SimpleStringProperty()
var name by nameProperty
}
...
..
package pe.com.business.domainfx
import tornadofx.*
class Departament {
val idProperty = SimpleIntegerProperty()
var id by idProperty
val directionProperty = SimpleStringProperty()
var direction by directionProperty
}
...
..
.我的问题是,如何通过简单地用gradle构建编译我的应用程序来在lib-domain-fx-0-1.jar中生成这些文件?我的项目“lib-domain 0-1.jar”只是一个库,所以它没有主类,所以我不知道从哪里开始生成代码?我已经看过几个例子,它们在同一个项目中使用@Annotations和两个不同的模块,但这不是我所需要的:我需要将lib-domain-0.1.jar的所有类转换为JavaFx版本,并在另一个项目中使用TornadoFX (lib-domain-fx-0.1.jar)。
谢谢和问候。
发布于 2020-05-14 06:24:47
在我看来,KotlinPoet在其文档中缺乏如何将其集成到项目中的任何示例。
正如@Egor所提到的,这个问题本身非常广泛,所以我只回答核心部分:如何在用Gradle构建应用程序时使用KotlinPoet生成代码?
我和自定义分级任务一起做的。
在src/main/java/com/business/package/GenerateCode.kt中有一个应用程序/库/子项目。
package com.business.package
import com.squareup.kotlinpoet.*
fun main() {
// using kotlinpoet here
// in the end wrap everything into FileSpec
val kotlinFile: FileSpec = ...
// and output result to stdout
kotlinFile.writeTo(System.out)
}现在让Gradle创建一个具有输出的文件。添加到build.gradle
task runGenerator(type: JavaExec) {
group = 'kotlinpoet'
classpath = sourceSets.main.runtimeClasspath
main = 'com.business.package.GenerateCodeKt'
// store the output instead of printing to the console:
standardOutput = new ByteArrayOutputStream()
// extension method genSource.output() can be used to obtain the output:
doLast {
ext.generated = standardOutput.toString()
}
}
task saveGeneratedSources(dependsOn: runRatioGenerator) {
group = 'kotlinpoet'
// use build directory
//def outputDir = new File("/${buildDir}/generated-sources")
// or add to existing source files
def outputDir = new File(sourceSets.main.java.srcDirs.first(), "com/business/package")
def outputFile = new File(outputDir, "Generated.kt")
doLast {
if(!outputDir.exists()) {
outputDir.mkdirs()
}
outputFile.text = tasks.runGenerator.generated
}
}在Android / Intellij中,打开分级工具窗口,查找新的组kotlinpoet (没有group,任务将在others部分),并执行任务saveGeneratedSources。
https://stackoverflow.com/questions/53856476
复制相似问题