通过引用其他存储库或项目或jar文件中的其他protobuf消息来分层编写协议缓冲区(protobuf)定义 [英] Compose Protocol buffers (protobuf) definitions hierarchically by referencing other protobuf messages in other repositories or projects or jar files

查看:419
本文介绍了通过引用其他存储库或项目或jar文件中的其他protobuf消息来分层编写协议缓冲区(protobuf)定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大图是我有一个protobuf(想象它就像一个类)数据类型,它引用另一个protobuf,它位于我的POM文件中的另一个Jar文件中。这适用于.java文件但不幸的是不适用于protobuf文件。我能想到的最好的解决方案是告诉Maven在一个位置提取另一个依赖Jar(包含proto文件)文件,然后告诉Maven对该位置的所有这些proto文件进行protoc编译。唉,我不知道如何告诉Maven这样做。由于我们使用标准Jar文件来捕获我们的proto文件,因此将非常感谢任何帮助。

The big picture is that I have a protobuf (think of it like a class) datatype that refers to another protobuf that is within another Jar file that is a dependency in my POM file. This works perfectly for .java files but unfortunately doesn't work for protobuf files. The best solution I can think of is to tell Maven to extract this other dependency Jar (that contain proto files) file in a location and then tell Maven to do a protoc compile of all these proto files in that location. Alas, I dont know how to tell Maven to do this. Any help would be greatly appreciated as we use standard Jar files to capture our proto files.

import "X.proto"; // refers to a proto file in another jar

import "Y.proto";

message A {

  repeated X o = 1; // This cant be done
  repeated Y e = 2;

}

由于X不在同一个位置,因此上述操作不起作用路径作为这个文件。

The above will not work since X is not in the same path as this file.

推荐答案

我在Gradle中找到了解决方案。填写下面的空白并正确指出你的回购,你应该能够得到以下工作。现在,您可以通过其他jar文件中的原型文件在多个项目中分层组合protobufs!

I found the solution for this in Gradle. Fill in the blanks below and point your repo correctly, and you should be able to get the below to work. Now you can hierarchically compose protobufs across multiple projects through proto files within other jar files !

在gradle中,您可以使用以下内容:

In gradle, you can use the following:

// these are your protobuf plugin repositories
buildscript {  
   repositories {
       maven {
           url 'WHERE YOUR PROTOBUF PLUGIN EXISTS. e.g. http://maven or MavenCentral()'
       } 
   }

   dependencies {
       classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'    
       classpath 'com.google.protobuf:protoc:2.6.1'
   }
}

apply plugin: 'com.google.protobuf'

group = "YOUR PACKAGE NAME. e.g. com.x.y"
version = "1.0" // release version

// these are your regular dependencies repositories. This could be
// very similar to the ones in buildscript above. 
repositories {  
    mavenLocal()
}

// this is needed by the plugin and is where teh magic happens. It 
// tells protobuf where to extract all the proto files that it finds 
// in all  the dependency jar files you have          
protobuf { 
    generatedFilesBaseDir="$projectDir/src/"    
}
// also magic you need for this protobuf plugin to work    
sourceSets {
     main {
        // this tells the plugin where your project specific
        // protofiles are located   
        proto {    
           srcDir 'src/main/resources/proto/'    
        }

        java {
           srcDir 'src/main/java'
         }
    }
}

dependencies {      
     compile 'com.google.protobuf:protobuf-java:2.6.1'
}

这篇关于通过引用其他存储库或项目或jar文件中的其他protobuf消息来分层编写协议缓冲区(protobuf)定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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