如何配置maven hbm2hbmxml和hbm2java在mvn clean install中依次运行

seb*_*anr 9 java hibernate maven

我需要能够调用mvn clean install并让maven调用hibernate3:hbm2hbmxml来从数据库生成映射文件,然后调用hbm2java来获取Java文件,然后让maven编译那些新创建的Java文件.有没有人这样做过?

谢谢

小智 14

如果你想拥有你的模型Java文件编译(通过复仇获得),你并不需要运行hbm2hbmxml.

插件配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <components>
                    <component>
                        <name>hbm2java</name>
                        <outputDirectory>src/main/java</outputDirectory>
                        <implementation>jdbcconfiguration</implementation>
                    </component>
                </components>
                <componentProperties>
                    <revengfile>/src/main/resources/reveng/model.reveng.xml</revengfile>
                    <propertyfile>/src/main/resources/META-INF/hibernate.properties</propertyfile>
                    <jdk5>true</jdk5>
                    <ejb3>true</ejb3>
                </componentProperties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.0.8</version>
                </dependency>
                <dependency>
                    <groupId>cglib</groupId>
                    <artifactId>cglib-nodep</artifactId>
                    <version>2.1_3</version>
                </dependency>
            </dependencies>             
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

hibernate.properties:

hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/YOUR_DB
hibernate.connection.username = yourUsrName
hibernate.connection.password= yourPwd
hibernate.default_schema = YOUR_DB
Run Code Online (Sandbox Code Playgroud)

model.reveng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
    <table-filter match-name=".*" package="your.package.here" />
</hibernate-reverse-engineering>
Run Code Online (Sandbox Code Playgroud)

你解雇这个:

mvn clean hibernate3:hbm2java compile
Run Code Online (Sandbox Code Playgroud)

如果你想让它被解雇只是:

mvn clean compile
Run Code Online (Sandbox Code Playgroud)

在插件定义中添加"executions"标记

            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals><goal>hbm2java</goal></goals>
                </execution>
            </executions>
Run Code Online (Sandbox Code Playgroud)


Ste*_*ver 5

这两个答案都没有为我开箱即用.经过一些研究,我能够从数据库中生成POJO.希望这快速追踪某人.

只需生成java文件 - 不生成映射文件.

在src/test/resources/reveng/hibernate.cfg.xml中定义数据库连接.使用测试分支,这些文件不会复制到可分发工件中.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="pmSessionFactory">
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <!-- Note that we are pointing directly at the catalog so we can use 
             unqualified table names -->
        <property name="hibernate.connection.url">jdbc:oracle:thin:@server.domain.com:1521:catalog</property>
        <property name="hibernate.connection.password">login</property>
        <property name="hibernate.connection.username">****</property>
        <property name="hibernate.default_schema">PM</property>
    </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

创建要导入的表的列表.再次在测试分支中:src/test/resources/reveng/model.reveng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC 
  "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" 
  "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>
  <!-- This assumes your database connection is pointing to the proper catalog -->
  <!-- To get all tables in the named schema, use the following 
       <schema-selection match-schema="PM" />
  -->
  <!--  to get only the named tables -->
  <schema-selection match-schema="PM" match-table="PM_PROPERTY"/>
  <schema-selection match-schema="PM" match-table="PM_APPLICATION"/>
  <schema-selection match-schema="PM" match-table="PM_PROPERTY_TYPE"/>
</hibernate-reverse-engineering>
Run Code Online (Sandbox Code Playgroud)

将hibernate3 maven插件添加到您的pom中

<build>
  <plugins>
    ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>hibernate3-maven-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <components>
          <component>
            <name>hbm2java</name>
            <outputDirectory>src/main/java/com/me/examples/pm/data</outputDirectory>
            <implementation>jdbcconfiguration</implementation>
          </component>
        </components>
        <componentProperties>
          <!-- Storing the reveng files in the test branch means we are not 
               deploying connection information-->
          <revengfile>src/test/resources/reveng/model.reveng.xml</revengfile>
          <configurationfile>src/test/resources/reveng/hibernate.cfg.xml</configurationfile>
          <jdk5>true</jdk5>
          <ejb3>true</ejb3>
        </componentProperties>
      </configuration>
      <dependencies>
        <dependency>
          <groupId>com.oracle</groupId>
          <artifactId>classes12</artifactId>
          <version>10.2.0.1.0</version>
        </dependency>
        <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib-nodep</artifactId>
          <version>2.1_3</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

跑maven

mvn hibernate3:hbm2java
Run Code Online (Sandbox Code Playgroud)


Boz*_*zho 1

Maven 生命周期

mvn clean dependency:copy-dependencies package
Run Code Online (Sandbox Code Playgroud)

如果要执行此操作,则将首先执行 clean 阶段(这意味着它将运行 clean 生命周期的所有前面的阶段,加上 clean 阶段本身),然后是 dependency:copy-dependencies 目标,最后执行 package 阶段(以及默认生命周期的所有先前构建阶段)。

所以,也许:

mvn clean hibernate3:hbm2hbmxml hibernate3:hbm2java package
Run Code Online (Sandbox Code Playgroud)

也就是说,我建议不要永久生成类。这让你非常不灵活。

在您发表评论之后,这似乎是休眠插件的“不明智”行为。您可以使用Maven antrun 插件“手动”将所需文件复制到所需目录来绕过该问题。