2009-12-14 62 views
6

我正忙於將現有項目從Ant構建轉換爲使用Maven的項目。該構建的一部分包括使用hibernate hbm2java工具將一組.hbm.xml文件轉換爲Java。這裏是用來做這個Ant腳本的一個片段:用於休眠的Maven Java源代碼生成

<target name="dbcodegen" depends="cleangen" 
     description="Generate Java source from Hibernate XML"> 
    <hibernatetool destdir="${src.generated}"> 
    <configuration> 
     <fileset dir="${src.config}"> 
     <include name="**/*.hbm.xml"/> 
     </fileset> 
    </configuration> 
    <hbm2java jdk5="true"/> 
    </hibernatetool> 
</target> 

我已經在互聯網上四處看了一下,有些人似乎做到這一點(我認爲),使用中的Maven Ant和其他與Maven插件。我寧願避免混用Ant和Maven。任何人都可以提出一種方法來做到這一點,以便所有的.hbm.xml文件都被挑選出來,代碼生成將作爲Maven代碼生成構建階段的一部分進行。

謝謝!

亞當。

+0

新maven3慣例似乎是 $ {} project.build.directory /生成/ Hibernate3中/主/ JAVA 而不是 $ { project.build.directory}/generated/hibernate3 我很難找到文檔來支持這個。新的m2Eclipse插件似乎使用這個新的約定。這在使用「更新項目配置」功能時尤其明顯。 – 2010-06-04 19:37:18

回答

13

那麼,如果你不想混合Ant和Maven(這裏是一個好主意),那麼就有Maven Hibernate3 Plugin。它有一個hbm2java目標,默認綁定到generate-sources階段。請參閱魔的網站,瞭解更多的細節,但該插件威力的設置看起來是這樣的:

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

編輯:插件實際上看起來在target/classes.hbm.xml生成Java源文件。所以,如果你把你的映射文件放在src/main/resources中,它們將會在插件調用的process-resources階段被複制到target/classes中,而且事情將會正常工作。我剛剛與下面的示例項目測試了這個:

 
maven-hibernate3-testcase 
|-- pom.xml 
`-- src 
    |-- main 
    | |-- java 
    | `-- resources 
    |  |-- Person.hbm.xml 
    |  `-- hibernate.cfg.xml 
    `-- test 
     `-- java 

pom.xml幾乎是空的,它只是包含了上面看到的插件配置和JUnit的依賴。該hibernate.cfg.xml包含:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
    <!-- Database connection settings --> 
    <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property> 
    <property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property> 
    <property name="connection.username">app</property> 
    <property name="connection.password">app</property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="dialect">org.hibernate.dialect.DerbyDialect</property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="show_sql">false</property> 

    <!-- Mapping files --> 
    <mapping resource="Person.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

而且Person.hbm.xml看起來如下:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-mapping 
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 

    <class name="Person" table="person"> 
    <id name="id" type="int"> 
     <generator class="increment" /> 
    </id> 

    <property name="name" column="cname" type="string" /> 
    </class> 

</hibernate-mapping> 

利用這種配置,運行mvn install產生Person.java如下圖所示:描述

$ cat target/generated-sources/hibernate3/Person.java 
// default package 
// Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GA 



/** 
* Person generated by hbm2java 
*/ 
public class Person implements java.io.Serializable { 


    private int id; 
    private String name; 

    public Person() { 
    } 

    public Person(String name) { 
     this.name = name; 
    } 

    public int getId() { 
     return this.id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return this.name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 




} 

一切正常。

+0

謝謝帕斯卡!我認爲這是一個好的開始,因爲它明確提供了「所有Maven」解決方案。我能看到的唯一剩下的問題是它指向一個單獨的配置文件(「hibernate.cfg.xml」)。我有一組配置文件,在Ant腳本中使用模式「**/*。hbm.xml」引用。有誰知道如何使用Maven和Pascal建議的插件來做到這一點? – Adam 2009-12-14 12:09:51

+0

我已經更新了我的答案以涵蓋該部分。 – 2009-12-14 13:13:47

+0

非常感謝!請看下面我的進一步評論。 – Adam 2009-12-14 23:20:55

0

帕斯卡,再次感謝您的幫助!您的解決方案運作良好

我在處理這件事時遇到的其他一些事情。第一個涉及這樣一個事實:這是一個相當大的項目,所以我將它分成多個Maven模塊來鏡像原始的ant多目錄構建。包含生成的類的模塊實際上並不執行任何數據庫訪問,因此hibernate.cfg.xml文件不需要,在這種情況下不應包含任何數據庫連接信息。我已經嘗試了這一點,它的工作原理與Pascal提供的文件的減少版本一樣,所有的屬性標籤都被刪除了。

有了這個,構建在命令行中工作正常。但是,儘可能地嘗試,我無法說服其他模塊在從Eclipse運行時拾取生成的類。暫且,所述溶液I具有與本是以下的行添加到下配置/組件/部件的POM:

<outputDirectory>/src/main/java</outputDirectory> 

這迫使那個蝕可以拾取它們的地方將要產生的文件爲其他模塊。完成此操作後,您必須在命令行上進行構建,然後請求Eclipse刷新源目錄的內容以獲取新文件。到目前爲止,我不知道一個更清潔的方式來處理這... ...

+1

很高興有幫助。不過,我有幾句話。首先,在'src/main/java'中生成代碼並不是最佳實踐,生成的代碼應該真正進入'target'。這背後的主要原因是我們希望'乾淨'來清潔它們。然後,關於Eclipse,包含生成的代碼的目錄確實被添加爲「源文件夾」。如果您使用的是m2eclipse插件,如果您右鍵單擊該項目,然後選擇* Maven> Update Project Configuration *,將自動完成此操作(請參閱http://docs.codehaus.org/display/M2ECLIPSE/Project+FAQ #ProjectFAQ產生)。 – 2009-12-14 23:58:15

+0

如果你正在使用maven-eclipse-plugin,調用'mvn eclipse:eclipse'將會產生一個'.classpath',它包含一個帶有'target/hibernate3/generated-sources'的'classpathentry'。所以,在這兩種情況下,都不需要在'src/main/java'下生成源代碼(這是件好事)。最後,關於我的hibernate.cfg.xml的內容,當然只是一個例子:)祝你的遷移過程順利! – 2009-12-15 00:02:17

+0

再次感謝!我的確在使用m2eclipse作爲插件,並且更新項目配置完成了這個技巧並解決了問題。此外,我同意你的觀點,即將生成的代碼保留在可以用類清理的地方。 – Adam 2009-12-15 22:28:10

0

如果您需要包括* .hbm.xml相編譯;編輯您的pom.xml,並添加下面的代碼:

<build> 
       <resources> 
      <resource> 
       <directory>source/com/qfund/orm/</directory> 
       <targetPath>com/qfund/orm/</targetPath> 
       <includes> 
        <include>*.hbm.xml</include> 
       </includes> 
      </resource> 
     </resources> 
     <testResources> 
      <testResource> 
       <directory>src/test/java/</directory> 
       <includes> 
        <include>*.xml</include> 
        <include>*.xsd</include> 
        <include>*.xslt</include> 
        <include>*.properties</include> 
       </includes> 
      </testResource> 
     </testResources> 
</build>