Importing protocol buffer definitions between Maven projects

后端 未结 2 1590
后悔当初
后悔当初 2021-02-02 17:26

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

相关标签:
2条回答
  • 2021-02-02 17:57

    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.

    0 讨论(0)
  • 2021-02-02 18:02

    You can package your .proto files in a separate .jar/.zip in the project where they are generated, and publish them in your repository using a dedicated classifier. Using the assembly plugin might help here to publish something close to "source jars" that are built during releases.

    Then, on projects using them, add previously created artifact as dependency. Use the dependency plugin with the "unpack-dependencies" goal, and bind it to a phase before "compile".

    0 讨论(0)
提交回复
热议问题