2010-05-30 60 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