Hibernate 的 Maven Java 源代码生成
我正忙于将现有项目从 Ant 构建转换为使用 Maven 构建的项目。此构建的一部分包括使用 hibernate hbm2java 工具将 .hbm.xml 文件集合转换为 Java。以下是用于执行此操作的 Ant 脚本的片段:
<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 代码生成构建阶段进行代码生成吗?
谢谢!
亚当.
I´m busy converting an existing project from an Ant build to one using Maven. Part of this build includes using the hibernate hbm2java tool to convert a collection of .hbm.xml files into Java. Here's a snippet of the Ant script used to do this:
<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>
I've had a look around on the internet and some people seem to do this (I think) using Ant within Maven and others with the Maven plugin. I'd prefer to avoid mixing Ant and Maven. Can anyone suggest a way to do this so that all of the .hbm.xml files are picked up and the code generation takes place as part of the Maven code generation build phase?
Thanks!
Adam.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,如果您不这样做,可以使用 Maven Hibernate3 插件不想混合 Ant 和 Maven(我认为这是一个好主意)。它有一个
hbm2java
目标默认绑定到generate-sources
阶段。请参阅 Mojo 网站了解更多详细信息,但插件的设置可能如下所示:编辑: 该插件实际上在
.hbm.xml
中查找.hbm.xml
code>target/classes 生成java源文件。因此,如果您将映射文件放入src/main/resources
中,它们将在process-resources
阶段复制到target/classes
中这是由插件调用的,一切都会正常工作。我刚刚使用以下示例项目对此进行了测试:pom.xml
几乎是空的,它只包含上面看到的插件配置和 junit 依赖项。hibernate.cfg.xml
包含:Person.hbm.xml
如下所示:使用此配置,运行
mvn install
会生成Person.java
如下所示:一切都按描述进行。
Well, there is the Maven Hibernate3 Plugin if you don't want to mix Ant and Maven (which is a good idea here IMO). It has a
hbm2java
goal which is bound by default to thegenerate-sources
phase. Refer to the website of the Mojo for more details but the setup of the plugin might looks like something like this:EDIT: The plugin actually looks for
.hbm.xml
intarget/classes
to generate the java source files. So, if you put your mapping files insrc/main/resources
, they will get copied intotarget/classes
during theprocess-resources
phase which is invoked by the plugin and things will just work. I've just tested this with the following sample project:The
pom.xml
is almost empty, it just contains the plugin configuration seen above and a junit dependency. Thehibernate.cfg.xml
contains:And
Person.hbm.xml
looks as follow:With this configuration, running
mvn install
generatesPerson.java
as shown below:Everything works as described.
帕斯卡,再次感谢您的帮助!您的解决方案效果很好。
我在从事这项工作时遇到了其他一些事情。第一个涉及到这是一个相当大的项目,因此我将其拆分为多个 Maven 模块以镜像原始的 ant 多目录构建。包含生成的类的模块实际上并不执行任何数据库访问,因此 hibernate.cfg.xml 文件不需要(在这种情况下也不应该)包含任何数据库连接信息。我已经尝试过了,它与 Pascal 提供的文件的缩减版本一起工作得很好,删除了所有属性标签。
完成此操作后,从命令行构建就可以正常工作了。然而,尽我所能,我无法说服其他模块在从 Eclipse 运行时选择生成的类。目前,我对此的解决方案是将以下行添加到配置/组件/组件下的 POM 中:
这会强制在 Eclipse 可以为其他模块拾取它们的位置生成文件。完成此操作后,您必须在命令行上进行构建,然后请求 Eclipse 刷新源目录的内容以获取新文件。到目前为止,我不知道有更干净的方法来处理这个问题......
Pascal, thanks again for your help! Your solution works well.
A couple of other things I encountered while working on this. The first relates to the fact that this is a fairly big project and so I've split it into multiple Maven modules to mirror the original ant multi-directory build. The module that contains the generated classes doesn't actually do any database access and so the hibernate.cfg.xml file need not, and in this case should not, contain any DB connection information. I've tried this out and it works just fine with a cut down version of the file provided by Pascal, with all of the properties tags removed.
With this in place, the build worked fine from the command line. However, try as I might, I couldn't persuade the other modules to pick up the generated classes when run from Eclipse. For the time being, the solution I have to this is to add the following line into the POM under configuration/components/component:
This forces the files to be generated in a place that eclipse can pick them up for the other modules. Once this is done you must do a build on the command line and then request that Eclipse refresh the contents of the source directory to pick up the new files. As yet, I don't know of a cleaner way to handle this....
如果需要在编译阶段包含 *.hbm.xml;编辑 pom.xml 并添加以下代码:
If you need include *.hbm.xml on the phase compile; edit your pom.xml and add the next code: