hibernatetemplates

Using custom templates with Hibernate tools reverse engineering from Gradle


Using the Hibernate ant task from with Gradle I'm able to generate entity classes from a database using the documentation at http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html#d0e5102

When I change template and templateprefix, 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.

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 template and templateprefix.

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

And here's the failing build file.

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

And here's the directory tree

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

Here's the error

[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

I've tried adding . and templates to the classpath along with config, which is already in the classpath and working.

I've tried numerous combinations for template and templateprefix, but the only ones that work are the ones in the first build.gradle file above.

UPDATE

I tried with absolute paths for templateprefix and template and got the same file not found error.


Solution

  • Add

    reverseMap files('.')
    

    to your dependencies.