簡體   English   中英

Intellij 無法識別協議緩沖區導入

[英]Protocol Buffer imports not recognized in Intellij

我正在嘗試將一個協議緩沖區消息導入另一個,但無法識別導入。 只要我不嘗試將一個 protobuf 導入另一個,就會生成 protobuf 代碼(在 java 中),代碼會按預期編譯和運行。

我在用着:

  • Intellij Idea 2020 v1.3 無限版
  • Protobuf 編輯器插件:jvolkman/intellij-protobuf-editor(2020 年 4 月)
  • Gradle

我的 gradle 構建文件如下所示:

plugins {
    id 'java'
    id 'com.google.protobuf' version "0.8.8"
}

group 'tech.tablesaw'
version '1.0-SNAPSHOT'

sourceCompatibility = 9.0

def grpcVersion = '1.30.1' // CURRENT_GRPC_VERSION
def protobufVersion = '3.12.0'
def protocVersion = protobufVersion

repositories {
    mavenCentral()
}

test {
    useJUnitPlatform()
}

dependencies {
    implementation "io.grpc:grpc-protobuf:${grpcVersion}"
    implementation "io.grpc:grpc-stub:${grpcVersion}"
    compileOnly "org.apache.tomcat:annotations-api:6.0.53"

    // advanced - need this for JsonFormat
    implementation "com.google.protobuf:protobuf-java-util:${protobufVersion}"

    runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"
    testImplementation "io.grpc:grpc-testing:${grpcVersion}"

    compile group: 'tech.tablesaw', name: 'tablesaw-core', version: '0.38.1'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.2'
    testImplementation "org.mockito:mockito-core:2.28.2"
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
    }
    generateProtoTasks {
        all()*.plugins { grpc {} }
    }
}

// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
    main {
        java {
            srcDirs 'build/generated/source/proto/main/grpc'
            srcDirs 'build/generated/source/proto/main/java'
        }
    }
}

task TablesawServer(type: CreateStartScripts) {
    mainClassName = 'tech.tablesaw.service.TableServiceServer'
    applicationName = 'tablesaw-table-server'
    outputDir = new File(project.buildDir, 'tmp')
}

task TablesawClient(type: CreateStartScripts) {
    mainClassName = 'tech.tablesaw.service.TableServiceClient'
    applicationName = 'tablesaw-table-client'
    outputDir = new File(project.buildDir, 'tmp')
}

我的 gradle 信息如下所示:

------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------

Build time:   2019-01-10 23:05:02 UTC
Revision:     3c9abb645fb83932c44e8610642393ad62116807

Kotlin DSL:   1.1.1
Kotlin:       1.3.11
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          9.0.4 (Oracle Corporation 9.0.4+11)
OS:           Mac OS X 10.13.5 x86_64

這是一個示例 protobuf。 column_type.proto 的導入失敗。

syntax = "proto3";

package tech.tablesaw.service.common;

import "tech/tablesaw/service/common/column_type.proto";

option java_multiple_files = true;
option java_package = "tech.tablesaw.service.common";
option java_outer_classname = "ColumnMetaProto";
option objc_class_prefix = "TSW";

// Proto file describing column metadata message.

// A column metadata object
message ColumnMetadata {

  string name = 1;
  int32 size = 2;
  ColumnTypeEnum.ColumnType column_type = 3;
}

這是我要導入的文件:

syntax = "proto3";

package tech.tablesaw.service.common;

option java_multiple_files = true;
option java_package = "tech.tablesaw.service.common";
option java_outer_classname = "ColumnTypeEnum";
option objc_class_prefix = "TSW";

enum ColumnType {
  SHORT = 0;
  INTEGER = 1;
  LONG = 2;
  FLOAT = 3;
  BOOLEAN = 4;
  STRING = 5;
  DOUBLE = 6;
  LOCAL_DATE = 7;
  LOCAL_TIME = 8;
  LOCAL_DATE_TIME = 9;
  INSTANT = 10;
  TEXT = 11;
  SKIP = 12;
}

最后,這里是 protobufs 在文件系統中的位置。

src > main > java
           > proto > tech > tablesaw > service > common > column_metadata.proto
                                                        > column_type.proto

查看描述如何添加其他路徑的自述文件。

默認情況下, intellij-protobuf-editor使用項目配置的源根作為 protobuf 導入路徑。 如果這不正確,您可以在Settings > Languages & Frameworks > Protocol Buffers中覆蓋這些路徑。 取消選中“自動配置”並添加您需要的任何路徑。 在您的情況下,您將添加.../src/main/java/proto (其中...表示您的項目的基本路徑是什么)。

另一個問題的評論中有這個問題的答案: Error using import in.proto file

如果您使用 IntelliJ IDEA,go 到 Preferences -> Protobuf Support 並將路徑添加到 your.proto 文件。 這將解決錯誤。

在更現代的 Intellij 版本中,這是在插件 intellij-protobuf-editor 中完成的。 我將路徑添加到我的原始源文件夾的根目錄,一切都很好

想描述什么對我有幫助,因為我在這里沒有找到答案。 我的步驟適用於 Maven,但我認為 Gradle 的步驟幾乎相同。

  1. 檢查你是否有這個插件
      <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.2:exe:${os.detected.classifier}</protocArtifact>
        </configuration>

        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
  1. 檢查您的build部分中是否有此擴展
  <build>
    <extensions>
      <extension>
        <groupId>kr.motd.maven</groupId>
        <artifactId>os-maven-plugin</artifactId>
        <version>1.6.1</version>
      </extension>
    </extensions>
    ....
  1. 運行這個 maven 目標
mvn protobuf:compile

我正在使用 GoLand,但 IntelliJ 的設置應該相同:

configure automatically

添加您的項目正在使用的所有路徑。

在此處輸入圖像描述

如果您使用的是protobuf-gradle-plugin ,則需要應用idea插件。 應用后,一切都像魅力一樣。 從他們的自述文件

apply plugin: 'idea'

clean {
    delete protobuf.generatedFilesBaseDir
}

idea {
    module {
        // proto files and generated Java files are automatically added as
        // source dirs.
        // If you have additional sources, add them here:
        sourceDirs += file("/path/to/other/sources");
    }
}

暫無
暫無

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

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