How to share protobuf files between maven modules?

Viewed 283

I have two services A and B which placed in monorepo in different maven modules, also they have Aggregate pom.xml which contains the next modules:

<modules>
    <module>A</module>
    <module>B</module>
</modules>

Both services are talking through gRPC and have common protocol which described in the proto files. The grpc-java manual says, that I must put my proto files into src/main/resourses/proto folder. It means I have to copy the same proto files bewteen two services:

  1. A/src/main/resourses/proto/somefile.proto
  2. B/src/main/resourses/proto/somefile.proto

Which is code duplication actually. The main question - How can I share and compile proto files between two maven modules in monorepo?

I have done the next:

  1. Created the separate library which contains only proto files. Let's call it C.
  2. Added C dependency to A and B modules. Aggregated pom.xml looks like:
<modules>
    <module>C</module>
    <module>A</module>
    <module>B</module>
</modules>

The approach seems quite havy for that case and I don't want to have a separate maven module for that. Moreover, I will definetley face with a problem, if I use different language for B service (something other than java and maven). Is there a known solution for this problem? Can I share protofiles without separate library/module? Any examples appreciated.

1 Answers

I've been wrong with

The grpc-java manual says, that I must put my proto files into src/main/resourses/proto folder.

We can set protoSourceRoot configuration for grpc-java plugin. We can specify any required proto source folder as follows:

<protoSourceRoot>${basedir}/../proto</protoSourceRoot>

It means no need in separate maven module and library.

Related