【问题标题】:Encoding issue with data.sql in Spring Boot with Maven使用 Maven 在 Spring Boot 中的 data.sql 编码问题
【发布时间】:2021-01-25 13:37:11
【问题描述】:

我正在尝试测试一个包含 Spring Data JPA 实体和存储库的数据访问库。

我的 SpringBootTests 创建一个数据库并使用 schema-test.sql 和 data-test.sql 自动填充它

在我的 application.properties 中:

spring.datasource.initialization-mode=always
spring.datasource.platform=test
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.hibernate.ddl-auto=none

在我的 data-test.sql 中,我有一些拉丁字符。比如:

INSERT INTO TABLE
(ID, NAME)
VALUES
(1, 'Donnée');

我的 data-test.sql 文件是 UTF-8 编码的。我用 Vim :se fenc 和 Eclipse 加倍检查:

当我从 Eclipse 启动 JUnit 测试时,一切正常,我的测试通过了。但是当我从 Eclipse 启动一个 maven 包目标时,我的测试失败了,如下所示:

Expecting:
  <Optional[TableEntity [id=1, name=Donnée]]>
to contain:
  <TableEntity [id=1, name=Donnée]>
but did not.

maven 似乎没有将我的 data-test.sql 文件解释为 UTF-8 编码文件,而是 CP-1252 编码文件。

我在 pom.xml 中添加了以下内容:

<properties>
    <java.version>11</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

还有:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>UTF-8</encoding>
        <!-- Latin1 is still the default for .properties files -->
        <propertiesEncoding>ISO-8859-1</propertiesEncoding>
    </configuration>
</plugin>

在 Eclipse “运行配置”中,我添加了以下 VM 参数:

-Dfile.encoding=UTF-8

我还强制在“通用”选项卡中进行编码:

在控制台中,我发现:

[DEBUG] Copying file application.properties
[DEBUG] file application.properties has a filtered file extension
[DEBUG] Using 'ISO-8859-1' encoding to copy filtered resource 'application.properties'.
[DEBUG] copy (...)\src\test\resources\application.properties to (...)\target\test-classes\application.properties
[DEBUG] Copying file data-test.sql
[DEBUG] file data-test.sql has a filtered file extension
[DEBUG] Using 'UTF-8' encoding to copy filtered resource 'data-test.sql'.
[DEBUG] copy (...)\src\test\resources\data-test.sql to (...)\target\test-classes\data-test.sql
[DEBUG] Copying file schema-test.sql
[DEBUG] file schema-test.sql has a filtered file extension
[DEBUG] Using 'UTF-8' encoding to copy filtered resource 'schema-test.sql'.
[DEBUG] copy (...)\src\test\resources\schema-test.sql to (...)\target\test-classes\schema-test.sql

是否存在强制 Maven 或我的 SpringBootTest 类将 SQL 文件解释为旧的 CP-1252 或 ISO-8859-1 编码的隐藏选项?

一个完整的例子是可用的on GitHub

【问题讨论】:

    标签: eclipse spring-boot maven character-encoding


    【解决方案1】:

    显示您的 Eclipse 属性。 也应该是“UTF-8”。

    数据库中的编码

    检查 H2 数据库的编码。 所以设置hibernate.hbm2ddl.charset_name = "UTF-8" 当你使用spring boot时,你也可以设置如下:

    spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;
    

    【讨论】:

    • 是的,我的 data.sql 在 Eclipse 中显示为 UTF-8(我添加了一个屏幕截图)。提醒:当我启动测试(作为 JUnit 测试运行)时,Eclipse 中一切正常。仅当我尝试启动 maven“包”时才会出现此问题。
    • 我更新了我的回复。你能检查H2数据库的编码吗?在通过测试代码读取值之前,您可以添加我命名的属性,或者只是在 H2 数据库中设置一个断点并观察自己。
    • 我不确定我是否正确:我在 application.properties 中添加了spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;。 Eclipse 无法识别“connectionProperties”部分。我还尝试添加hibernate.hbm2ddl.charset_name=UTF-8spring.jpa.hibernate.hbm2ddl.charset_name=UTF-8,在这两种情况下,Eclipse 都无法识别“hbm2ddl”部分。结果是一样的。使用“maven path”时,我的编码问题仍然存在。
    • 我更新了帖子,其中包含指向在 GitHub 上重现该问题的示例项目的链接。
    【解决方案2】:

    编码问题似乎来自surefire插件。我在 pom.xml 的 &lt;plugins/&gt; 中添加了以下几行:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>-Dfile.encoding=UTF-8</argLine>
            </configuration>
        </plugin>
    

    问题已解决:似乎 Surefire 在其自己的 JVM 中启动了测试,采用 ISO-8859-1 编码。

    固定项目可用on GitHub

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2019-07-25
      • 2021-09-29
      相关资源
      最近更新 更多