在Maven项目之间导入协议缓冲区定义 [英] Importing protocol buffer definitions between Maven projects

查看:105
本文介绍了在Maven项目之间导入协议缓冲区定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前管理几个单独的Maven项目,在这些项目中,我使用Protobufs作为序列化格式并通过有线方式进行.我正在使用David Trott的 maven-protoc插件在编译时生成代码.

一切都很好,直到我希望那些项目之间能够相互交流-或者更确切地说,是使用彼此的原型. protobuf语言具有"import"指令,该指令可以满足我的要求,但我面临的挑战是让项目A导出供项目B依赖的".proto"文件(或可能的某些中间格式?).

Maven为项目提供了一种捆绑资源的方法,但是AFAIK,它们是在运行时由代码使用的,而不是在编译/源生成阶段的目标中使用的-至少我找不到描述我要实现的目标的文档.

解决方案

我找到了另一种实现方法,它不涉及任何Maven魔术.深入研究maven-protoc插件的代码,我发现这是受支持的用例-该插件将在依赖的jar中查找并收集和.proto文件,并将其解压缩到一个临时目录中.然后将该目录设置为协议调用的导入路径.

所有需要发生的事情是将.proto文件包含在依赖项的程序包中,这是通过将其作为资源来完成的:

projects/a/src/main/resources/a.proto

现在在projects/b/pom.xml中,将'a'添加为常规Maven依赖项,然后从b.proto导入a.proto,就好像它在本地存在一样:

b.proto: 导入"a.proto";

这并不理想,因为文件名可能在各个项目之间发生冲突,但这很少发生.

I currently manage a few separate Maven projects in which I use Protobufs as a serialization format and over the wire. I am using David Trott's maven-protoc plugin to generate the code at compile time.

All is good and well until I want those project to communicate between one another — or rather, use each other's protobufs. The protobuf language has an "import" directive which does what I want but I'm faced with the challenge of having project A exporting a ".proto" file (or possibly some intermediate format?) for project B to depend upon.

Maven provides a way for a project to bundle resources but AFAIK, these are meant to be used at runtime by the code and not by a goal during the compile / source generation phase — at least I haven't been able to find documentation that describes what I want to achieve.

解决方案

I've found another way to achieve, and it doesn't involve any Maven magic. Diving into the code for the maven-protoc plugin, I found that this is a supported use case -- the plugin will look for and collect and .proto files in dependent jars and unpack them into a temporary directory. That directory is then set as an import path to the protoc invocation.

All that needs to happen is for the .proto file to be included in the dependency's package, which I did by making it a resource:

projects/a/src/main/resources/a.proto

Now in projects/b/pom.xml, add 'a' as a regular Maven dependency and just import a.proto from b.proto as if it existed locally:

b.proto: import "a.proto";

This isn't ideal, since files names may clash between various projects, but this should occur rarely enough.

这篇关于在Maven项目之间导入协议缓冲区定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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