使用IntelliJ中的Gradle导入Protobuf生成的类 [英] Import Protobuf generated classes using Gradle in IntelliJ

查看:492
本文介绍了使用IntelliJ中的Gradle导入Protobuf生成的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Gradle导入Protobuf生成的类时遇到麻烦.

I am having trouble importing Protobuf generated classes using Gradle.

这是我的项目树的样子:

This is how my project tree looks like:

我尝试将软件包标记为Source,尝试了所有可能的导入组合:

I've tried marking the packages as Source, I have tried all possible combinations of imports:

import generated.main.grpc.GreeterGrpc;
import main.java.HelloRequest;
import java.*;
import HelloRequest;

它们都不起作用.这是我的build.gradle:

None of them works. Here is my build.gradle:

group 'andu'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'com.google.protobuf'

sourceCompatibility = 1.5

repositories {
    mavenCentral()
    maven { url "https://plugins.gradle.org/m2/" }
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile 'io.grpc:grpc-protobuf:1.0.0-pre2'
    compile 'com.google.protobuf:protobuf-java:3.0.0'
    compile 'io.grpc:grpc-stub:1.0.0-pre2'
    compile 'io.grpc:grpc-netty:1.3.0'
    compile 'io.grpc:grpc-protobuf:1.3.0'
    compile 'io.grpc:grpc-stub:1.3.0'
}



sourceSets {
    main {
        proto {
            srcDir 'src/main/proto'
        }
        java {
            srcDirs =  ['src/main/java', 'generated/main/java']
        }
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.2.0"
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.3.0'
        }
    }

    generateProtoTasks.generatedFilesBaseDir = 'generated'

    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

添加之前

generateProtoTasks.generatedFilesBaseDir = 'generated'

我所有生成的类都将添加到build/generation/main/java

all of my generated classes would be added to build/generated/main/java

推荐答案

来自: Protobuf插件假定Protobuf文件(* .proto)在sourceSets中的组织方式与Java源文件相同. sourceSet的Protobuf文件在一次协议运行中进行编译,并将生成的文件添加到该sourceSet()的Java编译运行的输入中.

The Protobuf plugin assumes Protobuf files (*.proto) are organized in the same way as Java source files, in sourceSets. The Protobuf files of a sourceSet are compiled in a single protoc run, and the generated files are added to the input of the Java compilation run of that sourceSet ().

sourceSets {
    main {
        java {
            srcDirs 'build/generated/source/proto/main/grpc'
            srcDirs 'build/generated/source/proto/main/java'
        }
    }
}

这篇关于使用IntelliJ中的Gradle导入Protobuf生成的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆