zoukankan      html  css  js  c++  java
  • 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>
     
  • 相关阅读:
    [转] Chrome
    阿里安全潘多拉实验室首先完美越狱苹果iOS 11.2
    【阿里聚安全·安全周刊】阿里双11技术十二讲直播预约|AWS S3配置错误曝光NSA陆军机密文件
    卡巴斯基发布安全公告:2018年威胁预测
    【阿里聚安全·安全周刊】双十一背后的“霸下-七层流量清洗”系统| 大疆 VS “白帽子”,到底谁威胁了谁?
    分享一个白帽交流灵感的社区——先知技术安全社区
    WiFi网络WPA2 KRACK漏洞分析报告
    #云栖大会# 移动安全专场——APP渠道推广作弊攻防那些事儿(演讲速记)
    #云栖大会# 移动安全专场——APP加固新方向(演讲速记)
    Java安全编码:糟糕的在线建议和令人困惑的APIs
  • 原文地址:https://www.cnblogs.com/exmyth/p/12451855.html
Copyright © 2011-2022 走看看