Maven 和 java:如何从测试目录中的 protobuf 文件生成代码?

apa*_*ana 4 java protocol-buffers maven grpc grpc-java

我的问题与this问题非常相似,但是针对maven和java。

我正在测试 grpc,并想将一个简单的 helloworld.proto 放入 test/proto 文件夹中。

但是该文件不会生成 java 文件(与 /src/main/proto 中的 proto 文件不同)。

所以我的问题是如何在测试文件夹中生成 proto 代码?

Eri*_*son 6

首先,按照文档使用 org.xolstice.maven.plugins protobuf-maven-plugin。

或者,您可以复制示例 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>
Run Code Online (Sandbox Code Playgroud)