Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure maven hbm2hbmxml and hbm2java to run one after the other in mvn clean install

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

like image 331
sebastianr Avatar asked Jan 18 '10 03:01

sebastianr


People also ask

What is the difference between mvn clean install and mvn clean package?

mvn package command will compile source code and also package it as a jar or war as per pom file and put it into the target folder(by default). mvn install command will compile and package, but it will also put the package in your local repository.

What does this Maven command do mvn clean install?

mvn clean: Cleans the project and removes all files generated by the previous build. mvn compile: Compiles source code of the project.

Which command is used to run the clean life cycle followed by verify install and package with Maven?

When you call mvn deploy , mvn will also execute every lifecycle phase before deploy , in order: validate , compile , test , package , verify , install .

How run Maven clean install command line?

1) Open the command prompt by opening Run and type cmd and enter. 2) Take your path till the folder structure of your eclipse workspace . Type the command mvn archetype:generate and click enter.


2 Answers

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">
<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>
like image 62
user448153 Avatar answered Nov 16 '22 00:11

user448153


Neither of the two answers worked for me out of the box. After a bit of research, I was able to generate POJOs from a database. Hope this fast tracks someone.

Simply generate the java files - no mapping files generated.

Define your database connection in src/test/resources/reveng/hibernate.cfg.xml. Using the test branch so these files are not copied into the distributable artifact.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="pmSessionFactory">
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <!-- Note that we are pointing directly at the catalog so we can use 
             unqualified table names -->
        <property name="hibernate.connection.url">jdbc:oracle:thin:@server.domain.com:1521:catalog</property>
        <property name="hibernate.connection.password">login</property>
        <property name="hibernate.connection.username">****</property>
        <property name="hibernate.default_schema">PM</property>
    </session-factory>
</hibernate-configuration>

Create a list of tables you want to import. Again in the test branch: src/test/resources/reveng/model.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC 
  "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" 
  "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
  <!-- This assumes your database connection is pointing to the proper catalog -->
  <!-- To get all tables in the named schema, use the following 
       <schema-selection match-schema="PM" />
  -->
  <!--  to get only the named tables -->
  <schema-selection match-schema="PM" match-table="PM_PROPERTY"/>
  <schema-selection match-schema="PM" match-table="PM_APPLICATION"/>
  <schema-selection match-schema="PM" match-table="PM_PROPERTY_TYPE"/>
</hibernate-reverse-engineering>

Add the hibernate3 maven plugin to your pom

<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/com/me/examples/pm/data</outputDirectory>
            <implementation>jdbcconfiguration</implementation>
          </component>
        </components>
        <componentProperties>
          <!-- Storing the reveng files in the test branch means we are not 
               deploying connection information-->
          <revengfile>src/test/resources/reveng/model.reveng.xml</revengfile>
          <configurationfile>src/test/resources/reveng/hibernate.cfg.xml</configurationfile>
          <jdk5>true</jdk5>
          <ejb3>true</ejb3>
        </componentProperties>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>com.oracle</groupId>
          <artifactId>classes12</artifactId>
          <version>10.2.0.1.0</version>
        </dependency>
        <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib-nodep</artifactId>
          <version>2.1_3</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

Run maven

mvn hibernate3:hbm2java
like image 30
Steve Tarver Avatar answered Nov 16 '22 00:11

Steve Tarver