gpt4 book ai didi

java - .proto 文件导入时出错

转载 作者:行者123 更新时间:2023-11-30 07:42:05 28 4
gpt4 key购买 nike

我有一个 MyResponse.proto 文件,它导入两个 .proto 文件 Alternative.protoIndex.proto >.

MyResponse.proto 文件:

import "Alternative.proto";
import "Index.proto";
message MyResponse {
repeated AlternativeV1 alternativeV1 = 1;
required IndexV1 indexV1 = 2;
}

Alternative.proto 文件:

import "Reference.proto";
message AlternativeV1 {
required string name = 1;
required string id = 2;
}

Index.proto 文件:

message IndexV1 {
required string name = 1;
}

使用 maven 编译 proto 文件时收到错误“AlternativeV1”未定义。我使用 Intellij 作为 IDE。类型 AlternativeV1IndexV1 在 IDE 中显示为“未解析的引用”。谁能帮我解决这个问题?

最佳答案

下面是一个小的工作示例。因此您可以将其与您的项目设置进行比较。

假设以下文件和目录结构。

pom.xml
src/main/proto/Alternative.proto
src/main/proto/Index.proto
src/main/proto/MyResponse.proto

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>sub.optimal</groupId>
<artifactId>ProtoExample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- <maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>-->
</properties>

<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
<pluginRepository>
<id>protoc-plugin</id>
<url>https://dl.bintray.com/sergei-ivanov/maven/</url>
</pluginRepository>
</pluginRepositories>

<build>
<plugins>
<plugin>
<groupId>com.google.protobuf.tools</groupId>
<artifactId>maven-protoc-plugin</artifactId>
<version>0.4.4</version>
<configuration>
<protocExecutable>protoc</protocExecutable>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Alternative.proto

package sub.optimal;
message AlternativeV1 {
required string name = 1;
required string id = 2;
}

Index.proto

package sub.optimal;
message IndexV1 {
required string name = 1;
}

MyResponse.proto

package sub.optimal;
option java_outer_classname = "MyResponseProtos";
import "Alternative.proto";
import "Index.proto";
message MyResponse {
repeated AlternativeV1 alternativeV1 = 1;
required IndexV1 indexV1 = 2;
}

以下内容已添加到 *.proto 文件

  • 包 - 生成有效的 Java 源代码
  • 选项 java_outer_classname - 否则 protoc 编译器将生成默认类 MyResponseOuterClass.java

运行mvncompile将生成target/classes/sub/optimal/下面的类。

关于java - .proto 文件导入时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34506534/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com