【问题标题】:Simple setup for maven hbm2ddlmaven hbm2ddl 的简单设置
【发布时间】:2011-04-10 08:44:34
【问题描述】:

我正在设置 maven 来获取带注释的 java 类并生成一些 DDL,这些 DDL 因数据库而异。有一个更好的方法吗?似乎我应该能够过滤 hbm2ddl 插件的输入(作为管道的一部分),而不是告诉它对资源过滤的输出进行操作(然后我必须从我的最终 jar 中过滤出来)。

我正在过滤我的 hibernate.cfg.xml 文件以根据本地开发人员的设置替换环境属性:

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

然后我在输出上运行 hbm2ddl

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

然后我必须从我的生产 jar 中过滤掉 hibernate.cfg.xml,因为我不想发布与我的内部开发环境相关的任何内容。

【问题讨论】:

  • 只是为了确保我理解问题,DDL 的哪些部分确实发生了变化(一个简单的例子就足够了)?
  • 主要是数据库连接/用户名/方言。我想我在 中有一个解决方案,我现在意识到它可以与 hibernate.cfg.xml 分开。 (只是等待一切正常)我仍然更喜欢使用 maven 过滤器,尽管它似乎会使构建矩阵更容易。
  • 我实际上没有得到当前解决方案的问题:S
  • 这似乎步骤太多,我认为您不能同时过滤和排除相同的资源。我正在寻找一种方法来为 hibernate-3 插件(使用 maven 过滤器)提供输入 cfg.xml 文件的过滤视图。我想我只是希望这样的东西会存在,但它似乎并不存在。

标签: maven-2 build hbm2ddl


【解决方案1】:

我也有同样的问题,我是这样解决的。我使用了一个单独的 database.properties 文件来保存连接详细信息,并且我不过滤我的任何 XML 文件。

这个单独的 database.properties 文件会被过滤,但由于它是一个位于/src/main/test测试 资源,它不会被放入最终的工件中。然后我告诉 hbm2ddl 在哪里可以找到它,如下所示:

            <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>

希望对你有所帮助....

【讨论】:

    最近更新 更多