簡體   English   中英

.proto文件導入錯誤

[英]Error with import in .proto file

我有一個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" is not defined 我正在使用Intellij作為IDE。 在IDE中, AlternativeV1IndexV1類型顯示為“未解析的引用”。 誰能幫我解決這個問題?

在下面找到一個小的工作示例。 因此,您可以將其與項目設置進行比較。

假設以下文件和目錄結構。

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

運行mvn compile將在target/classes/sub/optimal/下面生成類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM