2


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>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Ramps
  • 5,190
  • 1
  • 45
  • 47

1 Answers1

1

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>
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • about a). This is the ideal, however there are occasions when it doesn't work out that way, for example when I use Axis2 to generate webservice code, and then manually edit the generated stub implementation. – Adriaan Koster Jan 18 '11 at 16:12
  • @Adriaan this should not happen. Either generate code or edit code by hand, don't mix those two approaches. For one thing, the builds become unreproducible. – Sean Patrick Floyd Jan 18 '11 at 16:31
  • I am the downvoter, because **never** is absolute. See, i have 1000 tables on a buzzy database, it tooks hours to generate code that did not change. – Grim Nov 29 '15 at 07:46
  • @PeterRader only a Sith deals in absolutes, true. Changed the wording. But I still disagree with you. By definition, src/main is for written code, target/generated-sources for generated code. If your db is huge and never changes, then it should be a separate artifact that is only rebuilt when the db changes, no reason to keep recompiling classes that never change – Sean Patrick Floyd Nov 29 '15 at 15:28