Generate DDL with Hibernate Tools, JPA and Maven

It is very convenient to get the SQL you need to setup your database generated by you. The information is all there in your JPA annotated classes.

Hibernate Tools

Hibernate provides for that purpose the really great Hibernate Tools. But these tools are eclipse or ANT based. No official Maven support.


Indeed there exists a Maven plugin at Codehaus, the hibernate3-maven-plugin. But this is (as the name states) only for hibernate 3.x and does not support hibernate 4.
Searching in the net revealed, that it could take a while until the new hibernate version would be supported (e.g.

Solution: ant-run Plugin

Luckily Maven has the possibility to execute ANT tasks. So the obvious solution is to just use the ant-run plugin and execute the hibernate-tools ANT target. Well, would be if you are an expert in Maven AND ANT classpaths :-). For me it took several hours to find the following solution:

I use the test classpath because I already create (with the help of the jpa-maven-plugin) a persistence.xml with all classes included for testing purposes. Just added a more production like unit to be used:

and the pom plugin entries for generating the entity class list, inserting it into the template persitence.xml and adding this directory to the test classpath: