【问题标题】:protoc-jar-maven-plugin: Not generating grpc service stubsprotoc-jar-maven-plugin:不生成 grpc 服务存根
【发布时间】:2018-07-09 03:41:55
【问题描述】:

我已经添加了maven配置如下。

        <plugin>
              <groupId>com.github.os72</groupId>
              <artifactId>protoc-jar-maven-plugin</artifactId>
              <version>3.5.1</version>
              <executions>
                  <execution>
                      <phase>generate-sources</phase>
                      <goals>
                          <goal>run</goal>
                      </goals>
                      <configuration>
                          <protocArtifact>com.google.protobuf:protoc:3.0.0</protocArtifact>
                          <inputDirectories>
                              <include>src/main/protobuf</include>
                          </inputDirectories>
                         <outputTargets>
                              <outputTarget>
                                  <type>java</type>
                                  <outputDirectory>src/main/java</outputDirectory>
                              </outputTarget>
                              <outputTarget>
                                  <type>grpc-java</type>
                                  <outputDirectory>src/main/java</outputDirectory>
                                  <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.1</pluginArtifact>
                              </outputTarget>
                          </outputTargets>
                      </configuration>
                  </execution>
              </executions>
       </plugin>

我通过“mvn protoc-jar:run”执行它,但是它跳过了 grpc 服务存根,只将“消息”转换为 java 代码。

输出是

[INFO] Protoc version: 3.5.1
protoc-jar: protoc version: 3.5.1, detected platform: osx-x86_64 (mac os x/x86_64)
protoc-jar: embedded: bin/3.5.1/protoc-3.5.1-osx-x86_64.exe
protoc-jar: executing: [/var/folders/9y/w8qrc__9513dv57323sjdlmw0000gn/T/protocjar320569499467403052/bin/protoc.exe, --version]
libprotoc 3.5.1
[INFO] Protoc command: /var/folders/9y/w8qrc__9513dv57323sjdlmw0000gn/T/protocjar320569499467403052/bin/protoc.exe
[INFO] Input directories:
[INFO]     /Users/dev/learn/proto-java/src/main/protobuf
[INFO] Output targets:
[INFO]     java: /Users/dev/learn/proto-java/target/generated-sources (add: main, clean: false, plugin: null, outputOptions: null)
[INFO]     Processing (java): helloworld.proto
protoc-jar: executing: [/var/folders/9y/w8qrc__9513dv57323sjdlmw0000gn/T/protocjar320569499467403052/bin/protoc.exe, -I/Users/dev/learn/proto-java/src/main/protobuf, --java_out=/Users/dev/learn/proto-java/target/generated-sources, /Users/dev/learn/proto-java/src/main/protobuf/helloworld.proto]
[INFO] Adding generated classes to classpath

我有一个简单的原型文件定义为

syntax = "proto3";

option java_multiple_files = true;
option java_package = "-.-.-.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {}
    rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
    string name = 1;
}

// The response message containing the greetings
message HelloReply {
    string message = 1;
}

我只输出生成为 HelloReply、HelloReplyOrBuilder、HelloRequest、HelloRequestOrBuilder、HelloWorldProto 的文件。 Greeter Grpc 服务存根丢失。我是 Maven 新手,我错过了什么吗?

【问题讨论】:

    标签: java maven protocol-buffers grpc-java


    【解决方案1】:

    使用 maven 阶段 ...https://maven.apache.org/ref/3.5.2/maven-core/lifecycles.html

    mvn generate-sources 会生成你的 GreeterRpc

    【讨论】:

    • 谢谢我在执行标签中添加了 process-resources。它对我来说很好。
    • 我不明白解决方案和评论,我们是否应该将问题 `generate-sources` 中的阶段更改为 &lt;phase&gt;process-resources&lt;/phase&gt;
    【解决方案2】:
     <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:1.4.0:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <id>protobuf-generate</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    【讨论】:

    • 我试过这个插件设置,它甚至没有生成消息文件。
    猜你喜欢
    • 2015-09-10
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多