I´m busy converting an existing project from an Ant build to one using Maven. Part of this build includes using the hibernate hbm2java tool to convert a collection of .hbm.xml files into Java. Here's a snippet of the Ant script used to do this:
<target name="dbcodegen" depends="cleangen"
description="Generate Java source from Hibernate XML">
<hibernatetool destdir="${src.generated}">
<configuration>
<fileset dir="${src.config}">
<include name="**/*.hbm.xml"/>
</fileset>
</configuration>
<hbm2java jdk5="true"/>
</hibernatetool>
</target>
I've had a look around on the internet and some people seem to do this (I think) using Ant within Maven and others with the Maven plugin. I'd prefer to avoid mixing Ant and Maven. Can anyone suggest a way to do this so that all of the .hbm.xml files are picked up and the code generation takes place as part of the Maven code generation build phase?
Thanks!
Adam.
Well, there is the Maven Hibernate3 Plugin if you don't want to mix Ant and Maven (which is a good idea here IMO). It has a
hbm2java
goal which is bound by default to thegenerate-sources
phase. Refer to the website of the Mojo for more details but the setup of the plugin might looks like something like this:EDIT: The plugin actually looks for
.hbm.xml
intarget/classes
to generate the java source files. So, if you put your mapping files insrc/main/resources
, they will get copied intotarget/classes
during theprocess-resources
phase which is invoked by the plugin and things will just work. I've just tested this with the following sample project:The
pom.xml
is almost empty, it just contains the plugin configuration seen above and a junit dependency. Thehibernate.cfg.xml
contains:And
Person.hbm.xml
looks as follow:With this configuration, running
mvn install
generatesPerson.java
as shown below:Everything works as described.