我正在尝试在 Java 项目中生成 Protobuf,该项目在另一个 Git 存储库中定义,我想将其添加为 Git 子模块。我的 build.gradle
包含
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:4.0.0-rc-2"
}
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'
}
}
}
我已将 protobufs 存储库(称为 my-protobufs
)包含在 src/main/proto
目录中。 Protobuf 又位于 my-protobufs
的 proto
子目录中。部分目录结构如下所示:
src/main/proto/edm-grpc-protobufs/proto
├── mypackage
│ └── v1
│ ├── bar.proto
│ └── foo.proto
foo.proto
文件有一个 import
语句,如下所示:
import "mypackage/v1/bar.proto";
这是因为在该存储库中,Protobuf 路径是 proto
目录。问题是,当我尝试 ./gradlew build
时,我收到如下错误:
> Task :generateProto FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':generateProto'.
> protoc: stdout: . stderr: mypackage/v1/bar.proto: File not found.
my-protobufs/proto/mypackage/v1/foo.proto:5:1: Import "axmorg/v1/bar.proto" was not found or had errors.
my-protobufs/proto/mypackage/v1/foo.proto:10:5: "SourceType" is not defined.
问题基本上是 --proto_path
(用 protoc
的说法)或搜索导入的目录未正确定义,因此 protobuf-gradle-plugin
不知道在哪里可以找到它们。是否可以更新 build.gradle
来指定此路径?
最佳答案
我在模具文档中找到了这个: https://github.com/google/protobuf-gradle-plugin#customizing-source-directories
sourceSets {
main {
proto {
// In addition to the default 'src/main/proto'
srcDir 'src/main/protobuf'
srcDir 'src/main/protocolbuffers'
// In addition to the default '**/*.proto' (use with caution).
// Using an extension other than 'proto' is NOT recommended,
// because when proto files are published along with class files, we can
// only tell the type of a file from its extension.
include '**/*.protodevel'
}
java {
...
}
}
test {
proto {
// In addition to the default 'src/test/proto'
srcDir 'src/test/protocolbuffers'
}
}
}
关于java - 如何使用protobuf-gradle-plugin指定Protobuf路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66794075/