protoc-jar-maven-plugin 和 protoc 导入路径

问题描述 投票:0回答:1

我目前正在开发一个由多个 Java 服务组成的应用程序,这些服务应使用 gRPC 进行通信。团队决定将所有 .proto 文件组织在专用于该目的的中央共享存储库中。

存储库的结构如下:

.
├── service-a
│   └── v1
|       |── pom.xml
│       └── a.proto
├── service-b
│   └── v1
|       |── pom.xml
│       └── b.proto

为了处理代码生成,我们编写了一个小型 bash 脚本,该脚本循环访问共享存储库中的所有服务,并为每个主要版本生成一个

pom.xml
,其中包含一个插件,用于生成 Java 类并将它们打包到包含所有内容的 JAR 中。所需的依赖项(即
protobuf-java
grpc-protobuf
grpc-stub
)。

服务的 .proto 文件可能会使用导入引用来自其他服务的消息。然而,我无法找到一种方法来配置 `protoc-jar-maven-plugin` 来告诉

protoc
将导入路径设置为共享存储库的根目录,以便它可以解析每个服务的 .proto 文件中定义的导入(所有导入原型文件中的路径相对于存储库的根目录)。

以下是 .proto 文件导入另一个文件的示例,以及为每个服务的主要版本目录生成的 pom.xml 中的 `protoc-jar-maven-plugin` 配置:

syntax = "proto3";

package b.v1;

// Import statement allowing to use the "a.v1.A" message
// Path is relative to the shared repository's root
import "service-a/v1/a.proto";

service B {
  rpc someRpc(a.v1.A) returns (a.v1.A);
}
<plugin>
    <groupId>com.github.os72</groupId>
    <artifactId>protoc-jar-maven-plugin</artifactId>
    <version>3.11.4</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <includeMavenTypes>direct</includeMavenTypes>
                                <!-- Including all .proto file in the shared repo. so -->
                                <!-- that imports can be resolved -->
                <includeDirectories>
                    <include>../../</include>
                </includeDirectories>
                                <!-- Path to the service's .proto files -->
                <inputDirectories>
                    <include>./</include>
                </inputDirectories>
            </configuration>
        </execution>
    </executions>
</plugin>

当我尝试生成使用另一个服务消息的服务的源时,插件失败告诉我,由于找不到服务的原型文件中定义的导入,协议退出并出现错误。

我还应该指定此 POM 与每个主要 API 版本的 .proto 文件位于同一级别,并使用

mvn clean install
执行,以便将生成的源安装在本地 Maven 存储库上以供进一步本地使用。

所以我的问题是,如何配置插件以将协议导入路径设置为存储库的根目录,以便解析每个 .proto 文件导入?

我尝试按照

文档 
中的指定调整
includeDirectories
inputDirectories 但没有取得太大成功。

java maven protocol-buffers
1个回答
0
投票

您可以尝试使用与您的配置类似的配置(包含目录除外)并且在这里工作正常吗?

                <plugin>
                    <groupId>com.github.os72</groupId>
                    <artifactId>protoc-jar-maven-plugin</artifactId>
                    <version>3.11.4</version>
                    <executions>
                        <execution>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <protocVersion>3.11.4</protocVersion>
                                <includeDirectories>
                                    <include>src/main/resources</include>
                                </includeDirectories>
                                <inputDirectories>
                                    <include>src/main/resources</include>
                                </inputDirectories>
                               <includeMavenTypes>direct</includeMavenTypes>
                            </configuration>
                        </execution>
                    </executions>
                    <configuration>
                        <protocCommand>${protoc.location}</protocCommand>
                    </configuration>
                </plugin>
© www.soinside.com 2019 - 2024. All rights reserved.