将自定义模板与Hibernate工具一起使用可对Gradle进行反向工程 [英] Using custom templates with Hibernate tools reverse engineering from Gradle

查看:54
本文介绍了将自定义模板与Hibernate工具一起使用可对Gradle进行反向工程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Gradle中的Hibernate ant任务,我可以使用

这是失败的构建文件.

 应用插件:"java"储存库{mavenCentral()}配置{reverseMap}依赖项{reverseMap'org.hibernate:hibernate-tools:4.0.0-CR1'reverseMap'org.slf4j:slf4j-simple:1.7.5'reverseMap文件('/Users/jzwolak/local/lib/ojdbc6-11.2.0.2.0.jar')编译'javax:javaee-api:7.0'}project.ext {hibernateRevEngXml ="$ projectDir/config/hibernate.reveng.xml"hibernateDestDir = file("$ buildDir/Generated")}任务reverseMap {inputs.files hibernateRevEngXmloutput.dir hibernateDestDirdoLast {hibernateDestDir.exists()||hibernateDestDir.mkdirs()蚂蚁{taskdef(name:'hibernatetool',类别名称:"org.hibernate.tool.ant.HibernateToolTask​​",classpath:configuration.reverseMap.asPath)hibernatetool(destdir:hibernateDestDir){jdbcconfiguration(配置文件:"$ projectDir/config/hibernate.cfg.xml",revengfile:hibernateRevEngXml,包裹名字:"com.mybiz"//reversestrategy ="ReverseEngineeringStrategy classname"//detectmanytomany ="true | false"//detectoptmisticlock ="true | false")hbmtemplate(templateprefix:"templates/custom_pojo/",模板:模板/custom_pojo/Pojo.ftl",filepattern:"{package-name}/{class-name} .java"){属性(键:"jdk5",值:"true")属性(键:"ejb3",值:"true")}/*hbm2java(jdk5:是的,ejb3:是)*///将config目录添加到路径中,以便log4j可以选择//其属性文件.类路径{pathelement(path:"config")}}}}}compileJava.source reverseMap.outputs.files,sourceSets.main.java 

这是目录树

  build.gradle配置|hibernate.cfg.xml|hibernate.reveng.xml|log4j.properties范本|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 

这是错误

  [ant:hibernatetool] org.hibernate.tool.hbm2x.ExporterException:处理实体时出错:带有模板template/custom_pojo/Pojo.ftl的com.mybiz.TsModelRealizationView[ant:hibernatetool] java.io.FileNotFoundException:找不到模板模板/custom_pojo/Pojo.ftl.:reverseMap失败 

我尝试将. templates 以及 config 一起添加到类路径中,该配置已在类路径中并且可以正常工作.

我为 template templateprefix 尝试了多种组合,但是唯一有效的组合是上面第一个build.gradle文件中的组合.

更新

我尝试使用 templateprefix template 的绝对路径,并得到未找到相同文件的错误.

添加

  reverseMap文件('.') 

依赖于您.

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.

解决方案

Add

reverseMap files('.')

to your dependencies.

这篇关于将自定义模板与Hibernate工具一起使用可对Gradle进行反向工程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆