简体   繁体   English

使用gradle时找不到Protobuf生成的类

[英]Protobuf generated classes cannot be found when using gradle

If I follow the instruction in the grpc-java readme and I use maven, the protobuf generated files appear in the target directory and are subsequently in the classpath for me to extend etc. However, when I use gradle, the generated classes appear in the build directory and are absent from the classpath. 如果我遵循grpc-java自述文件中的说明,并且使用了maven,则protobuf生成的文件将出现在target目录中,并随后在类路径中供我扩展等。但是,当我使用gradle时,生成的类将出现在build目录,并且不在类路径中。 I'm fairly new to gradle so I'm not really sure why it's behaving so differently. 我是新手,所以我不太确定为什么它会表现得如此与众不同。

My build.gradle file 我的build.gradle文件

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

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

group 'co.example'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'io.grpc:grpc-netty-shaded:1.15.1'
    compile 'io.grpc:grpc-protobuf:1.15.1'
    compile 'io.grpc:grpc-stub:1.15.1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.5.1-1"
    }
    //noinspection GroovyAssignabilityCheck
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.15.1'
        }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
        }
    }
}

It looks like for gradle (if you want to use the generated stubs/server interfaces) in the project where your proto files live in (ie the project is not just for generating jars and publishing them) then you'll need to add generatedFilesBaseDir to your build.gradle file: 它看起来像gradle这个(如果你想使用生成的存根/服务器接口),在您的项目proto文件存在(即该项目不仅是用于生成jar文件并将其发布到),那么你就需要添加generatedFilesBaseDir到您的build.gradle文件:

protobuf {
    generatedFilesBaseDir = "$projectDir/src/main/java/generated"
    ...
}

Once you've done this, the stubs should be in your classpath. 完成此操作后,存根应位于您的类路径中。

public class SomeServer extends MyProtoClassGrpc.PDFExtractImplBase {}

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

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