我正在尝试将一个协议缓冲区消息导入到另一个协议缓冲区消息中,但导入无法识别。只要我不尝试将一个 protobuf 导入另一个 protobuf,就会生成 protobuf 代码(在 java 中),代码会按预期编译并运行。
我正在使用:
我的 gradle 构建文件如下所示:
plugins {
id 'java'
id 'com.google.protobuf' version "0.8.8"
}
group 'tech.tablesaw'
version '1.0-SNAPSHOT'
sourceCompatibility = 9.0
def grpcVersion = '1.30.1' // CURRENT_GRPC_VERSION
def protobufVersion = '3.12.0'
def protocVersion = protobufVersion
repositories {
mavenCentral()
}
test {
useJUnitPlatform()
}
dependencies {
implementation "io.grpc:grpc-protobuf:${grpcVersion}"
implementation "io.grpc:grpc-stub:${grpcVersion}"
compileOnly "org.apache.tomcat:annotations-api:6.0.53"
// advanced - need this for JsonFormat
implementation "com.google.protobuf:protobuf-java-util:${protobufVersion}"
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
compile group: 'tech.tablesaw', name: 'tablesaw-core', version: '0.38.1'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.2'
testImplementation "org.mockito:mockito-core:2.28.2"
}
protobuf {
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
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'
}
}
}
task TablesawServer(type: CreateStartScripts) {
mainClassName = 'tech.tablesaw.service.TableServiceServer'
applicationName = 'tablesaw-table-server'
outputDir = new File(project.buildDir, 'tmp')
}
task TablesawClient(type: CreateStartScripts) {
mainClassName = 'tech.tablesaw.service.TableServiceClient'
applicationName = 'tablesaw-table-client'
outputDir = new File(project.buildDir, 'tmp')
}
我的 gradle 信息如下所示:
------------------------------------------------------------
Gradle 5.1.1
------------------------------------------------------------
Build time: 2019-01-10 23:05:02 UTC
Revision: 3c9abb645fb83932c44e8610642393ad62116807
Kotlin DSL: 1.1.1
Kotlin: 1.3.11
Groovy: 2.5.4
Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM: 9.0.4 (Oracle Corporation 9.0.4+11)
OS: Mac OS X 10.13.5 x86_64
这是一个 protobuf 示例。导入column_type.proto失败。
syntax = "proto3";
package tech.tablesaw.service.common;
import "tech/tablesaw/service/common/column_type.proto";
option java_multiple_files = true;
option java_package = "tech.tablesaw.service.common";
option java_outer_classname = "ColumnMetaProto";
option objc_class_prefix = "TSW";
// Proto file describing column metadata message.
// A column metadata object
message ColumnMetadata {
string name = 1;
int32 size = 2;
ColumnTypeEnum.ColumnType column_type = 3;
}
这是我要导入的文件:
syntax = "proto3";
package tech.tablesaw.service.common;
option java_multiple_files = true;
option java_package = "tech.tablesaw.service.common";
option java_outer_classname = "ColumnTypeEnum";
option objc_class_prefix = "TSW";
enum ColumnType {
SHORT = 0;
INTEGER = 1;
LONG = 2;
FLOAT = 3;
BOOLEAN = 4;
STRING = 5;
DOUBLE = 6;
LOCAL_DATE = 7;
LOCAL_TIME = 8;
LOCAL_DATE_TIME = 9;
INSTANT = 10;
TEXT = 11;
SKIP = 12;
}
最后,这是 protobuf 在文件系统中的位置。
src > main > java
> proto > tech > tablesaw > service > common > column_metadata.proto
> column_type.proto
查看自述文件,其中描述了如何添加其他路径。
默认情况下,
intellij-protobuf-editor
使用项目配置的源根作为protobuf导入路径。如果这不正确,您可以覆盖 Settings > Languages & Frameworks > Protocol Buffers
中的这些路径。取消选中“自动配置”并添加您需要的路径。在您的情况下,您需要添加 .../src/main/java/proto
(其中 ...
表示项目的基本路径是什么)。
在另一个问题的评论中对此问题有答案:Error using import in .proto file
如果您使用的是 IntelliJ IDEA,请转到 Preferences -> Protobuf Support 并添加 .proto 文件的路径。这将解决该错误。
在更现代的 Intellij 版本中,这是在插件 intellij-protobuf-editor 中完成的。我将路径添加到原始源文件夹的根目录,一切都很好
想要描述什么对我有帮助,因为我在这里没有找到答案。 我的步骤适用于 Maven,但我认为 Gradle 的步骤几乎相同。
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.12.2:exe:${os.detected.classifier}</protocArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
build
部分是否有此扩展程序 <build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>
....
mvn protobuf:compile
如果您使用protobuf-gradle-plugin,则需要应用
idea
插件。使用后,一切都像魅力一样。来自他们的自述文件
apply plugin: 'idea'
clean {
delete protobuf.generatedFilesBaseDir
}
idea {
module {
// proto files and generated Java files are automatically added as
// source dirs.
// If you have additional sources, add them here:
sourceDirs += file("/path/to/other/sources");
}
}
这有时是由于在原始文件中使用导入而导致的,这些原始文件作为 Maven 依赖项添加到项目中。
要解决此问题,您可以在 Maven 构建中包含一个步骤,从库中解压原始文件(我在这里使用
proto-google-common-protos
作为示例)并将它们添加到“自动检测”的位置IntelliJ.
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<!-- This is a workaround for https://youtrack.jetbrains.com/issue/IDEA-285012/Protobuff-In-case-path-to-.proto-files-not-specified-in-Protocol-Buffers-settings-on-run-grpc-request-I-get -->
<id>unpack-google-common-protos</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-common-protos</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/generated-sources/protobuf/grpc-java</outputDirectory>
<includes>**/*.proto</includes>
</artifactItem>
<artifactItem>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
<includes>**/*.proto</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
我尝试取消选中“自动配置”,但对我来说,它不起作用。 我已经检查了“自动配置”参数并重新打开了 Intellij。之后,我可以将鼠标悬停在“错误”的导入上,IDE 将建议在小上下文菜单中添加这些导入。现在正在工作。