In integration tests, I often need to setup the database to a determined state before every test case. In order to do that, we need to create database schema which reflect your current models and then populate with proper data. In today post, I will show you how to generate the database schema from your Hibernate models by using the Hibernate Maven Plugin (3.0). In my shop, we are still using the "old-school" Hibernate mapping: XML!. The reason is we want to keep our models pristine (no annotations or whatever). We use Maven as our build tool, so here is the plugin configuration that works:
We use Spring to configure Hibernate directly, the hibernate.cfg.xml used here is just for this purpose only (ie. generating the database schema). Here is it just for the sake of completeness:org.codehaus.mojo hibernate3-maven-plugin 3.0 create-schema process-test-resources run
org.hibernate.dialect.HSQLDialect org.hsqldb.jdbcDriver jdbc:hsqldb:mem:sample_db sa
There are other operations that you can utilize with Hibernate Tools. For more information, please refer to Hibernate Tools documentation and the hibernate3-maven-plugin site
.I've recently bought an Intel 520 SSD, so my top priority for my laptop is to reduce the disk write during daily use. In the Java world, Maven is one of the most common build tool out there. My most used Maven command is:
mvn clean package
This command will do a fresh build of the project, then copy all the compiled classes/resources to the build directory of Maven (which is the folder 'target', next to your source directory). If you have a large Maven project at hand, this command will perform thousands disk write (clean compiled classes and resources, then compile Java classes and assemble the resources into WAR/JAR file). By changing the build directory of Maven to /tmp (which is a tmpfs mountpoint (residing on RAM on my ArchLinux)), I can now rebuild my project as many times as I like without the write amplication concern. Here is now I do it in Maven:
- Define a property name target.directory with the default value is target
- Under the build section in your POM file, add this tag <directory>${target.directory}</directory>
- Modify your settings.xml file to include this snippet in your active profile:
/tmp/maven/${project.groupId}-${project.artifactId}/target
And the sample settings.xml file:4.0.0 com.abc abc war 1.0 Sampe Webapp http://www.abc.com target junit junit 3.8.1 test abc ${target.directory} maven-eclipse-plugin 2.8 true true target target 2 ${project.build.outputDirectory}
Happy coding!development /tmp/maven/${project.groupId}-${project.artifactId}/target public-snapshots development