通过引用其他存储库或项目或 jar 文件中的其他 protobuf 消息,分层组合 Protocol buffers (protobuf) 定义
Compose Protocol buffers (protobuf) definitions hierarchically by referencing other protobuf messages in other repositories or projects or jar files
总的来说,我有一个 protobuf(将其视为 class)数据类型,它引用另一个 Jar 文件中的另一个 protobuf,该 Jar 文件是我的 POM 文件中的依赖项。这非常适用于 .java 文件,但不幸的是不适用于 protobuf 文件。我能想到的最佳解决方案是告诉 Maven 在某个位置提取这个其他依赖 Jar(包含 proto 文件)文件,然后告诉 Maven 在该位置对所有这些 proto 文件进行 protoc 编译。 las,我不知道如何告诉 Maven 这样做。任何帮助将不胜感激,因为我们使用标准 Jar 文件来捕获我们的原型文件。
import "X.proto"; // refers to a proto file in another jar
import "Y.proto";
message A {
repeated X o = 1; // This cant be done
repeated Y e = 2;
}
由于 X 与该文件不在同一个路径中,因此上述方法将不起作用。
我在 Gradle 中找到了解决方案。填写下面的空白并正确指向你的回购协议,你应该能够让下面的工作正常进行。现在,您可以通过其他 jar 文件中的 proto 文件,跨多个项目分层组合 protobufs!
在gradle中,您可以使用以下内容:
// these are your protobuf plugin repositories
buildscript {
repositories {
maven {
url 'WHERE YOUR PROTOBUF PLUGIN EXISTS. e.g. http://maven or MavenCentral()'
}
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'
classpath 'com.google.protobuf:protoc:2.6.1'
}
}
apply plugin: 'com.google.protobuf'
group = "YOUR PACKAGE NAME. e.g. com.x.y"
version = "1.0" // release version
// these are your regular dependencies repositories. This could be
// very similar to the ones in buildscript above.
repositories {
mavenLocal()
}
// this is needed by the plugin and is where teh magic happens. It
// tells protobuf where to extract all the proto files that it finds
// in all the dependency jar files you have
protobuf {
generatedFilesBaseDir="$projectDir/src/"
}
// also magic you need for this protobuf plugin to work
sourceSets {
main {
// this tells the plugin where your project specific
// protofiles are located
proto {
srcDir 'src/main/resources/proto/'
}
java {
srcDir 'src/main/java'
}
}
}
dependencies {
compile 'com.google.protobuf:protobuf-java:2.6.1'
}
总的来说,我有一个 protobuf(将其视为 class)数据类型,它引用另一个 Jar 文件中的另一个 protobuf,该 Jar 文件是我的 POM 文件中的依赖项。这非常适用于 .java 文件,但不幸的是不适用于 protobuf 文件。我能想到的最佳解决方案是告诉 Maven 在某个位置提取这个其他依赖 Jar(包含 proto 文件)文件,然后告诉 Maven 在该位置对所有这些 proto 文件进行 protoc 编译。 las,我不知道如何告诉 Maven 这样做。任何帮助将不胜感激,因为我们使用标准 Jar 文件来捕获我们的原型文件。
import "X.proto"; // refers to a proto file in another jar
import "Y.proto";
message A {
repeated X o = 1; // This cant be done
repeated Y e = 2;
}
由于 X 与该文件不在同一个路径中,因此上述方法将不起作用。
我在 Gradle 中找到了解决方案。填写下面的空白并正确指向你的回购协议,你应该能够让下面的工作正常进行。现在,您可以通过其他 jar 文件中的 proto 文件,跨多个项目分层组合 protobufs!
在gradle中,您可以使用以下内容:
// these are your protobuf plugin repositories
buildscript {
repositories {
maven {
url 'WHERE YOUR PROTOBUF PLUGIN EXISTS. e.g. http://maven or MavenCentral()'
}
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'
classpath 'com.google.protobuf:protoc:2.6.1'
}
}
apply plugin: 'com.google.protobuf'
group = "YOUR PACKAGE NAME. e.g. com.x.y"
version = "1.0" // release version
// these are your regular dependencies repositories. This could be
// very similar to the ones in buildscript above.
repositories {
mavenLocal()
}
// this is needed by the plugin and is where teh magic happens. It
// tells protobuf where to extract all the proto files that it finds
// in all the dependency jar files you have
protobuf {
generatedFilesBaseDir="$projectDir/src/"
}
// also magic you need for this protobuf plugin to work
sourceSets {
main {
// this tells the plugin where your project specific
// protofiles are located
proto {
srcDir 'src/main/resources/proto/'
}
java {
srcDir 'src/main/java'
}
}
}
dependencies {
compile 'com.google.protobuf:protobuf-java:2.6.1'
}