GRPC + Maven:无法使用 `oneof`:缺少符号 `InternalOneOfEnum`

GRPC + Maven: Unable to use `oneof`: Symbol `InternalOneOfEnum` missing

我无法使用最新版本的 GRPC 编译任何包含 oneof 字段的 GRPC 消息。

mvn install导致编译错误:

[ERROR] /home/travis/build/joaomlneto/grpc-java-oneof/target/generated-sources/protobuf/java/MyServiceOuterClass.java:[138,48] cannot find symbol
  symbol:   class InternalOneOfEnum
  location: class com.google.protobuf.AbstractMessage

我已经将它缩减为两个文件的示例,但我仍然对此感到困惑。我错过了什么? :-(


最小、完整且可验证的示例

src/main/proto/MyService.proto

syntax = "proto3";

service MyService {
  rpc uploadStuff(stream UploadStuffRequest) returns (UploadStuffResponse);
}

message UploadStuffRequest {
  oneof contents {
    string data = 1;
    string metadata = 2;
  }
}

message UploadStuffResponse {
  int32 result = 1;
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.github.joaomlneto</groupId>
    <artifactId>oneof-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>1.24.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.24.0</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.24.0</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.10.0:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.24.0:exe:${os.detected.classifier}</pluginArtifact>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



</project>

io.grpc:grpc-protobuf:1.24.0 仍然使用 com.google.protobuf:protobuf-java:3.9.0,因此将 com.google.protobuf:protoc:3.10.0:exe 降级到版本 3.9.0 将解决您的问题。