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

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

我的问题非常类似于这个question,但对于maven和java。

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

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

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

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

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

或者,您可以复制example pom.xml(这是固定到v1.19.0版本;考虑使用最新的标记)。这个pom被helloworld示例使用。

然后为protobuf-maven-plugin添加test-compiletest-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>
© www.soinside.com 2019 - 2024. All rights reserved.