views:

1006

answers:

3

Hi, I need to be able to call mvn clean install and have maven call hibernate3:hbm2hbmxml to generate the mapping files from a database and after than call hbm2java to get the Java files and then have maven compile those newly created Java files. Has anyone done this before?

Thanks

A: 

Maven lifecycle

mvn clean dependency:copy-dependencies package

If this were to be executed, the clean phase will be executed first (meaning it will run all preceeding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceeding build phases of the default lifecycle).

So, perhaps:

mvn clean hibernate3:hbm2hbmxml hibernate3:hbm2java package

That said, I'd recommend against perpetually generating classes. This makes you very inflexible.

After your comment, it seems like an "unwise" behaviour from the hibernate plugin. You can bypass that by "manually" copying the required files to the desired directory, using the Maven antrun plugin.

Bozho
Bozho, this is not exactly how I understood the question (which isn't trivial at all, the wanted workflow involves really tricky configuration of the hibernate3 plugin). But maybe I missed something.
Pascal Thivent
I also wasn't sure I'm getting everything, but at least he should give it a try this way, and see what happens.
Bozho
Thanks for the reply. What I'm looking for is to be able to make those goals part of my continuous integration process. I managed to make hbm2hbmxml work but places the *.hbm.xml files under ./target/hibernate3/generated-mappings/mypackage. When I run hbm2java I get a failure saying "mypackage/Domain.hbm.xml not found". Shouldn't the plugin know where to find those files?I made that run by adding the <outputdirectory> entry and hbm2java generates the source code but again, it places the Java files under the target folder and when I compile it doesn't compile those files. Any clues? Thanks
sebastianr
A: 

the following config works for me. (sample is with Derby database and 1 table)
mvn clean package does it all.

the plugin configuration:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
    <execution>
        <id>hbm2hbmxml</id>
        <phase>generate-sources</phase>
        <goals>
            <goal>hbm2hbmxml</goal>
        </goals>
        <configuration>
            <components>
                <component>
                    <name>hbm2hbmxml</name>
                    <outputDirectory>src/main/resources</outputDirectory>
                </component>
            </components>
        </configuration>
    </execution>
    <execution>
        <id>hbm2java</id>
        <phase>generate-sources</phase>
        <goals>
            <goal>hbm2java</goal>
        </goals>
        <configuration>
            <components>
                <component>
                    <name>hbm2java</name>
                    <implementation>configuration</implementation>
                </component>
            </components>
            <componentProperties>
                <jdk5>true</jdk5>
                <configurationfile>/src/main/resources/hibernate.cfg.xml
                </configurationfile>
            </componentProperties>
        </configuration>
    </execution>
</executions>
<dependencies>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.4.2.0</version>
    </dependency>
</dependencies>

hibernate.cfg.xml:

<hibernate-configuration>
<session-factory>
    <property name="connection.url">jdbc:derby://localhost:1527/demo</property>
    <property name="connection.username">app</property>
    <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
    <property name="connection.password">password</property>
    <property name="hibernate.show_sql">true</property>

    <mapping resource="Tag.hbm.xml" />
</session-factory>

Stefan De Boey
A: 

Hola,

If you want to have your model java files (obtained by reveng) compiled, you don't need to run hbm2hbmxml.

plugin configuration:

    <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <components>
                    <component>
                        <name>hbm2java</name>
                        <outputDirectory>src/main/java</outputDirectory>
                        <implementation>jdbcconfiguration</implementation>
                    </component>
                </components>
                <componentProperties>
                    <revengfile>/src/main/resources/reveng/model.reveng.xml</revengfile>
                    <propertyfile>/src/main/resources/META-INF/hibernate.properties</propertyfile>
                    <jdk5>true</jdk5>
                    <ejb3>true</ejb3>
                </componentProperties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.0.8</version>
                </dependency>
                <dependency>
                    <groupId>cglib</groupId>
                    <artifactId>cglib-nodep</artifactId>
                    <version>2.1_3</version>
                </dependency>
            </dependencies>             
        </plugin>
    </plugins>
</build>

hibernate.properties :

hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/YOUR_DB
hibernate.connection.username = yourUsrName
hibernate.connection.password= yourPwd
hibernate.default_schema = YOUR_DB

model.reveng.xml :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd"&gt;
<hibernate-reverse-engineering>

    <table-filter match-name=".*" package="your.package.here/>


</hibernate-reverse-engineering>

you fire this with:

mvn clean hibernate3:hbm2java compile

if you want it to be fired just with:

mvn clean compile

add the "executions" tag in your plugin definition

                <executions>
                <execution>
                    <phase>compile</phase>
                    <goals><goal>hbm2java</goal></goals>
                </execution>
            </executions>