title: Using custom templates with Hibernate tools reverse engineering from Gradle
body: <p>Using the Hibernate ant task from with Gradle I'm able to generate entity classes from a database using the documentation at <a href="http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html#d0e5102" rel="nofollow">http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html#d0e5102</a></p>
  
  <p>When I change <code>template</code> and <code>templateprefix</code>, Hibernate cannot find my custom templates.  To be sure, I copied the templates directly from the hibernate jars and added a single comment to the Pojo.ftl.</p>
  
  <p>Here's my working Gradle build file with the Hibernate templates... below the working build file is the failing one, which is identical except for the <code>template</code> and <code>templateprefix</code>.</p>
  
  <pre><code>apply plugin: 'java'
  
  repositories {
      mavenCentral()
  }
  
  configurations {
      reverseMap
  }
  
  dependencies {
      reverseMap 'org.hibernate:hibernate-tools:4.0.0-CR1'
      reverseMap 'org.slf4j:slf4j-simple:1.7.5'
      reverseMap files('/Users/jzwolak/local/lib/ojdbc6-11.2.0.2.0.jar')
      compile 'javax:javaee-api:7.0'
  }
  
  project.ext {
      hibernateRevEngXml = "$projectDir/config/hibernate.reveng.xml"
      hibernateDestDir = file("$buildDir/generated")
  }
  
  task reverseMap {
      inputs.files hibernateRevEngXml
      outputs.dir hibernateDestDir
      doLast {
          hibernateDestDir.exists() || hibernateDestDir.mkdirs()
          ant {
              taskdef(name: 'hibernatetool',
                      classname: 'org.hibernate.tool.ant.HibernateToolTask',
                      classpath: configurations.reverseMap.asPath )
              hibernatetool( destdir : hibernateDestDir ) {
                  jdbcconfiguration(
                          configurationfile:"$projectDir/config/hibernate.cfg.xml",
                          revengfile:hibernateRevEngXml,
                          packagename:
                                  "com.mybiz"
                          //reversestrategy="ReverseEngineeringStrategy classname"
                          //detectmanytomany="true|false"
                          //detectoptmisticlock="true|false"
                  )
                  hbmtemplate(
                      templateprefix:"pojo/" ,
                      template:"pojo/Pojo.ftl", 
                      filepattern:"{package-name}/{class-name}.java"
                  ) {
                      property(key:"jdk5",value:"true")
                      property(key:"ejb3",value:"true")
                  }
                  /*
                  hbm2java(
                          jdk5: true,
                          ejb3: true
                  )
                  */
                  // Adds the config directory to the path so that log4j can pick up
                  // its properties file.
                  classpath {
                      pathelement( path: "config" )
                  }
              }
          }
      }
  }
  
  compileJava.source reverseMap.outputs.files, sourceSets.main.java
  </code></pre>
  
  <p>And here's the failing build file.</p>
  
  <pre><code>apply plugin: 'java'
  
  repositories {
      mavenCentral()
  }
  
  configurations {
      reverseMap
  }
  
  dependencies {
      reverseMap 'org.hibernate:hibernate-tools:4.0.0-CR1'
      reverseMap 'org.slf4j:slf4j-simple:1.7.5'
      reverseMap files('/Users/jzwolak/local/lib/ojdbc6-11.2.0.2.0.jar')
      compile 'javax:javaee-api:7.0'
  }
  
  project.ext {
      hibernateRevEngXml = "$projectDir/config/hibernate.reveng.xml"
      hibernateDestDir = file("$buildDir/generated")
  }
  
  task reverseMap {
      inputs.files hibernateRevEngXml
      outputs.dir hibernateDestDir
      doLast {
          hibernateDestDir.exists() || hibernateDestDir.mkdirs()
          ant {
              taskdef(name: 'hibernatetool',
                      classname: 'org.hibernate.tool.ant.HibernateToolTask',
                      classpath: configurations.reverseMap.asPath )
              hibernatetool( destdir : hibernateDestDir ) {
                  jdbcconfiguration(
                          configurationfile:"$projectDir/config/hibernate.cfg.xml",
                          revengfile:hibernateRevEngXml,
                          packagename:
                                  "com.mybiz"
                          //reversestrategy="ReverseEngineeringStrategy classname"
                          //detectmanytomany="true|false"
                          //detectoptmisticlock="true|false"
                  )
                  hbmtemplate(
                      templateprefix:"templates/custom_pojo/" ,
                      template:"templates/custom_pojo/Pojo.ftl", 
                      filepattern:"{package-name}/{class-name}.java"
                  ) {
                      property(key:"jdk5",value:"true")
                      property(key:"ejb3",value:"true")
                  }
                  /*
                  hbm2java(
                          jdk5: true,
                          ejb3: true
                  )
                  */
                  // Adds the config directory to the path so that log4j can pick up
                  // its properties file.
                  classpath {
                      pathelement( path: "config" )
                  }
              }
          }
      }
  }
  
  compileJava.source reverseMap.outputs.files, sourceSets.main.java
  </code></pre>
  
  <p>And here's the directory tree</p>
  
  <pre><code>build.gradle
  config
  |    hibernate.cfg.xml
  |    hibernate.reveng.xml
  |    log4j.properties
  templates
  |    custom_pojo
  |    |    Ejb3PropertyGetAnnotation.ftl
  |    |    Ejb3TypeDeclaration.ftl
  |    |    GetPropertyAnnotation.ftl
  |    |    Pojo.ftl
  |    |    PojoConstructors.ftl
  |    |    PojoEqualsHashcode.ftl
  |    |    PojoExtraClassCode.ftl
  |    |    PojoFields.ftl
  |    |    PojoInterfacePropertyAccessors.ftl
  |    |    PojoPropertyAccessors.ftl
  |    |    PojoToString.ftl
  |    |    PojoTypeDeclaration.ftl
  |    |    test.ftl
  </code></pre>
  
  <p>Here's the error</p>
  
  <pre><code>[ant:hibernatetool] org.hibernate.tool.hbm2x.ExporterException: Error while processing Entity: com.mybiz.TsModelRealizationView with template templates/custom_pojo/Pojo.ftl
  [ant:hibernatetool] java.io.FileNotFoundException: Template templates/custom_pojo/Pojo.ftl not found.
  :reverseMap FAILED
  </code></pre>
  
  <p>I've tried adding <code>.</code> and <code>templates</code> to the classpath along with <code>config</code>, which is already in the classpath and working.</p>
  
  <p>I've tried numerous combinations for <code>template</code> and <code>templateprefix</code>, but the only ones that work are the ones in the first build.gradle file above.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I tried with absolute paths for <code>templateprefix</code> and <code>template</code> and got the same file not found error.</p>