maven hbm2ddl 的简单设置

发布于 2024-09-18 14:45:49 字数 993 浏览 3 评论 0原文

我正在设置 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 因为我不想发布与我的内部开发环境相关的任何内容。

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 filter out the hibernate.cfg.xml from my production jar since I don't want to ship anything related to my internal dev environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2024-09-25 14:45:49

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

这个单独的database.properties 文件被过滤,但由于它是位于/src/main/test 中的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>

希望它能有所帮助......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文