来自 Kotlin 的 Vert.x 服务代理和 vertx-codegen

amb*_*b85 2 kotlin vert.x

我在 Kotlin 中编写了一个 vertx 服务接口,我正在尝试为其生成服务代理。但是,除了在 中生成generated目录之外src/main,它什么都不做。

src/main/java/amb85/portfolio/package-info.java

@ModuleGen(name = "portfolio", groupPackage = "amb85.portfolio")
package amb85.portfolio;

import io.vertx.codegen.annotations.ModuleGen;
Run Code Online (Sandbox Code Playgroud)

然后我有以下服务接口src/main/kotlin/amb85/portfolio/PortfolioService.kt

@VertxGen
@ProxyGen
interface PortfolioService {
    companion object {
        val ADDRESS = "service.portfolio"
        val EVENT_ADDRESS = "portfolio"
    }
    fun getPortfolio(resultHandler: (AsyncResult<Portfolio>) -> Unit)
    fun buy(amount: Int, quote: JsonObject, resultHandler: (AsyncResult<Portfolio>) -> Unit)
    fun sell(amount: Int, quote:JsonObject, resultHandler: (AsyncResult<Portfolio>) -> Unit)
    fun evaluate(resultHandler: (AsyncResult<Double>) -> Unit)
}
Run Code Online (Sandbox Code Playgroud)

以及相关配置来自build.gradle

task generateProxies(type: JavaCompile, group: "build",
        description: "Generates the Vert.x proxies") { // codegen
    source = sourceSets.main.java
    source += sourceSets.main.kotlin
    classpath = configurations.compile + configurations.compileOnly
    destinationDir = project.file("${projectDir}/src/main/generated")
    options.compilerArgs = [
            "-proc:only",
            "-processor", "io.vertx.codegen.CodeGenProcessor",
            "-Acodegen.output=${project.projectDir}/src/main"
    ]
}
Run Code Online (Sandbox Code Playgroud)

然后我运行./gradlew portfolio:generateProxies,但没有超出generated目录。

是否可以使用vertx-codegen基于 Kotlin 编写的接口生成服务代理?如果是这样,我缺少哪些配置步骤?如果没有,还有其他方法可以生成代理吗?更好的是,有没有办法完全在 Kotlin 中完成,避免生成 java 或将其用作中间步骤?

Кла*_*арц 5

使用 vertx 服务代理的最简单方法kotlin是使用kapt和 vertx-codegenprocessor分类依赖。

在您build.gradle应该添加以下内容:

apply plugin: 'kotlin-kapt'

dependencies {

    kapt "io.vertx:vertx-codegen:$vertx_version:processor"
    compileOnly "io.vertx:vertx-codegen:$vertx_version"

    // other deps go here
}
Run Code Online (Sandbox Code Playgroud)

到目前为止没有其他需要。