Maven and java: how to generate code from protobuf files in test directory?

大兔子大兔子 提交于 2019-12-12 18:34:25

问题


My question is very similar to this question but for maven and java.

I am testing grpc, and want to put to a simple helloworld.proto in the test/proto folder.

However the file doesn't generate a java file (unlike the proto file in /src/main/proto).

So my question is how to generate code for proto in the test folder?


回答1:


First, follow the documentation to use the org.xolstice.maven.plugins protobuf-maven-plugin.

Alternatively, you can copy the example pom.xml (this is pinned to the v1.19.0 release; consider using whatever the latest tag is). This pom is used by the helloworld example, among others.

Then add the test-compile and test-compile-custom goals for the protobuf-maven-plugin. This will cause files in src/test/proto to be generated.

      <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>


来源:https://stackoverflow.com/questions/55522995/maven-and-java-how-to-generate-code-from-protobuf-files-in-test-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!