IntelliJ中Maven编译.proto文件失败及日志乱码解决求助
问题
使用IntelliJ IDEA 2024.3(旗舰版)内置的Maven 3.9.9编译项目中的.proto文件,项目结构如下:
my-project ├── src │ ├── main │ │ ├── resource │ │ │ └── exampleA.proto │ │ │ └── exampleB.proto ├── pom.xml
对应的pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <properties> <java.version>17</java.version> <protoc.version>3.19.3</protoc.version> <protobuf.plugin.version>0.6.1</protobuf.plugin.version> </properties> <dependencies> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>${protoc.version}</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>${protoc.version}</version> </dependency> </dependencies> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.7.1</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>${protobuf.plugin.version}</version> <extensions>true</extensions> <configuration> <!-- 工具版本 --> <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact> <!--默认值,proto源文件路径--> <protoSourceRoot>${project.basedir}/src/main/resources</protoSourceRoot> </configuration> <executions> <execution> <goals> <!--生成OuterClass类--> <goal>compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
编译.proto文件为.java文件时,出现错误:protoc did not exit cleanly. Review output for more information,同时错误日志存在乱码。需要解决两个问题:
- 如何解决错误日志中的乱码问题?
- 如何解决该编译错误?
解决方案
1. 解决错误日志乱码问题
调整IDEA控制台编码
- 打开IDEA的
File > Settings > Editor > File Encodings - 将
Global Encoding、Project Encoding、Default encoding for properties files全部设置为UTF-8 - 勾选
Transparent native-to-ascii conversion - 点击
Apply和OK后重启IDEA
添加Maven编码配置
在pom.xml的<properties>节点中追加编码相关配置:
<properties> <!-- 原有配置保留 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties>
配置Maven运行参数
- 打开
Run/Debug Configurations,找到对应的Maven编译配置(比如protobuf:compile) - 在
VM options中添加:-Dfile.encoding=UTF-8 - 保存配置后重新执行编译命令
2. 解决protoc did not exit cleanly编译错误
从现有配置和项目结构出发,按以下步骤排查修复:
修正目录名称拼写
项目结构中的resource目录拼写错误,标准Maven资源目录应为resources(复数形式)。将src/main/resource重命名为src/main/resources,确保pom.xml中的protoSourceRoot路径与实际目录一致。
检查.proto文件语法合法性
- 确保
exampleA.proto和exampleB.proto符合protobuf 3.19.3语法规范,比如文件开头必须声明syntax = "proto3";(使用proto3语法时) - 检查消息定义、字段类型、字段编号等是否存在语法错误
- 确认没有引用不存在的proto文件或包
升级protobuf插件及protoc版本
当前使用的protobuf-maven-plugin0.6.1版本较旧,与Maven 3.9.9、Java 17可能存在兼容性问题。建议升级到最新稳定版本,并同步更新protoc版本:
<properties> <protoc.version>3.25.3</protoc.version> <protobuf.plugin.version>0.7.2</protobuf.plugin.version> </properties>
手动指定protoc可执行文件路径
如果os-maven-plugin自动获取protoc失败,可在protobuf-maven-plugin的configuration中手动指定本地路径:
<configuration> <!-- 原有配置保留 --> <protocPath>/path/to/your/protoc/exe</protocPath> </configuration>
替换为本地protoc的绝对路径,比如Windows下的C:\protoc\bin\protoc.exe,Linux/macOS下的/usr/local/bin/protoc
清理缓存并重新构建
- 执行Maven命令:
mvn clean install -U,强制更新依赖并清理旧编译产物 - 在IDEA中点击
File > Invalidate Caches... > Invalidate and Restart,清理IDEA缓存后重启
内容的提问来源于stack exchange,提问作者A salted fish without dreams
相关产品推荐
相关产品推荐

