如何在 mvn clean install 中配置 maven hbm2hbmxml 和 hbm2java 一个接一个地运行

Posted

技术标签:

【中文标题】如何在 mvn clean install 中配置 maven hbm2hbmxml 和 hbm2java 一个接一个地运行【英文标题】:How to configure maven hbm2hbmxml and hbm2java to run one after the other in mvn clean install 【发布时间】:2011-01-06 05:23:36 【问题描述】:

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

谢谢

【问题讨论】:

我更新了我的答案,说明如何“手动”(通过 maven)将文件复制到所需位置 【参考方案1】:

如果你想编译你的模型 java 文件(由 reveng 获得),你不需要运行 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>

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

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>

你用:

mvn clean hibernate3:hbm2java compile

如果你想让它被触发:

mvn clean compile

在你的插件定义中添加“executions”标签

            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals><goal>hbm2java</goal></goals>
                </execution>
            </executions>

【讨论】:

【参考方案2】:

hibernate3-maven-plugin 版本 3.0hbm2java

的工作示例
<profile>
    <id>hbm2java</id>
    <build>
    <plugins>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>3.0</version>
        <configuration>
            <hibernatetool>
            <classpath>
                <path location="$project.build.directory/classes" />
            </classpath>
            <jdbcconfiguration propertyfile="$basedir/helper/hibernate.properties" revengfile="$basedir/helper/hibernate-reverse-engineering.xml"
                reversestrategy="de.hibernate.ExampleStrategy" />
            <hbm2java jdk5="true" ejb3="true" destdir="$project.build.sourceDirectory" />
            </hibernatetool>
        </configuration>
        <executions>
            <execution>
            <goals>
                <goal>hbm2java</goal>
            </goals>
            <!-- must be compile or higher to find ExampleStrategy class in same project -->
            <phase>compile</phase>
            </execution>
        </executions>
        <dependencies>
            <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.2.GA</version>
            </dependency>
            <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.1_3</version>
            </dependency>
            <dependency>
            <groupId>com.oracle.jdbc</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>$ojdbc6.version</version>
            </dependency>
        </dependencies>
        </plugin>
    </plugins>
    </build>
</profile>

【讨论】:

【参考方案3】:

这两个答案都不适合我开箱即用。经过一番研究,我能够从数据库中生成 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>

创建要导入的表的列表。再次在测试分支中: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>

将 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>

运行maven

mvn hibernate3:hbm2java

【讨论】:

【参考方案4】:

在你的 pom 中添加 Hibernate 2 插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>2.2</version>

            <executions>
                <execution>
                    <id>generate-mapping-files</id>
                    <phase>compile</phase>

                 <goals>
                    <goal>hbm2hbmxml</goal>
                    <goal>hbm2cfgxml</goal>
                    <goal>hbm2java</goal>
                </goals>
...

然后在模型 Reveng 中放这个。

<!-- Primary Tables -->
 <schema-selection match-schema="TEST_SCHEMA" match-table="TEST_TABLE" />

然后只需在 maven 中使用 clean install 构建您的项目,模型类就会从数据库中自动生成。

【讨论】:

【参考方案5】:

Maven lifecycle

mvn clean dependency:copy-dependencies package

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

所以,也许

mvn clean hibernate3:hbm2hbmxml hibernate3:hbm2java package

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

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

【讨论】:

Bozho,这并不是我对问题的理解(这根本不是微不足道的,想要的工作流程涉及 hibernate3 插件的非常棘手的配置)。但也许我错过了什么。 我也不确定我是否得到了一切,但至少他应该这样尝试一下,看看会发生什么。 感谢您的回复。我正在寻找的是能够使这些目标成为我持续集成过程的一部分。我设法使 hbm2hbmxml 工作,但将 *.hbm.xml 文件放在 ./target/hibernate3/generated-mappings/mypackage 下。当我运行 hbm2java 时,我收到一条失败消息,提示“找不到 mypackage/Domain.hbm.xml”。插件不应该知道在哪里可以找到这些文件吗?我通过添加 条目来运行它,并且 hbm2java 生成源代码,但同样,它将 Java 文件放在目标文件夹下,并且当我编译时它不会编译这些文件。有什么线索吗?谢谢【参考方案6】:

以下配置对我有用。 (示例带有 Derby 数据库和 1 个表) mvn clean package 做到了这一切。 插件配置:

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

hibernate.cfg.xml:

<hibernate-configuration>
<session-factory>
    <property name="connection.url">jdbc:derby://localhost:1527/demo</property>
    <property name="connection.username">app</property>
    <property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="dialect">org.hibernate.dialect.DerbyDialect</property>
    <property name="connection.password">password</property>
    <property name="hibernate.show_sql">true</property>

    <mapping resource="Tag.hbm.xml" />
</session-factory>

【讨论】:

以上是关于如何在 mvn clean install 中配置 maven hbm2hbmxml 和 hbm2java 一个接一个地运行的主要内容,如果未能解决你的问题,请参考以下文章

pom.xml配置,针对mvn clean install -P参数(环境参数)打包

mvn install 包含 mvn clean吗

“mvn clean install”和“mvn clean verify install”有啥区别?

“mvn clean install”与“mvn install”有何不同?

Run “mvn clean install” in Eclipse

mvn clean package install deploy