grpc-整合gradle与代码生成

来源:互联网 发布:喜纳昌吉 知乎 编辑:程序博客网 时间:2024/05/16 19:50

在build.gradle中添加插件:

apply plugin: ‘com.google.protobuf’

buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.1'    }}protobuf {    protoc {        artifact = "com.google.protobuf:protoc:3.2.0"    }    plugins {        grpc {            artifact = 'io.grpc:protoc-gen-grpc-java:1.4.0'        }    }    generateProtoTasks {        all()*.plugins {            grpc {}        }    }}

protobuf plugin for gradle:

  1. 执行protoc命令,将生成你的proto file对应的java 源代码
  2. 添加生成的java源代码到项目中相应的工作空间,能够和其他java代码一起编译

可以通过修改默认配置来自定义proto源文件的目录

sourceSets {  main {    proto {      // In addition to the default 'src/main/proto'      srcDir 'src/main/protobuf'      srcDir 'src/main/protocolbuffers'      // In addition to the default '**/*.proto' (use with caution).      // Using an extension other than 'proto' is NOT recommended,      // because when proto files are published along with class files, we can      // only tell the type of a file from its extension.      include '**/*.protodevel'    }    java {      ...    }  }  test {    proto {      // In addition to the default 'src/test/proto'      srcDir 'src/test/protocolbuffers'    }  }}

配置完成之后执行gradle generateProto,默认会在build下生成proto文件的代码,后续我们会考虑如何将自动生成的文件放置到配置好的项目目录中。

原创粉丝点击