maven maven-surefire-plugin的乱码问题

今天项目中出现奇怪问题,在eclipse中直接运行TestNG时,全部都OK,但是执行mvn test时却失败.观察其输出日志,发现有乱码,怀疑是乱码导致.

最终在官网发现蛛丝马迹.

maven-surefire-plugin是运行mvn test时执行测试的插件,其有一个配置参数forkMode,默认为once,即表示每次运行test时,新建一个JVM进程运行所有test.这可能会导致乱码问题.首先将forkMode设置为never,即不新建.再运行mvn test,全部OK了.果然是这个问题!!

于是再找官方参数说明,发现了argLine参数,这个参数与forkMode一起使用,可以设置新建JVM时的JVM启动参数,于是设置<argLine>-Dfile.encoding=UTF-8</argLine>,明确指定一下JVM的file.encoding,并将forkMode从never改回once,还是每次新建一个JVM进程.再次运行mvn test,一起OK了,问题解决.

究其原因,是因为MAVEN的maven-surefire-plugin在新建JVM进程后,由于没有指定encoding,采用了OS的默认编码,导致读取UTF-8文件(测试类的sql脚本文件)时出现乱码,最终影响了TestNG的正确执行.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>${argLine} -Xmx1024m -Dfile.encoding=UTF-8</argLine>
                    <!--<skipTests>true</skipTests>-->
                </configuration>
            </plugin>
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring.boot.version}</version>
                <configuration>
                    <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
                    <fork>true</fork>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <configuration>
                    <destFile>target/jacoco-unit.exec</destFile>
                    <dataFile>target/jacoco-unit.exec</dataFile>
                    <excludes>
                        <exclude>**/entity/**</exclude>
                        <exclude>**/mapper/**</exclude>
                        <exclude>**/exception/**</exclude>
                        <exclude>**/*Application.class</exclude>
                        <exclude>**/*Builder.class</exclude>
                        <exclude>**/*BO.class</exclude>
                        <exclude>**/*DO.class</exclude>
                        <exclude>**/*DTO.class</exclude>
                        <exclude>**/*VO.class</exclude>
                    </excludes>
                    <!--<includes>
                        <include>**/service/**</include>
                        <include>**/controller/**</include>
                    </includes>-->
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!--这个check:对代码进行检测,控制项目构建成功还是失败-->
                    <execution>
                        <id>check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                    <!--这个report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果-->
                    <execution>
                        <id>jacoco-site</id>
                        <phase>package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>${argLine} -Xmx1024m -Dfile.encoding=UTF-8</argLine>
                    <!--<skipTests>true</skipTests>-->
                </configuration>
            </plugin>
        </plugins>
    </build>
 
原文地址:https://www.cnblogs.com/exmyth/p/12451855.html