使用maven-hibernate3插件生成源代码

时间:2011-01-18 14:46:19

标签: java hibernate maven-2 maven hibernate-tools


除了生成其他源文件,我想为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>

1 个答案:

答案 0 :(得分:1)

a)生成的代码通常不应该进入src/main/java !!!! 使用target/generated-sources/somefoldername(或更确切地说:${project.build.directory}/generated-sources/somefoldername)代替!否则,您生成的代码将最终出现在您的SCM中,并且当事情变得混乱时。根据经验:您编辑的所有内容都在src中,maven创建或编辑的所有内容都在目标中

如果hibernate工具没有自动将生成的源目录添加到编译器的源根目录,则可以使用buildhelper-maven-plugin

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
         <execution>
            <id>add-source</id>
            <phase>process-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>
${project.build.directory}/generated-sources/somefoldername
                    </source>
              </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

b)您似乎无法将其限制为单个班级。所以你可以做的一件事是删除你不需要的生成的java文件。执行此类操作的标准方法是使用antrun插件:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <phase>process-sources</phase>
      <configuration>
        <target>
          <delete>
            <fileset
              dir="${project.build.directory}/generated-sources/somefoldername"
              includes="**/*.java" excludes="**/ClassYouWantToKeep.java" />
          </delete>
        </target>
      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
    </execution>
  </executions>
</plugin>