使用Maven运行时Unicode字符乱码问题求助
Maven运行TestNG测试时Unicode乱码问题解决方案
以下是几种针对性的解决办法,按优先级尝试:
1. 配置Maven Surefire插件编码
Maven Surefire插件执行测试时默认编码可能不匹配,直接在pom.xml的build节点下添加插件配置,强制指定UTF-8编码:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <!-- 替换为你使用的插件版本 --> <configuration> <argLine>-Dfile.encoding=UTF-8</argLine> </configuration> </plugin> </plugins> </build>
2. 全局设置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>
3. 运行Maven时指定编码参数
如果上述配置不生效,可以直接在执行测试命令时追加编码参数:
mvn test -Dfile.encoding=UTF-8
也可以设置系统环境变量JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8,让所有Java进程默认使用UTF-8编码,一劳永逸。
4. 验证测试文件本身的编码
确保你的测试代码文件是以UTF-8格式保存的。多数IDE默认使用系统编码,需要手动调整(比如IntelliJ IDEA中进入File > Settings > Editor > File Encodings,将所有编码选项设为UTF-8)。
5. 检查TestNG配置文件编码
如果使用了TestNG的xml配置文件,确认该文件也是以UTF-8保存,且配置中没有错误的编码设置。
内容的提问来源于stack exchange,提问作者Suresh K
相关产品推荐
相关产品推荐

