首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mvn全新安装中配置maven hbm2hbmxml和hbm2java依次运行

如何在mvn全新安装中配置maven hbm2hbmxml和hbm2java依次运行
EN

Stack Overflow用户
提问于 2010-01-18 11:22:49
回答 6查看 23.1K关注 0票数 9

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

谢谢

EN

回答 6

Stack Overflow用户

发布于 2010-09-15 16:37:33

如果你想编译你的模型java文件(通过reveng获得),你不需要运行hbm2hbmxml。

插件配置:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<?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>

您可以使用以下命令来启动此命令:

代码语言:javascript
复制
mvn clean hibernate3:hbm2java compile

如果您希望仅使用以下命令来触发它:

代码语言:javascript
复制
mvn clean compile

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

代码语言:javascript
复制
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals><goal>hbm2java</goal></goals>
                </execution>
            </executions>
票数 14
EN

Stack Overflow用户

发布于 2011-12-09 15:07:37

这两个答案对我来说都不是开箱即用的。经过一些研究,我能够从数据库生成POJO。希望这能快速追踪到某个人。

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

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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<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

代码语言:javascript
复制
mvn hibernate3:hbm2java
票数 5
EN

Stack Overflow用户

发布于 2010-01-18 15:21:37

Maven lifecycle

代码语言:javascript
复制
mvn clean dependency:copy-dependencies package

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

所以,也许

代码语言:javascript
复制
mvn clean hibernate3:hbm2hbmxml hibernate3:hbm2java package

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

在你的评论之后,这似乎是hibernate插件的“不明智”行为。您可以使用Maven antrun plugin将所需的文件“手动”复制到所需的目录,从而绕过此步骤。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2083727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档