开发者

Simple setup for maven hbm2ddl

I am setting up maven to take annotated java classes and produce some DDL which varies depending on the database. Is there a better way to do this? It seems like I should be able to filter the input to the hbm2ddl plugin (as part of a pipeline) rather than tell it to operate on the output of resource filtering (which I then must filter out of my final jar).

I am filtering my hibernate.cfg.xml file to substitute environment properties based on the local developer's setup:

  <build>
    <filters>
      <filter>${user.home}/datamodel-build.properties</filter>
    </filters>
    <resources><resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource></resources>
  </build>

Then I run hbm2ddl on the output

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>hibernate3-maven-plugin</artifactId>
  ...
 <configuration>
   <componentProperties>
   <configurationfile>target/classes/com/myOrg/datamodel/hibernate.cfg.xml</configurationfile>
</plugin>

I then must fi开发者_如何学编程lter out the hibernate.cfg.xml from my production jar since I don't want to ship anything related to my internal dev environment.


I have this same issue and here is how I solved it. I use have a separate database.properties file that holds the connection details and I don't filter any of my XML files.

This seperate database.properties file gets filtered, but since it is a test resource located in /src/main/test it doesn't get put into the final artifact. I then tell hbm2ddl where to find it as follows:

            <configuration>
                <components>
                    <component>
                        <name>hbm2ddl</name>
                        <implementation>jpaconfiguration</implementation>
                    </component>
                </components>
                <componentProperties>
                    <propertyfile>src/test/resources/database.properties</propertyfile>
                    <!-- Gives the name of the persistence unit as defined in persistence.xml -->
                    <persistenceunit>myapp-core</persistenceunit>
                    <!-- Tells the plugin to send the output to a file -->
                    <outputfilename>create-${database.vendor}-schema.sql</outputfilename>
                    <!-- Pretty Format SQL Code -->
                    <format>true</format>
                    <!-- Do not create tables automatically - other plug-ins will handle that -->
                    <export>false</export>
                    <!-- Do not print the DDL to the console -->
                    <console>false</console>
                </componentProperties>
            </configuration>

Hope it helps anyway....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜