2013-12-11 105 views
0

使用Hibernate的Ant任務從搖籃我能夠在http://docs.jboss.org/tools/latest/en/hibernatetools/html_single/index.html#d0e5102與Hibernate工具使用自定義模板逆向工程從搖籃

成從使用的文檔數據庫實體類當我改變templatetemplateprefix,Hibernate無法找到我的自定義模板。可以肯定的是,我直接從hibernate jar中複製了模板,並向Pojo.ftl添加了一條評論。

這是我工作的帶有Hibernate模板的Gradle構建文件...工作構建文件下面是失敗的,除了templatetemplateprefix之外,它是相同的。

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 

這裏是失敗的構建文件。

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 

而這裏的目錄樹

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 

這裏的錯誤

[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 

我已經嘗試添加.templates到類路徑config以來,這已經是在classpath和工作。

我試過無數的組合爲templatetemplateprefix,但唯一的工作是在上面的第一個文件的build.gradle的那些。

UPDATE

我使用絕對路徑試過templateprefixtemplate,並得到未找到錯誤相同的文件。

回答

1

添加

reverseMap files('.') 

到你的依賴。

+0

爲什麼?這是什麼 ?從hbm.xml文件生成帶註釋的代碼時遇到同樣的錯誤,而且我沒有任何build.xml ... – lmo

0

小晚,但如果仍然有幫助,這是其他方法來指定模板的基本路徑:

更改此:

hibernatetool(destdir : hibernateDestDir) 

到:

hibernatetool(destdir : hibernateDestDir, templatepath : 'templates') 

和模板部分應該是:

templateprefix:"custom_pojo/" , 
template:"custom_pojo/Pojo.ftl",