Maven和Java:如何从测试目录中的protobuf文件生成代码? [英] Maven and java: how to generate code from protobuf files in test directory?

查看:331
本文介绍了Maven和Java:如何从测试目录中的protobuf文件生成代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与这个问题非常相似,但对于maven和Java.

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

我正在测试grpc,并且想要放在test/proto文件夹中的简单helloworld.proto中.

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

但是,该文件不会生成Java文件(与/src/main/proto中的proto文件不同).

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

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

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

推荐答案

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

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

或者,您可以复制示例pom. xml (已固定在v1.19.0版本中;请考虑使用最新的标记). helloworld示例以及其他示例都使用了此pom.

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.

然后为protobuf-maven-plugin添加test-compiletest-compile-custom目标.这将导致生成src/test/proto中的文件.

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>

这篇关于Maven和Java:如何从测试目录中的protobuf文件生成代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆