【问题标题】:GRPC Generate Sources - Java IntelliJGRPC 生成源 - Java IntelliJ
【发布时间】:2020-11-22 03:34:08
【问题描述】:

我正在关注 GRPC 的 tutorial,我的问题是当我运行“mvn protobuf:compile”时,文件会在目标文件夹中生成,我不确定如何将代码生成到 java目录,这是我的 POM 文件

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java-util -->
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java-util</artifactId>
        <version>4.0.0-rc-2</version>
    </dependency>

    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-netty-shaded</artifactId>
        <version>1.33.1</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-protobuf</artifactId>
        <version>1.33.1</version>
    </dependency>
    <dependency>
        <groupId>io.grpc</groupId>
        <artifactId>grpc-stub</artifactId>
        <version>1.33.1</version>
    </dependency>
    <dependency> <!-- necessary for Java 9+ -->
        <groupId>org.apache.tomcat</groupId>
        <artifactId>annotations-api</artifactId>
        <version>6.0.53</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

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

这是我的proto 文件,

这是我的问题,

1- The sources are generated in the target folder, I can import them just fine but why in Google docs it shows in the main/java/io..etc folder?
2- stubs like RouteGuideBlockingStub are not getting generated with mvn protobuf:compile, should I be using something else?

【问题讨论】:

    标签: java intellij-idea grpc


    【解决方案1】:

    正如其他答案中已经提到的关于 proto 文件的目录结构限制,这里是您应该如何配置 pom.xml 文件以编译并将生成的类定位到您的 java 类文件中。

    为了编译和生成必要的类文件,需要以下依赖项。

        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>1.33.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>1.33.1</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>1.33.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.14.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <version>3.14.0</version>
        </dependency>
    

    您还需要以下 2 个插件:

    <build>
      ...
      <plugins>
        ...
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>3.2.0</version>
                    <executions>
                        <execution>
                            <id>test</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>add-source</goal>
                            </goals>
                            <configuration>
                                <sources>
                                    <source>${basedir}/target/generated-sources</source>
                                </sources>
                            </configuration>
                        </execution>
                    </executions>
                </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.11.0:exe:${os.detected.classifier}</protocArtifact>
                        <pluginId>grpc-java</pluginId>
                        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.27.2:exe:${os.detected.classifier}</pluginArtifact>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>compile-custom</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    

    【讨论】:

      【解决方案2】:

      除非您想将代码分发为具有可用源代码的库,否则生成的源代码并没有那么有用。生成的类足以与 GRPC 一起使用。而且项目构建速度更快。

      protobuf-maven-plugin 可以通过插件配置部分中的outputDirectory 配置为使用您选择的生成类的路径。

      只是不要将它们放入您的源文件夹中。

      protobuf:compile 只会得到 protobuf 定义。 要获取 grpc 存根,您需要在构建中配置的 compile-custom 目标。

      通常不需要单独调用它们,只需编译整个项目就可以了。

      【讨论】:

        猜你喜欢
        • 2015-05-31
        • 1970-01-01
        • 2018-03-20
        • 1970-01-01
        • 2017-09-21
        • 2021-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多