美文网首页Vert.xJava
gradle 在Idea 中生成Vert.x 中vertx-se

gradle 在Idea 中生成Vert.x 中vertx-se

作者: 肯弩立尔斯基 | 来源:发表于2019-10-15 16:34 被阅读0次

学Vert.x 有一段时间了,在vertx-service-proxy 中总是卡住,反复了好几次,今天总算是掌握了,在此分享一下。

最初一直不得要领,是因为对java 的annotation processor 不了解,所以一直很困惑。查询大量资料后,明白了这是用来自动生成代码的。

官方文档给了maven 的插件配置,使用起来很方便。

  1. 首先是jar 依赖
<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-service-proxy</artifactId> 
</dependency>
<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-codegen</artifactId>
  <scope>provided</scope>
</dependency>
  1. plugin 依赖
<!-- 生成文件 -->
<plugin>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.5.1</version>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <useIncrementalCompilation>false</useIncrementalCompilation>
    <annotationProcessors><annotationProcessor>io.vertx.codegen.CodeGenProcessor</annotationProcessor>
    </annotationProcessors>
    <!-- 生成java 文件所在目录 -->
    <generatedSourcesDirectory>${project.basedir}/src/main/generated</generatedSourcesDirectory>
    <compilerArgs>
      <arg>-AoutputDirectory=${project.basedir}/src/main</arg>
    </compilerArgs>
  </configuration>
</plugin>

<!-- 删除生成的文件 -->
<plugin>
  <artifactId>maven-clean-plugin</artifactId>
  <version>3.0.0</version>
  <configuration>
    <filesets>
      <fileset>
        <directory>${project.basedir}/src/main/generated</directory>
      </fileset>
    </filesets>
  </configuration>
</plugin>
  1. 编写自己的服务接口
@VertxGen
public interface DatabaseService {
  
  @Fluent
  DatabaseService save(String data, Handler<AsyncResult<Void>> resultHandler);

}

执行后会抛出异常:

src\main\java\org\acui\DBService.java:7: ����: Could not generate model for org.acui.DBService: Declaration annotated with @VertxGen must be under a package annotatedwith @ModuleGen. Check that the package 'org.acui.DBService' or a parent package contains a 'package-info.java' using the @ModuleGen annotation
public interface DBService {
       ^
1 ������

需要创建package-info.java

// groupPackage  最好和服务接口package 一致
@ModuleGen(name = "proxy", groupPackage = "org.acui")
package org.acui;

import io.vertx.codegen.annotations.ModuleGen;

之后执行compileJava,会生成相关的Proxy 代码。

以上都很顺利,不过我换成gradle 后,按照官方文档所说Idea 支持自动annotation processor,网上也有很多配置方式,但我使用后却完全没有效果(如果有大神知道原因,求教!)。反复折腾了很长时间最终放弃了,后来发现gradle 有相关的插件io.dotinc.vertx-codegen-plugin,大家可以参考插件介绍文档。

我最终没有选择使用插件,是因为插件不支持最新的vert.x 版本,接下来是我自己在gradle 中进行的配置。

// 添加依赖
compile("io.vertx:vertx-codegen:3.8.2:processor")
compile("io.vertx:vertx-service-proxy:3.8.2")

// clean 时删除生成的文件
tasks.withType<Delete> {
    doFirst {
        delete(File("$projectDir/src/main/generated"))
    }
}

// 编译时使用annotation processor 处理
tasks.withType<JavaCompile> {
    options.annotationProcessorGeneratedSourcesDirectory = File("$projectDir/src/main/generated")
    // 指定注解处理器的位置为classpath
    options.annotationProcessorPath = this.classpath
}

执行compileJava,生成相关Proxy 代码!

相关文章

网友评论

    本文标题:gradle 在Idea 中生成Vert.x 中vertx-se

    本文链接:https://www.haomeiwen.com/subject/enesmctx.html