Bluemoon10
Bluemoon10

Reputation: 89

How to configure different components in maven for hibernate

I'm currently reverse engineering table from a DB, I wish to produce xml mappings, java pojo classes and java dao classes. I would like to store the resulting classes in different package folders eg app.myapp.dao, app.myapp.pojo and app.myapp.dao. I would like to configure my pom to have separate 'component' configurations, so for example for hbm2dao, look for a specific hibernate.reveng.xml file which would contain info and tell the class generation to add package app.myapp.dao to the top of the class. My current set up is:

<configuration>
    <components>
        <component>
            <name>hbm2java</name>
            <outputDirectory>src/main/java</outputDirectory>
            <implementation>jdbcconfiguration</implementation>
      </component>
          <component>
              <name>hbm2dao</name>
              <outputDirectory>src/main/java</outputDirectory>
              <implementation>jdbcconfiguration</implementation>
          </component>
        </components>
   <componentProperties
        <revengfile>src/main/resources/hibernate.reveng.xml</revengfile
 <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
    <jdk5>true</jdk5>
    <ejb3>false</ejb3>
   </componentProperties>
</configuration>

My current set up can only use one version of hibernate.reveng.xml for all 3 types of generation(hbm2dao, hbm2java, hbm2hbxml) So my question is how do i set up my pom to use 3 separate hibernate.reveng.xml files, so i can configure the package info and put the generated classes in different locations? is it possible? Thanks

Upvotes: 0

Views: 242

Answers (1)

karmarok
karmarok

Reputation: 51

Instead of having a shared configuration element in plugin/configuration you have to move it to every separate execution in plugin/executions/execution/configuration.

So for example if you have your plugin currently configured like this:

<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>
            <component>
                <name>hbm2dao</name>
                <outputDirectory>src/main/java</outputDirectory>
                <implementation>jdbcconfiguration</implementation>
            </component>
        </components>
        <componentProperties
            <revengfile>src/main/resources/hibernate.reveng.xml</revengfile>
            <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
            <jdk5>true</jdk5>
            <ejb3>false</ejb3>
        </componentProperties>
    </configuration>
    <executions>
        <execution>
            <id>hbm2java-execution</id>
            <phase>compile</phase>
            <goals>
                <goal>hbm2java</goal>
            </goals>
        </execution>
        <execution>
            <id>hbm2dao-execution</id>
            <phase>compile</phase>
            <goals>
                <goal>hbm2dao</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        ...
    </dependencies>
</plugin>

Change it to:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>hbm2java-execution</id>
            <phase>compile</phase>
            <goals>
                <goal>hbm2java</goal>
            </goals>
            <configuration>
                <!-- hbm2java component only -->
                <components>
                    <component>
                        <name>hbm2java</name>
                        <outputDirectory>src/main/java</outputDirectory>
                        <implementation>jdbcconfiguration</implementation>
                    </component>
                </components>
                <!-- hbm2java component properties -->
                <componentProperties
                    <revengfile>src/main/resources/hibernate.reveng.xml</revengfile>
                    <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
                    <packagename>app.myapp.pojo</packagename>
                    <jdk5>true</jdk5>
                    <ejb3>false</ejb3>
                </componentProperties>
            </configuration>
        </execution>
        <execution>
            <id>hbm2dao-execution</id>
            <phase>compile</phase>
            <goals>
                <goal>hbm2dao</goal>
            </goals>
            <configuration>
                <!-- hbm2dao component only -->
                <components>
                    <component>
                        <name>hbm2dao</name>
                        <outputDirectory>src/main/java</outputDirectory>
                        <implementation>jdbcconfiguration</implementation>
                    </component>
                </components>
                <!-- hbm2dao component properties -->
                <componentProperties
                    <revengfile>src/main/resources/hibernate.reveng.xml</revengfile>
                    <configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
                    <packagename>app.myapp.dao</packagename>
                    <jdk5>true</jdk5>
                    <ejb3>false</ejb3>
                </componentProperties>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        ...
    </dependencies>
</plugin>

See how now you can specify different packagename property values for hbm2java and hbm2dao.

Upvotes: 1

Related Questions