June 28, 2007

Maven2 Hibernate Plugin and Spring

Filed under: Java — Chetan @ 2:43 pm

Continuing my explorations on Maven I had a requirement to generate the database schema using Hibernate tool feature. And maven had a plugin readymade for this. The maven plugin expects the hibernate configuration through the config file but we were using Spring’s LocalSessionFactoryBean to maintain the Hibernate configuration.

Stuck with that I downloaded the source which is scattered over multiple sub modules. Browsing through that I saw few annotations like
@component role=”org.codehaus.mojo.hibernate3.configuration.ComponentConfiguration” over some attributes. Now that was something new and I had no idea what it was meant for. Searching about it I found ( should have known) that Maven internally uses Plexus which is an IOC container similar to Spring.

That gave me the only hope of using the plugin without modifying it and not wasting much time. The plexus container works on the concept of Role. Let me explain that a bit with maven plugin. The plugin uses an interface ComponentConfiguration for retrieval of the Hibernate configuration from various sources like config file,annotation etc. Thus in theory I can plugin my own implementation which gets the config from Spring container. Now the question was how to inject it.

The Plexus container works on the concept of Role. Have a look at this 5 min tutorial at there site. Here is the interface for the ComponentConfiguration

public interface ComponentConfiguration {
    String ROLE = ComponentConfiguration.class.getName();

    Configuration getConfiguration( ExporterMojo exporterMojo )
        throws MojoExecutionException;

    String getName();

The ROLE field acts as a key for the container.The mapping of implementation to the role is done through an xml located at META-INF/plexus/components.xml inside the jar. So here was my mapping


Next comes the questions how the plugin would pick a particular implementation. Here the role-hint comes. The HibernateExporterMojo which all the mojo’s extend has a field

     * @component role="org.codehaus.mojo.hibernate3.configuration.ComponentConfiguration"
     * @noinspection MismatchedQueryAndUpdateOfCollection
    private List componentConfigurations = new ArrayList();

So when Maven creates the plugin class then Plexus populates componentConfigurations with the implementations of ComponentConfiguration. And thus our implementation also gets injected.Now only task left is the final wiring of all these things through pom.xml


Here though implementation property we tell the plugin to use my implementation for getting Configuration. Then through dependencies I add my class’s jar to plugin classpath. The hibernate jar was required as plugin was picking some wrong jar and causing problems.Thats all that was required.

Execute the plugin through mvn hibernate3:hbm2ddl and get the ddl script generated for the schema.



  1. Great exploration of Maven2 ! I have to take sometime to have a look into this…

    Thanks a lot for taking the time to post your findings !

    Comment by Guillaume — September 27, 2007 @ 12:33 pm

  2. Hi!
    This is nice! I was looking for something like this to generate the db scripts.
    I know this is an old post, so do you know if there’s an “official” way to do this?
    Or, is your mojo available somewhere so I can reuse it?
    Without the “example.hibernatetool.SpringComponentConfiguration” class along with the jar plugin, it still needs some work to get it done. So, do you have a GPL o LGPL license for it?


    Comment by Javier Fernandes — November 11, 2008 @ 12:23 am

  3. Hi Javier,

    I did not checked on this part again so cannot say if there is any official way for this. I digged my old source code and got the code. Its uploaded at Try it and see if it works for you.

    Comment by Chetan — November 15, 2008 @ 7:16 am

  4. Dear Chetan Sir,

    Please keep posting this kind of blogs. It is helping a lot many developers.


    Comment by Chandan Mishra — May 1, 2009 @ 8:33 pm

  5. Dear Chetan Sir,

    where can I find the “META-INF/plexus/components.xml”

    forgive my ignorance

    Comment by eduardo — August 5, 2010 @ 3:03 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Blog at

%d bloggers like this: