Friday, July 1, 2011

Hibernate "import.sql" Encoding when Running Unit Tests in Maven

Ok, the title is quite horrible. The story goes like this: We have decided to use HSQLDB in memory database for our unit tests. It's just a matter of JDBC configuration. And when you tell hibernate to create your database (hibernate.hbm2ddl.auto=create), it looks for import.sql file that runs after database is created. If you have it, it can populate your database.
But if you use different than default platform encoding in your sql file, you may have troubles.

You must then set your encoding (-Dfile.encoding=UTF8). Sounds easy, but setting the encoding for Maven does not help. The trick is that maven-surefire-plugin runs in separate JVM.

So what I needed is just one more line:


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

3 comments:

  1. Help please! It is plugin work for tests jUnit, but ... How I can set utf8 for import.sql in running application (no for tests).

    ReplyDelete
  2. Thanks for the trick, it helped a lot!

    ReplyDelete