使用 maven-hibernate3 插件生成源
除了生成其他源文件之外,我还想为 DAO 类生成一个工厂类 - DAOFactory.java。我正在使用 hbmtemplate 来实现此目的 - 以及我自己的 *.ftl 文件。 问题是(据我正确理解)为数据库中的每个实体生成文件。是否可以只生成该文件一次?
我的 pom.xml 的一部分:
<execution>
<id>hbmtemplate0</id>
<phase>generate-sources</phase>
<goals>
<goal>hbmtemplate</goal>
</goals>
<configuration>
<components>
<component>
<name>hbmtemplate</name>
<outputDirectory>src/main/java</outputDirectory>
</component>
</components>
<componentProperties>
<revengfile>/src/main/resources/hibernate.reveng.xml</revengfile>
<propertyfile>src/main/resources/database.properties</propertyfile>
<jdk5>false</jdk5>
<ejb3>false</ejb3>
<packagename>my.package.name</packagename>
<format>true</format>
<haltonerror>true</haltonerror>
<templatepath>src/main/resources/reveng.templates/</templatepath>
<filepattern>DAOFactory.java</filepattern>
<template>DAOFactory.java.ftl</template>
</componentProperties>
</configuration>
</execution>
Besides generating other source files, I want to generate one factory class for DAO classes - DAOFactory.java. I'm using hbmtemplate for that purpose - with my own *.ftl file.
Problem is that (as I understand correctly) file is generated for each entity in database. Is it possible to generate that file only once?
Part of my pom.xml:
<execution>
<id>hbmtemplate0</id>
<phase>generate-sources</phase>
<goals>
<goal>hbmtemplate</goal>
</goals>
<configuration>
<components>
<component>
<name>hbmtemplate</name>
<outputDirectory>src/main/java</outputDirectory>
</component>
</components>
<componentProperties>
<revengfile>/src/main/resources/hibernate.reveng.xml</revengfile>
<propertyfile>src/main/resources/database.properties</propertyfile>
<jdk5>false</jdk5>
<ejb3>false</ejb3>
<packagename>my.package.name</packagename>
<format>true</format>
<haltonerror>true</haltonerror>
<templatepath>src/main/resources/reveng.templates/</templatepath>
<filepattern>DAOFactory.java</filepattern>
<template>DAOFactory.java.ftl</template>
</componentProperties>
</configuration>
</execution>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
a) 生成的代码通常不应放入
src/main/java
!!!! 使用target/ generated-sources/somefoldername
(或者更确切地说:${project.build.directory}/ generated-sources/somefoldername
) 代替!否则,您生成的代码最终将出现在您的 SCM 中,这就是事情变得混乱的时候。根据经验:您编辑的所有内容都在 src 中,maven 创建或编辑的所有内容都在目标中。如果 hibernate 工具不会自动将生成的源目录添加到编译器的源根目录中,您可以使用
buildhelper-maven-plugin
:b)看来您不能将其限制为单个类。因此,您可以做的一件事就是删除生成的不需要的 java 文件。执行此类操作的标准方法是使用 antrun 插件:
a) generated code should usually not go in
src/main/java
!!!! Usetarget/generated-sources/somefoldername
(or rather:${project.build.directory}/generated-sources/somefoldername
) instead! Otherwise your generated code will end up in your SCM and that's when things get messy. As a rule of thumb: everything you edit is in src, everything maven creates or edits is in target.If the hibernate tools don't automatically add the generated source dir to the compiler's source roots, you can do that with the
buildhelper-maven-plugin
:b) It appears that you can not restrict it to a single class. So one thing you could do is to delete the generated java files you don't need. The standard way to do things like that is to use the antrun plugin: