我需要能够调用mvn clean install并让maven调用hibernate3:hbm2hbmxml从数据库生成映射文件,然后调用hbm2java来获取Java文件,然后让maven编译那些新创建的Java文件。以前有人这么做过吗?
谢谢
发布于 2010-09-15 16:37:33
如果你想编译你的模型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_DBmodel.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>发布于 2011-12-09 15:07:37
这两个答案对我来说都不是开箱即用的。经过一些研究,我能够从数据库生成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发布于 2010-01-18 15:21:37
Maven lifecycle
mvn clean dependency:copy-dependencies package如果要执行此操作,将首先执行clean阶段(这意味着它将运行clean生命周期的所有先前阶段,加上clean阶段本身),然后执行dependency:copy-dependencies目标,最后执行包阶段(以及默认生命周期的所有先前构建阶段)。
所以,也许
mvn clean hibernate3:hbm2hbmxml hibernate3:hbm2java package也就是说,我建议不要永久地生成类。这让你变得非常不灵活。
在你的评论之后,这似乎是hibernate插件的“不明智”行为。您可以使用Maven antrun plugin将所需的文件“手动”复制到所需的目录,从而绕过此步骤。
https://stackoverflow.com/questions/2083727
复制相似问题