java - gRPC maven 未在目标文件夹中生成 stub 类

标签 java grpc

它曾经工作正常,但我不确定发生了什么变化,我再也看不到生成的 stub 了。

我还在目标文件夹中看到 archive-temp,不知道为什么它现在才出现。

任何生命周期事件都能很好地编译项目,没有任何错误,但没有任何 stub 。

这是我的 Maven 文件。

<?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>com.mycompany</groupId>
    <artifactId>Customer</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

所有生命周期事件,例如清理、打包

属性

    <properties>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <grpc.version>1.22.1</grpc.version>
        <os.plugin.version>1.5.0.Final</os.plugin.version>
        <protobuf.plugin.version>0.6.1</protobuf.plugin.version>
        <protoc.version>3.9.0</protoc.version>
    </properties>

依赖关系

     <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>${protoc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
            <version>${grpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
            <version>${grpc.version}</version>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
            <version>${grpc.version}</version>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-bom</artifactId>
                <version>${grpc.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

构建插件 <build>

        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.puremvc.java.multicore.demos.microservice.employeeadmin.department.Service</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

扩展和插件管理

        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.5.0.Final</version>
            </extension>
        </extensions>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>kr.motd.maven</groupId>
                    <artifactId>os-maven-plugin</artifactId>
                    <version>${os.plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.xolstice.maven.plugins</groupId>
                    <artifactId>protobuf-maven-plugin</artifactId>
                    <version>${protobuf.plugin.version}</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>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

    </build>
</project>

最佳答案

使用 pom.xml 进行构建会产生:

[INFO] --- protobuf-maven-plugin:0.6.1:compile (default) @ Customer ---
[INFO] Skipping mojo execution for project with packaging type 'pom'
[INFO] 
[INFO] --- protobuf-maven-plugin:0.6.1:compile-custom (default) @ Customer ---
[INFO] Skipping mojo execution for project with packaging type 'pom'

将打包从 pom 更改为 jar 似乎可以解决问题,因为 stub 在 target/generated-sources/protobuf/grpc-java 中可见mvn 编译之后。

    <groupId>com.mycompany</groupId>
    <artifactId>Customer</artifactId>
    <packaging>jar</packaging> <!-- was 'pom' -->
    <version>1.0-SNAPSHOT</version>

关于java - gRPC maven 未在目标文件夹中生成 stub 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677053/

相关文章:

java - 在 .java 文件中使用 unicode 字符?

c# - 在服务器端c#读取grpc中的元数据

protocol-buffers - 在 protobuf 消息中定义嵌套的 oneof 消息

java - 无法找到请求目标的有效证书路径 - java

java - 如何在多个EC2 Windows实例上执行CMD命令?

c++ - Windows 上使用 C++ 的 google protobuf 时间戳未声明的标识符

node.js - 动态和静态生成的grpc代码有什么区别?

C++ "error: no type named ‘type’ 在 ‘class std::result_of< ... >"

java - 如何在不退出 JVM 的情况下多次运行 Java 程序?

java - 无法使用maven在spring mvc项目中启动服务器