Question


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>
Was it helpful?

Solution

a) generated code should usually not go in src/main/java!!!! Use target/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:

<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) 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:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top