那麼,如果你不想混合Ant和Maven(這裏是一個好主意),那麼就有Maven Hibernate3 Plugin。它有一個hbm2java
目標,默認綁定到generate-sources
階段。請參閱魔的網站,瞭解更多的細節,但該插件威力的設置看起來是這樣的:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>hbm2java</goal>
</goals>
</execution>
</executions>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>configuration</implementation>
<outputDirectory>target/generated-sources/hibernate3</outputDirectory>
</component>
</components>
<componentProperties>
<drop>true</drop>
<jdk5>true</jdk5>
<configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
</plugin>
編輯:插件實際上看起來在target/classes
.hbm.xml
生成Java源文件。所以,如果你把你的映射文件放在src/main/resources
中,它們將會在插件調用的process-resources
階段被複制到target/classes
中,而且事情將會正常工作。我剛剛與下面的示例項目測試了這個:
maven-hibernate3-testcase
|-- pom.xml
`-- src
|-- main
| |-- java
| `-- resources
| |-- Person.hbm.xml
| `-- hibernate.cfg.xml
`-- test
`-- java
的pom.xml
幾乎是空的,它只是包含了上面看到的插件配置和JUnit的依賴。該hibernate.cfg.xml
包含:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby://localhost:1527/mydatabase</property>
<property name="connection.username">app</property>
<property name="connection.password">app</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
而且Person.hbm.xml
看起來如下:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Person" table="person">
<id name="id" type="int">
<generator class="increment" />
</id>
<property name="name" column="cname" type="string" />
</class>
</hibernate-mapping>
利用這種配置,運行mvn install
產生Person.java
如下圖所示:描述
$ cat target/generated-sources/hibernate3/Person.java
// default package
// Generated Dec 14, 2009 2:19:22 PM by Hibernate Tools 3.2.2.GA
/**
* Person generated by hbm2java
*/
public class Person implements java.io.Serializable {
private int id;
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
一切正常。
新maven3慣例似乎是 $ {} project.build.directory /生成/ Hibernate3中/主/ JAVA 而不是 $ { project.build.directory}/generated/hibernate3 我很難找到文檔來支持這個。新的m2Eclipse插件似乎使用這個新的約定。這在使用「更新項目配置」功能時尤其明顯。 – 2010-06-04 19:37:18