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

Java 使用maven-hibernate3插件生成源代码,java,hibernate,maven-2,maven,hibernate-tools,Java,Hibernate,Maven 2,Maven,Hibernate Tools,除了生成其他源文件外,我还想为DAO类生成一个工厂类——DAOFactory.java。我使用hbmtemplate就是为了这个目的——使用我自己的*.ftl文件。 问题是(正如我正确理解的)为数据库中的每个实体生成文件。是否可以只生成一次该文件 my pom.xml的一部分: <execution> <id>hbmtemplate0</id> <phase>generate-sources</phase> <goal


除了生成其他源文件外,我还想为DAO类生成一个工厂类——DAOFactory.java。我使用hbmtemplate就是为了这个目的——使用我自己的*.ftl文件。 问题是(正如我正确理解的)为数据库中的每个实体生成文件。是否可以只生成一次该文件

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>

hbmtemplate0
生成源
hbmtemplate
hbmtemplate
src/main/java
/src/main/resources/hibernate.reveng.xml
src/main/resources/database.properties
假的
假的
我的.package.name
真的
真的
src/main/resources/reveng.templates/
DAOFactory.java
DAOFactory.java.ftl
a)生成的代码通常不应该进入
src/main/java
使用
target/generated sources/somefoldername
(或者更确切地说:
${project.build.directory}/generated sources/somefoldername
)!否则,生成的代码将最终出现在SCM中,这就是事情变得混乱的时候。根据经验:您编辑的所有内容都在src中,maven创建或编辑的所有内容都在target中

如果hibernate工具没有自动将生成的源目录添加到编译器的源根目录中,您可以通过以下方法执行此操作:


org.codehaus.mojo
构建助手maven插件
1.5
添加源
过程源
添加源
${project.build.directory}/generated sources/somefoldername
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>

maven antrun插件
1.6
过程源
跑

about a)。这是最理想的,但也有一些情况下它不是这样工作的,例如,当我使用Axis2生成Web服务代码,然后手动编辑生成的存根实现时。@Adrian这不应该发生。无论是手工生成代码还是手工编辑代码,都不要混合使用这两种方法。首先,构建变得不可生产。我是反对者,因为从不是绝对的。看,我在一个繁忙的数据库上有1000个表,它花了数小时生成代码,但代码没有改变。@PeterRader只有西斯才处理绝对的问题,没错。更改了措辞。但我仍然不同意你的看法。根据定义,src/main用于编写代码,target/generated sources用于生成代码。如果您的数据库很大且从未更改,那么它应该是一个单独的工件,只有在数据库更改时才会重建,没有理由继续重新编译从未更改的类
<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>