除了生成其他源文件外,我还想生成一个用于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>
有帮助吗?

解决方案

一个) 生成的代码通常不应进入 src/main/java!!!! 利用 target/generated-sources/somefoldername (更确切地说: ${project.build.directory}/generated-sources/somefoldername) 反而!否则,您生成的代码最终会进入您的SCM,那就是事情变得混乱。根据经验: 您编辑的所有内容都在SRC中,Maven创建或编辑都在目标中.

如果Hibernate工具不会自动将生成的源DIR添加到编译器的源根中,则可以使用 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>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top