【问题标题】:Maven and java: how to generate code from protobuf files in test directory?Maven 和 java:如何从测试目录中的 protobuf 文件生成代码?
【发布时间】:2019-08-26 15:01:22
【问题描述】:
我的问题与question 非常相似,但针对 maven 和 java。
我正在测试 grpc,并想在 test/proto 文件夹中放入一个简单的 helloworld.proto。
但是该文件不会生成 java 文件(与 /src/main/proto 中的 proto 文件不同)。
所以我的问题是如何在 test 文件夹中为 proto 生成代码?
【问题讨论】:
标签:
java
maven
protocol-buffers
grpc
grpc-java
【解决方案1】:
首先,按照documentation 使用 org.xolstice.maven.plugins protobuf-maven-plugin。
或者,您可以复制example pom.xml(这是固定到 v1.19.0 版本;考虑使用最新的标签)。这个 pom 被 helloworld 示例等使用。
然后为 protobuf-maven-plugin 添加 test-compile 和 test-compile-custom 目标。这将导致生成src/test/proto 中的文件。
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
<goal>test-compile</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>