如何使用协议缓冲区编译器为我的服务生成 java 接口而不是抽象类?
现在我使用一个 Gradle 插件,它接受.proto文件并为我的服务而不是接口生成抽象类。鉴于 Java 不允许您扩展多个类,这可能会产生问题。
在浏览文档后,到目前为止我找不到解决方案或方法,所以任何帮助都会很棒。
我的 build.gradle 看起来像这样
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
// ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
// gradle versions
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
repositories {
mavenCentral()
mavenLocal()
}
// IMPORTANT: You probably want the non-SNAPSHOT version of gRPC. Make sure you
// are looking at a tagged version of the example and not "master"!
// Feel free to delete the comment at the next line. It is just for safely
// updating the version in our release process.
def grpcVersion = '1.0.0' // CURRENT_GRPC_VERSION
dependencies {
compile "io.grpc:grpc-netty:${grpcVersion}"
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
}
protobuf {
protoc {
// The version of protoc must match protobuf-java. If you don't depend on
// protobuf-java directly, you will be transitively depending on the
// protobuf-java version that grpc depends on.
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {
// To generate deprecated interfaces and static bindService method,
// turn the enable_deprecated option to true below:
option 'enable_deprecated=false'
}
}
}
}
// Inform IntelliJ projects about the generated code.
apply plugin: 'idea'
idea {
module {
// Not using generatedSourceDirs because of
// https://discuss.gradle.org/t/support-for-intellij-2016/15294/8
sourceDirs += file("${projectDir}/build/generated/source/proto/main/java");
sourceDirs += file("${projectDir}/build/generated/source/proto/main/grpc");
}
我发现了它不生成接口的原因,看起来它已被弃用,所以我不确定生成接口的方法是什么。如果您查看我的build.gradle option 'enable_deprecated=false'
将其打开为 true 将生成接口,但是注释说它已被弃用,所以我不确定生成接口的新方法是什么。我想要接口而不是抽象类。