【问题标题】:Maven Java Source Code Generation for Hibernate用于 Hibernate 的 Maven Java 源代码生成
【发布时间】:2010-12-26 09:26:41
【问题描述】:

我正忙着将现有项目从 Ant 构建转换为使用 Maven 构建的项目。此构建的一部分包括使用 hibernate hbm2java 工具将 .hbm.xml 文件集合转换为 Java。这是用于执行此操作的 Ant 脚本的 sn-p:

<target name="dbcodegen" depends="cleangen" 
        description="Generate Java source from Hibernate XML">
  <hibernatetool destdir="${src.generated}">
    <configuration>   
      <fileset dir="${src.config}">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </configuration>   
    <hbm2java jdk5="true"/>
  </hibernatetool>   
</target>

我在互联网上浏览了一下,有些人似乎(我认为)在 Maven 中使用 Ant,而其他人则使用 Maven 插件。我宁愿避免混合 Ant 和 Maven。谁能建议一种方法来执行此操作,以便拾取所有 .hbm.xml 文件并在 Maven 代码生成构建阶段进行代码生成?

谢谢!

亚当。

【问题讨论】:

  • 新的 maven3 约定似乎是 ${project.build.directory}/generated/hibernate3/main/java 而不是 ${project.build.directory}/generated/hibernate3 我有一个很难找到支持这一点的文档。新的 m2Eclipse 插件似乎使用了这个新约定。这在使用“更新项目配置”功能时尤其明显。

标签: hibernate maven-2 orm ant hbm2java


【解决方案1】:

好吧,如果您不想混合使用 Ant 和 Maven,可以使用 Maven Hibernate3 Plugin(这在 IMO 中是个好主意)。它有一个hbm2java 目标,默认绑定到generate-sources 阶段。有关详细信息,请参阅 Mojo 的网站,但插件的设置可能如下所示:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>
    <executions>
      <execution>
        <phase>generate-sources</phase>
        <goals>
          <goal>hbm2java</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <components>
        <component>
          <name>hbm2java</name>
          <implementation>configuration</implementation>
          <outputDirectory>target/generated-sources/hibernate3</outputDirectory>
        </component>
      </components>
      <componentProperties>
        <drop>true</drop>
        <jdk5>true</jdk5>
        <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
      </componentProperties>
    </configuration>
  </plugin> 

编辑:插件实际上在target/classes 中寻找.hbm.xml 来生成java 源文件。因此,如果您将映射文件放在src/main/resources 中,它们将在插件调用的process-resources 阶段被复制到target/classes 中,并且一切正常。我刚刚使用以下示例项目对此进行了测试:

maven-hibernate3-testcase |-- pom.xml `--src |-- 主要 | |-- 爪哇 | `-- 资源 | |-- 人物.hbm.xml | `--休眠.cfg.xml `-- 测试 `--java

pom.xml 几乎是空的,它只包含上面看到的插件配置和一个 junit 依赖项。 hibernate.cfg.xml 包含:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property>
    <property name="connection.username">app</property>
    <property name="connection.password">app</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.DerbyDialect</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>

    <!-- Mapping files -->
    <mapping resource="Person.hbm.xml" />
  </session-factory>
</hibernate-configuration>

Person.hbm.xml 如下所示:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
   PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

  <class name="Person" table="person">
    <id name="id" type="int">
      <generator class="increment" />
    </id>

    <property name="name" column="cname" type="string" />
  </class>

</hibernate-mapping>

使用此配置,运行mvn install 会生成Person.java,如下所示:

$ cat target/generated-sources/hibernate3/Person.java 
// default package
// Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GA



/**
 * Person generated by hbm2java
 */
public class Person  implements java.io.Serializable {


     private int id;
     private String name;

    public Person() {
    }

    public Person(String name) {
       this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }




}

一切正常。

【讨论】:

  • 谢谢帕斯卡!我认为这是一个好的开始,因为它清楚地提供了“全 Maven”解决方案。我能看到的唯一剩下的问题是它引用了一个配置文件(“hibernate.cfg.xml”)。我有一组配置文件,在 Ant 脚本中使用“*/.hbm.xml”模式引用。有谁知道如何使用 Maven 和 Pascal 建议的插件来做到这一点?
  • 正是我想要的。谢谢+1。
  • 感谢 +1,节省时间的解决方案
【解决方案2】:

帕斯卡,再次感谢您的帮助!您的解决方案效果很好。

我在处理此问题时遇到的其他一些事情。第一个与这是一个相当大的项目有关,因此我将其拆分为多个 Maven 模块以反映原始 ant 多目录构建。包含生成的类的模块实际上并不进行任何数据库访问,因此 hibernate.cfg.xml 文件不需要,在这种情况下也不应该包含任何数据库连接信息。我已经尝试过了,它适用于 Pascal 提供的文件的缩减版本,所有属性标签都已删除。

有了这个,从命令行构建工作正常。但是,尽我所能,我无法说服其他模块在从 Eclipse 运行时选择生成的类。目前,我的解决方案是在配置/组件/组件下的POM中添加以下行:

<outputDirectory>/src/main/java</outputDirectory>

这会强制在 eclipse 可以为其他模块提取它们的地方生成文件。完成此操作后,您必须在命令行上进行构建,然后请求 Eclipse 刷新源目录的内容以获取新文件。到目前为止,我不知道有一种更清洁的方法来处理这个......

【讨论】:

  • 很高兴它有帮助。不过,我有几点意见。首先,在src/main/java 中生成代码不是最佳实践,生成的代码应该在target 中生成。这背后的主要原因是我们希望clean 清理它们。然后,关于 Eclipse,包含生成代码的目录确实必须添加为“源文件夹”。如果您使用的是 m2eclipse 插件,如果您右键单击项目然后Maven > 更新项目配置(参见docs.codehaus.org/display/M2ECLIPSE/…),这将自动完成。
  • 如果您使用 maven-eclipse-plugin,调用 mvn eclipse:eclipse 将生成一个 .classpath,其中包含一个 classpathentrytarget/hibernate3/generated-sources 开箱即用。所以,在这两种情况下,都不需要在src/main/java 下生成源(这是一件好事)。最后,关于我的 hibernate.cfg.xml 的内容,当然只是一个例子 :) 祝你迁移过程顺利!
  • 再次感谢!我确实使用 m2eclipse 作为插件,并且更新项目配置成功了并解决了问题。另外,我同意你的观点,即把生成的代码放在一个可以用类清理的地方。
【解决方案3】:

如果您需要在阶段编译时包含 *.hbm.xml;编辑你的 pom.xml 并添加下一个代码:

<build>
                <resources>
            <resource>
                <directory>source/com/qfund/orm/</directory>
                <targetPath>com/qfund/orm/</targetPath>
                <includes>
                    <include>*.hbm.xml</include>
                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/java/</directory>
                <includes>
                    <include>*.xml</include>
                    <include>*.xsd</include>
                    <include>*.xslt</include>
                    <include>*.properties</include>
                </includes>
            </testResource>
        </testResources>
</build>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2014-11-19
    • 2012-05-30
    • 2013-05-08
    • 1970-01-01
    • 2011-02-25
    相关资源
    最近更新 更多