简体   繁体   English

在 Gradle (IntelliJ) 中使用 protobuf

[英]Using protobuf with Gradle (IntelliJ)

I am trying to generate code for a simple protobuf example using the build instructions given here .我正在尝试使用此处给出的构建说明为一个简单的 protobuf示例生成代码。 I have been trying for awhile but I am not able to see any auto generated code in my source root.我已经尝试了一段时间,但在我的源根目录中看不到任何自动生成的代码。

The following is my build.gradle file以下是我的build.gradle文件

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.protobuf'

group = 'io.ai.vivid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.5.1-1"
    }

    generateProtoTasks.generatedFilesBaseDir = 'generated-sources'

    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

dependencies {
    compile 'io.grpc:grpc-netty-shaded:1.14.0'
    compile 'io.grpc:grpc-protobuf:1.14.0'
    compile 'io.grpc:grpc-stub:1.14.0'
}

Also in my build.gradle file IntelliJ complains that it cannot resolve name protobuf同样在我的build.gradle文件中,IntelliJ 抱怨它无法解析名称protobuf

Things I have tried我尝试过的事情

  • Sync gradle tool in IntelliJ. IntelliJ 中的同步 gradle 工具。 This is the most prominent solution这是最突出的解决方案
    given in SO在 SO 中给出

  • Setting Build tools -> Gradle -> Runner -> Delelgate IDE build/run actions on gradle to true将构建工具 -> Gradle -> Runner -> 委托 IDE 在 gradle 上构建/运行操作设置为 true

  • Clean rebuilding of the gradle project.彻底重建 gradle 项目。

From my understanding of the GitHub post, when you use the protobuf plugin, the stub will be automatically generated for you.根据我对GitHub帖子的理解,当您使用protobuf插件时,会自动为您生成存根。 What am I missing?我错过了什么?

I am trying to generate code for a simple protobuf example using the build instructions given here .我正在尝试使用此处给出的构建指令为一个简单的protobuf示例生成代码。 I have been trying for awhile but I am not able to see any auto generated code in my source root.我已经尝试了一段时间,但在源根目录中看不到任何自动生成的代码。

The following is my build.gradle file以下是我的build.gradle文件

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.protobuf'

group = 'io.ai.vivid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.5.1-1"
    }

    generateProtoTasks.generatedFilesBaseDir = 'generated-sources'

    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

dependencies {
    compile 'io.grpc:grpc-netty-shaded:1.14.0'
    compile 'io.grpc:grpc-protobuf:1.14.0'
    compile 'io.grpc:grpc-stub:1.14.0'
}

Also in my build.gradle file IntelliJ complains that it cannot resolve name protobuf同样在我的build.gradle文件中,IntelliJ抱怨它无法解析名称protobuf

Things I have tried我尝试过的事情

  • Sync gradle tool in IntelliJ. IntelliJ中的同步gradle工具。 This is the most prominent solution这是最突出的解决方案
    given in SO以SO给出

  • Setting Build tools -> Gradle -> Runner -> Delelgate IDE build/run actions on gradle to true设置构建工具-> Gradle-> Runner-> Delelgate IDE在gradle上的构建/运行操作为true

  • Clean rebuilding of the gradle project.彻底重建gradle项目。

From my understanding of the GitHub post, when you use the protobuf plugin, the stub will be automatically generated for you.根据我对GitHub帖子的理解,当您使用protobuf插件时,将自动为您生成存根。 What am I missing?我想念什么?

I am trying to generate code for a simple protobuf example using the build instructions given here .我正在尝试使用此处给出的构建指令为一个简单的protobuf示例生成代码。 I have been trying for awhile but I am not able to see any auto generated code in my source root.我已经尝试了一段时间,但在源根目录中看不到任何自动生成的代码。

The following is my build.gradle file以下是我的build.gradle文件

buildscript {
    ext {
        springBootVersion = '2.0.4.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath "io.spring.gradle:dependency-management-plugin:1.0.3.RELEASE"
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.google.protobuf'

group = 'io.ai.vivid'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'com.amazonaws:aws-java-sdk-bom:1.11.228'
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.5.1-1"
    }

    generateProtoTasks.generatedFilesBaseDir = 'generated-sources'

    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

dependencies {
    compile 'io.grpc:grpc-netty-shaded:1.14.0'
    compile 'io.grpc:grpc-protobuf:1.14.0'
    compile 'io.grpc:grpc-stub:1.14.0'
}

Also in my build.gradle file IntelliJ complains that it cannot resolve name protobuf同样在我的build.gradle文件中,IntelliJ抱怨它无法解析名称protobuf

Things I have tried我尝试过的事情

  • Sync gradle tool in IntelliJ. IntelliJ中的同步gradle工具。 This is the most prominent solution这是最突出的解决方案
    given in SO以SO给出

  • Setting Build tools -> Gradle -> Runner -> Delelgate IDE build/run actions on gradle to true设置构建工具-> Gradle-> Runner-> Delelgate IDE在gradle上的构建/运行操作为true

  • Clean rebuilding of the gradle project.彻底重建gradle项目。

From my understanding of the GitHub post, when you use the protobuf plugin, the stub will be automatically generated for you.根据我对GitHub帖子的理解,当您使用protobuf插件时,将自动为您生成存根。 What am I missing?我想念什么?

In case anyone else ends up here problems getting IntelliJ to recognise the generated sources (Red highlight imports , classes etc).如果其他人最终在这里遇到问题,让 IntelliJ 识别生成的源(红色突出显示导入、类等)。 Beware of the intellisense file size limit.注意智能感知文件大小限制。 If your generated protobuf code exceeds the default setitng of 2500KB then the file is ignored.如果您生成的 protobuf 代码超过了 2500KB 的默认设置,则该文件将被忽略。

Got Help -> Edit custom properties and add an entry appropriate for your case eg idea.max.intellisense.filesize=4000获得帮助 -> 编辑自定义属性并添加适合您案例的条目,例如 idea.max.intellisense.filesize=4000

Spent half a day faffing over different source set source folder, generated sources, and include / exclude directories.花了半天时间处理不同的源集源文件夹、生成的源和包含/排除目录。 Turned out I just need to increase this value原来我只需要增加这个值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM