java - 如何将 google.protobuf.Timestamp 与 protoc-jar-maven-plugin 一起使用?

标签 java maven protocol-buffers protoc

我一直在尝试将 protobuf 类型 google.protobuf.Timestampprotoc-jar-maven-plugin 一起使用,但只收到这些编译时错误:

google/protobuf/timestamp.proto: File not found.
test.proto: Import "google/protobuf/timestamp.proto" was not found or had errors.
test.proto:9:5: "google.protobuf.Timestamp" is not defined.

原型(prototype)文件如下所示:

syntax = "proto3";

import "google/protobuf/timestamp.proto";

package test;
option java_package = "test";

message TestTimestamp {
    google.protobuf.Timestamp liveStartDate = 1;
}

pom文件有以下配置:

<build>
    <plugins>
        <plugin>
            <groupId>com.github.os72</groupId>
            <artifactId>protoc-jar-maven-plugin</artifactId>
            <version>3.6.0.1</version>
            <executions>
                <execution>
                    <id>protoc.main</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <protocVersion>3.6.0</protocVersion>
                        <addSources>main</addSources>
                        <includeDirectories>
                            <include>src/main/protobuf</include>
                        </includeDirectories>
                        <inputDirectories>
                            <include>src/main/protobuf</include>
                        </inputDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

据我了解,这种类型是 proto3 的一部分,那么为什么我会收到这些错误?

最佳答案

事实证明,由于这种类型不是标准类型之一,Maven 插件需要额外的配置才能使其工作,即 <includeMavenTypes>direct</includeMavenTypes>参数,如下所示:

<build>
    <plugins>
        <plugin>
            <groupId>com.github.os72</groupId>
            <artifactId>protoc-jar-maven-plugin</artifactId>
            <version>3.6.0.1</version>
            <executions>
                <execution>
                    <id>protoc.main</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <protocVersion>3.6.0</protocVersion>
                        <addSources>main</addSources>
                        <includeMavenTypes>direct</includeMavenTypes>
                        <includeDirectories>
                            <include>src/main/protobuf</include>
                        </includeDirectories>
                        <inputDirectories>
                            <include>src/main/protobuf</include>
                        </inputDirectories>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

使用此配置,可以编译 proto 文件。

关于java - 如何将 google.protobuf.Timestamp 与 protoc-jar-maven-plugin 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53096369/

相关文章:

hibernate.cfg.xml 未解析

java - 如何为我的 Maven 项目制作一个可运行的 jar?

java - Jenkins maven 还有其他 java 版本。如何解决?

json - JSON REST/RPC 接口(interface)的 IDL

node.js - GRPC微服务架构实现

java - Gson:从 Json 字符串实例化接口(interface)

java - 无法在 LibGDX 中设置 config.height 和 config.width

cmake - 创建 cmake 文件期间的 PROTOBUF_GENERATE_CPP

java - 无法执行基本的 Hadoop Mapreduce Wordcount 示例

java - 如何为两个解析器语法制作相同的词法分析器