Gradle 在构建应用程序时,如何使用KotlinPoet生成代码?(格拉德尔)

Gradle 在构建应用程序时,如何使用KotlinPoet生成代码?(格拉德尔),gradle,kotlin,build,kotlinpoet,Gradle,Kotlin,Build,Kotlinpoet,我是新使用kotlinpoet的,我一直在阅读文档,它似乎是一个很棒的库,但我找不到一个例子来解决我的问题 我有一个依赖项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

我是新使用kotlinpoet的,我一直在阅读文档,它似乎是一个很棒的库,但我找不到一个例子来解决我的问题

我有一个依赖项
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)
...
..
.
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
}
...
..
.
我想构建一个名为
lib-domain-fx-0-1.jar的新依赖项,其中它具有相同的域,但具有JavaFx属性(使用tornadofx),例如:

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)
...
..
.
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
}
...
..
.
我的问题是,如何在
lib-domain-fx-0-1.jar
中生成这些文件,只需使用gradle构建编译我的应用程序?我的项目“lib-domain-fx-0-1.jar”只是一个库,所以它没有主类,所以我不知道从哪里开始生成代码?。我看过几个例子,其中他们在同一个项目中使用了
@注释
和两个不同的模块,但这不是我需要的:(.我需要在另一个项目中将
lib-domain-0.1.jar
的所有类转换为JavaFx版本,使用tornafx(
lib-domain-fx-0.1.jar


感谢和问候。

在我看来,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)
}
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
    }
}
现在让Gradle创建一个带有生成输出的文件。添加到build.Gradle

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)
}
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 Studio/Intellij IDEA中打开,找到新的组
kotlinpoet
(如果没有
group
,任务将在
others
部分),然后执行任务
saveGeneratedSources

您的问题非常广泛。您可能需要1)处理输入jar中的类,可能使用反射,2)基于步骤1创建一个中间模型,3)基于模型使用KotlinPoet生成代码,4)编译生成的代码,5)将编译后的代码打包到jar中。开始研究这些问题,并提出更具体的问题(如果有的话)。至于KotlinPoet,它肯定会有助于代码生成,但其他步骤超出了库的范围。感谢您的回复,但是如果我的项目只是一个库,没有主类,我如何执行代码生成?例如,如果在GenerateCode.kt类“FileSpec.builder”(“,“HelloWorld”).AddType(TypeSpec.classBuilder”(“Greeter”)……build()”中有此代码,那么在执行“gradle build”时如何执行该类或者这是不能做到的吗?我想到的是用一个主方法创建一个类,该方法执行类的创建(GenerateCode.kt),但在使用“gradle build”构建应用程序时,我想优化这一步骤。类、方法和所有内容的创建并不复杂,因为有很多关于Kotlinpoet的文档。导致我出现问题的是知道在哪一部分运行类生成(GenerateCode.kt)对于一个像上一个示例一样只是一个库的项目。任何使用android应用程序插件的项目解决方案?Gradle构建在尝试访问runtimeClasspath时停止