管理共享协议缓冲区库并使用Gradle进行编译 [英] Managing Shared Protocol Buffer library and using Gradle to Compile

查看:66
本文介绍了管理共享协议缓冲区库并使用Gradle进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用协议缓冲区(gRPC)进行通讯的3个Java应用程序(后端,前端和Android应用程序).因此,我希望这3个应用程序都能访问我管理.proto文件的共享protobuf存储库(Github).我是使用Gradle和protobufs的新手,所以我不确定管理此方法的正确方法是什么,任何帮助或指导都将不胜感激.我可以让每个Gradle项目将我的github protobuf存储库声明为依赖项,然后在构建项目时将其下拉并编译吗?我认为这种方法是一种好方法,而不是存储已编译的protobuf类,因为Android应用程序可能需要protobuf的不同"Java-lite"版本?我正在使用google/protobuf-gradle-plugin来编译.proto文件,并查看用于从本地文件进行编译的文档,或者查看已预编译.proto文件的项目,但是没有用于获取远程.proto文件的文档.我在正确的轨道上吗?

I'd like to have 3 java applications (a backend, a front end, and an Android app) using protocol buffers (gRPC) to communicate. So I would like the 3 apps all to be able to have access to a shared protobuf repo (Github) where I manage the .proto files. I am new to using Gradle and protobufs, so I'm not sure what the proper way to manage this is, and any help or guidance would be appreciated. Can I have each Gradle project declare my github protobuf repo as a dependency, and then pull it down and compile it when I build the project? I would assume this way would be a good way to do it, rather than storing the compiled protobuf classes, since the Android app might need a different "Java-lite" version of the protobufs? I am using the google/protobuf-gradle-plugin to compile the .proto files, and see documentation for compile from local files, or pulling in projects that have precompiled .proto files, but no documentation for pulling in remote .proto files. Am I on the right track?

推荐答案

您的远程.proto文件/存储库以什么形式?如果只是一个URL,则可以使用 Download 任务:

In what form is your remote .proto file/repo? If it is just a url, then you can use Download task:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
    }
}

plugins {
    id "de.undercouch.download" version "3.2.0"
}

group 'testtest'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'com.google.protobuf'


sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

task downloadFile << {
    download {
        src 'https://raw.githubusercontent.com/grpc/grpc-java/master/compiler/src/test/proto/test.proto'
        dest "$projectDir/src/main/proto/test.proto"
        overwrite true
    }
}

build.dependsOn downloadFile

dependencies {
    compile "io.grpc:grpc-protobuf-lite:1.5.0"
    compile "io.grpc:grpc-stub:1.5.0"
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.3.0'
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.5.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite {}
                grpc {
                    option 'lite'
                }
            }
        }
    }
}

这篇关于管理共享协议缓冲区库并使用Gradle进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆