2010-05-30 62 views
3

我正試圖將我的ant構建遷移到maven2。在我的build.xml我調用調用:hbm2java以下列方式:如何配置hbm2java maven2插件爲所有映射文件生成POJO

<hibernatetool destdir="/src/generated/"> 
     <configuration configurationfile="${env.ITP_HOME}/core/xml/hibernate/hibernate.cfg.xml"> 
      <fileset dir="/xml/hibernate"> 
       <include name="*.hbm.xml"/> 
      </fileset> 
     </configuration> 
     <hbm2java/> 
    </hibernatetool> 

我的hibernate.cfg.xml是:

<hibernate-configuration> 
<session-factory> 
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> 
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>   
</session-factory>  

在我maven2的POM文件我有:

<plugin> 
<groupId>org.codehaus.mojo</groupId> 
<artifactId>hibernate3-maven-plugin</artifactId> 
<version>2.2</version> 
<executions> 
<execution> 
    <id>hbm2java</id> 
    <phase>generate-sources</phase> 
    <goals> 
    <goal>hbm2java</goal> 
    </goals> 
    <configuration> 
    <components> 
    <component> 
    <name>hbm2java</name> 
    <implementation>configuration</implementation> 
    <outputDirectory>/src/main/java</outputDirectory> 
    </component> 
    </components> 
    <componentProperties> 
    <jdk5>true</jdk5> 
    <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile> 
    </componentProperties> 
    </configuration>  
</execution> 

但是當執行mvn hibernate3:hbm2java我看到沒有文件得到生成,除非它們都列在hiberna te.cfg.xml。 有沒有辦法在類似於ant任務的maven配置中指定一個文件集?

感謝, NAOR

+0

有沒有人找到解決方案?我有同樣的問題。我沒有運行數據庫,不能使用hbm2cfgxml。 – 2012-03-31 21:15:58

回答

3

我不知道這是唯一的方式,但我會用hbm2cfgxml第一生成包括<mapping resource="..."/>條目,然後hbm2java目標生成的POJO一個hibernate.cfg.xml配置文件。下面,配置這樣作爲構建的一部分:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>hibernate3-maven-plugin</artifactId> 
    <version>2.2</version> 
    <executions> 
    <execution> 
     <id>generate-xml-files</id> 
     <phase>generate-resources</phase> 
     <goals> 
     <goal>hbm2cfgxml</goal> 
     </goals> 
    </execution> 
    <execution> 
     <id>generate-entities</id> 
     <phase>generate-sources</phase> 
     <goals> 
     <goal>hbm2java</goal> 
     </goals> 
    </execution> 
    </executions> 
    <configuration> 
    <components> 
     <component> 
     <name>hbm2cfgxml</name> 
     <implementation>jdbcconfiguration</implementation> 
     <outputDirectory>target/classes</outputDirectory> 
     </component> 
     <component> 
     <name>hbm2java</name> 
     <implementation>configuration</implementation> 
     <outputDirectory>target/generated-sources/hibernate3</outputDirectory> 
     </component> 
    </components> 
    <componentProperties> 
     <propertyfile>src/main/resources/database.properties</propertyfile> 
     <jdk5>true</jdk5> 
     <ejb3>false</ejb3> 
     <packagename>com.mycompany.myapp</packagename> 
     <format>true</format> 
     <haltonerror>true</haltonerror> 
    </componentProperties> 
    </configuration> 
    <dependencies> 
    <!-- your JDBC driver --> 
    ... 
    </dependencies> 
</plugin> 

src/main/database.properties文件包含以下信息

hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver 
hibernate.connection.url=... 
hibernate.connection.username=... 
hibernate.connection.password=... 
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect 

此設置假設您.hbm.xml被放置在src/main/resources(並因此被複制到target/classes用於處理hbm2java)。

+0

感謝帕斯卡爾的回答。 我忘了提及我的ant build也會生成我的ddl腳本來構建數據庫作爲構建(不同目標)的一部分。我希望在使用hbm2ddl的maven2構建中做類似的事情。 您的解決方案是否要求我已經創建了我的數據庫來生成cfg.xml?如果是這樣,有沒有辦法實現這一點,而不指向現有的模式? – naor 2010-05-30 20:17:16

+0

@naor我不確定它(數據庫連接信息將用於創建hibernate.cfg.xml),我現在無法測試它。但是也許你可以嘗試一下。 – 2010-05-30 20:38:43

+0

謝謝帕斯卡。 我做了一個本地測試。不幸的是,我確實看到生成的hibernate.cfg.xml列出了基於我指向的模式的映射。 到目前爲止,我找不到任何指示maven插件可以支持指向映射文件所在的目錄。 我下一步將手動列出映射文件:-( – naor 2010-06-01 00:22:35