使用Maven插件编译Protobuf遇‘protoc did not exit cleanly’错误的解决方法
我已在Maven中引入用于编译Protobuf的插件,对应的pom.xml配置如下:
<plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact> <protocExecutable>/home/pengpeng/tools/protoc/bin/protoc</protocExecutable> <protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot> <outputDirectory>${basedir}/src/main/java</outputDirectory> <clearOutputDirectory>false</clearOutputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin>
我的proto文件内容如下:
syntax = "proto3"; //option java_multiple_files = true; package com.ifx.connect.proto; option java_package = "com.ifx.connect.proto"; option java_outer_classname = "Message"; message Message{ int32 header = 1; string jwt = 2; // java-web-token }
尝试编译Protobuf时出现错误:protoc did not exit cleanly. Review output for more information.,此前从未运行过protoc,请问该如何解决?
检查protoc可执行文件的有效性与权限
直接在终端执行命令:/home/pengpeng/tools/protoc/bin/protoc --version,验证protoc是否能正常运行。如果提示权限不足,执行chmod +x /home/pengpeng/tools/protoc/bin/protoc添加可执行权限;如果提示文件不存在,确认路径是否正确,或重新下载适配当前系统的protoc版本。移除冲突的Maven配置项
pom.xml中同时配置了protocArtifact和protocExecutable,这两个配置互斥:前者让插件自动下载protoc,后者指定本地已有的protoc实例。保留其中一个即可,建议保留protocExecutable(因为你已存在本地protoc),删除protocArtifact这一行配置。修复proto文件的命名冲突
你的proto文件里,java_outer_classname设置为Message,同时内部又定义了同名的message Message,这会导致编译冲突。将外部类名修改为不同名称,比如MessageProto,修改后的proto文件示例:syntax = "proto3"; //option java_multiple_files = true; package com.ifx.connect.proto; option java_package = "com.ifx.connect.proto"; option java_outer_classname = "MessageProto"; message Message{ int32 header = 1; string jwt = 2; // java-web-token }查看详细错误日志定位问题
执行Maven编译时添加-X参数:mvn clean compile -X,查看完整的调试输出,其中会包含protoc返回的具体错误信息,能帮助精准定位问题根源。
内容的提问来源于stack exchange,提问作者Peng

