|
接着,你要用到这个任务。例如,通过写一个目标(target)将源目录中的所有*.hbm.xml文件生成源代码。假设${src.hibernate}表示含有Hibernate映射文件的目录,${src.generated}就是你想放源代码的地方。这样Ant任务看起来就象是:
<target name="codegen" description="Generate Java source code from the Hibernate mapping files"> <hbm2java output="${source.generated}"> <fileset dir="${src.hibernate}"> <include name="**/*.hbm.xml"/> </fileset> </hbm2java> </target>
用Maven 1 定制构建过程
为结合进Maven ( 1.0)构建过程,你需要修改maven.xml文件。Maven代码就存放在这个文件中。脚本(script)主要检查了Hibernate映射文件自上次类生成后是否已被更改(使用uptodate 标记),如果没有,就调用此前所描述的Ant中的hbm2java任务。这种情况下,我们做了以下的假设:
· hbm2java.xml配置文件应在src/hibernate目录中 · Hibernate映射文件应在src/hibernate目录中 · 在src/generated/src/java目录下生成Java类
<goal name="generate-hibernate-classes"> <ant:echo message="Hibernate class generation"/>
<fileset dir="${basedir}/src" id="fileset.hbm.xml"> <include name="**/*.hbm.xml"/> </fileset>
<uptodate property="hibernateBuild.uptodate" targetfile="${maven.src.dir}/generated/hbm.jar"> <srcfiles refid="fileset.hbm.xml"/> </uptodate>
<j:set var="buildHibernateFiles" value="${hibernateBuild.uptodate}"/>
<j:choose> <j:when test="${buildHibernateFiles != null}"> <ant:echo message="Hibernate classes up to date"/> </j:when> <j:otherwise> <ant:echo message="Generating Hibernate classes to src/java"/>
<ant:taskdef name="hbm2java" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref="maven.dependency.classpath"/>
<ant:hbm2java config="${maven.src.dir}/conf/hbm2java.xml" output="${maven.src.dir}/generated/src/java" > <ant:fileset dir="${maven.src.dir}/hibernate"> <ant:include name="**/*.hbm.xml"/> </ant:fileset> </ant:hbm2java>
<ant:jar jarfile="${maven.src.dir}/generated/hbm.jar"> <fileset refid="fileset.hbm.xml"/> </ant:jar> </j:otherwise> </j:choose> </goal>
用Maven 2 定制构建过程
若碰巧你正使用Maven 2,事情就更简单一点了。把maven-antrun-plugin插件添加到pom.xml文件中,而不是在maven.xml文件中使用完整的goals属性(pre and post goals)。在此插件中的task那部分,你可以象上述那样直接调用Ant 任务。 <project...> <modelVersion>4.0.0</modelVersion> ... <build> ... <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>generate-sources</phase> <configuration> <tasks> <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="maven.dependency.classpath"/>
<hbm2java output="src/generated"> <fileset dir="src/hibernate"> <include name="**/*.hbm.xml"/> </fileset> </hbm2java> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 在Hibernate 3中使用hbm2java
Hbm2java 工具已经经受了Hibernate 3 的考验。hbm2java任务与其它相似的任务一起,被集成进了新版Hibernate 工具集中的hibernatetool任务(撰写此文时仍是alpha版)。Ant任务需要在类路径(class path)中查找以下的.jar 文件:
· hibernate-tools.jar · velocity-1.4.jar |