因项目需要,要在工程运行时根据hbm生成POJO和数据库定义。在DOS里直接执行ANT没什么问题,但是用JAVA代码执行ANT时就报错!
(强调,直接执行ANT没问题,用JAVA代码执行就出错误,同样的JAVA代码执行其它的ANT的build也没问题)
错误如下:
hbm2java:
 [hbm2java] Executing Hibernate Tool with a Standard Configuration
 [hbm2java] 1. task: hbm2java (Generates a set of .java files)
2009-12-17 14:31:30,453 INFO [org.hibernate.cfg.Environment] - <Hibernate 3.2 cr3>
2009-12-17 14:31:30,468 INFO [org.hibernate.cfg.Environment] - <hibernate.properties not found>
2009-12-17 14:31:30,468 INFO [org.hibernate.cfg.Environment] - <Bytecode provider name : cglib>
2009-12-17 14:31:30,468 INFO [org.hibernate.cfg.Environment] - <using JDK 1.4 java.sql.Timestamp handling>
2009-12-17 14:31:30,515 INFO [org.hibernate.cfg.Configuration] - <configuring from file: hibernate.cfg.xml>
2009-12-17 14:31:30,578 INFO [org.hibernate.cfg.Configuration] - <Reading mappings from resource: WEB-INF/classes/edu/bupt/struts/bf/User.hbm.xml>
 [hbm2java] An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
 [hbm2java] To get the full stack trace run ant with -verbose
 [hbm2java] org.hibernate.MappingException: Resource: WEB-INF/classes/edu/bupt/struts/bf/User.hbm.xml not foundBUILD FAILED
org.hibernate.MappingException: Resource: WEB-INF/classes/edu/bupt/struts/bf/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:517)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1511)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1479)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1386)
at org.hibernate.tool.ant.ConfigurationTask.doConfiguration(ConfigurationTask.java:108)
at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:55)
at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:226)
at org.hibernate.tool.ant.HibernateToolTask.getProperties(HibernateToolTask.java:242)
at org.hibernate.tool.ant.ExporterTask.configureExporter(ExporterTask.java:94)
at org.hibernate.tool.ant.Hbm2JavaExporterTask.configureExporter(Hbm2JavaExporterTask.java:34)
at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:39)
at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:160)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:357)
at org.apache.tools.ant.Target.performTasks(Target.java:385)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
at edu.bupt.service.RunAnt.main(RunAnt.java:36)Total time: 0 seconds
ANT的build文件如下:
<?xml version="1.0" encoding="UTF-8"?><project name="recommend" default="hbm2ddl">    <property name="sourcedir" value="${basedir}/WEB-INF/classes/edu/bupt/struts/bf"/>
    <property name="targetdir" value="${basedir}/WEB-INF/classes/edu/bupt/struts/bf"/>
    <property name="librarydir" value="${basedir}/WEB-INF/lib"/>
    <property name="schema.dir" value="${basedir}/WEB-INF/classes/edu/bupt/struts/bf"/> 
<property name="srcdir" value="${basedir}/WEB-INF/classes/edu/bupt/struts/bf/edu/bupt/vo"/> 
<property name="fromclass" value="${basedir}/WEB-INF/classes/edu/bupt/struts/bf/edu/bupt/vo/Movieinfo.class"/> 
<property name="fromxml" value="${basedir}/WEB-INF/classes/edu/bupt/struts/bf/Movieinfo.hbm.xml"/> 
<property name="todir" value="${basedir}/WEB-INF/classes/edu/bupt/vo"/>     <path id="libraries">
        <fileset dir="${librarydir}">
            <include name="*.jar"/>
        </fileset>
    </path>    <path id="project.class.path">
    <!-- Include our own classes, of course -->
        <pathelement location="${targetdir}" />
    <!-- Include jars in the project library directory -->
        <fileset dir="${librarydir}">
            <include name="*.jar"/>
        </fileset>
    </path>   
    <!-- create .java form  *.hbm.xml -->
    <target name="hbm2java" 
        description="Generate Java source from the O/R mapping files">    
        <taskdef name="hbm2java" 
            classname="org.hibernate.tool.ant.HibernateToolTask" 
            classpathref="project.class.path"/>
            <hbm2java destdir="${targetdir}">
                <configuration configurationfile="${targetdir}/hibernate.cfg.xml" />  
                <hbm2java  jdk5="true"/>            
                <!-- <cfg2hbm/> --> 
            </hbm2java>          
     <echo>hbm2java,java</echo>
    </target>
    
<target name="compile" depends="hbm2java"
    description="compile the source file to class and copy the class file">
<javac srcdir="${srcdir}"/>
<echo>compilation complete!</echo>
<!--copy Movieinfo.class-->
<copy file="${fromclass}" todir="${todir}" overwrite="true"/>

<!--copy Movieinfo.hbm.xml-->
<copy file="${fromxml}" todir="${todir}" overwrite="true"/> <echo>copy complete!</echo>
    </target>


    <!-- create ddl from  *.hbm.xml -->
    <target name="hbm2ddl" depends="compile"
            description="Generate DB schema from the O/R mapping files">
        <taskdef name="hbm2ddl" 
            classname="org.hibernate.tool.ant.HibernateToolTask" 
            classpathref="project.class.path"/>
        <hbm2ddl destdir="${schema.dir}">
            <configuration configurationfile="${targetdir}/hibernate.cfg.xml" /> 
            <hbm2ddl export="true" console="false" create="true" update="true" drop="false" outputfilename="recommend.sql"/> 
        </hbm2ddl>   
    </target>

</project>JAVA代码如下:
package edu.bupt.service;
import java.io.File;import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;public class RunAnt { /**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
File buildFile = new File("C:\\Tomcat 6.0\\webapps\\recommend\\build.xml");
//创建一个ANT项目
Project p = new Project();
        
//创建一个默认的监听器,监听项目构建过程中的日志操作
DefaultLogger consoleLogger = new DefaultLogger();
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger); try { p.fireBuildStarted();
//初始化该项目
p.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
//解析项目的构建文件
helper.parse(p, buildFile);
//执行项目的默认目标
p.executeTarget(p.getDefaultTarget());
p.fireBuildFinished(null); } catch (BuildException e) { p.fireBuildFinished(e); } }}

解决方案 »

  1.   

    楼主看看下面错误就知道,应该是hibernate影射文件找不到.BUILD FAILED 
    org.hibernate.MappingException: Resource: WEB-INF/classes/edu/bupt/struts/bf/User.hbm.xml not found
      

  2.   


    谢谢,这个问题已经解决。开始确实看到那个原因了,但是改成绝对路径仍然报同样的错。折腾了好长时间才找到原因,路径应该改为:edu/bupt/struts/bf/User.hbm.xml,因为路径是从classes 开始的。
    而其报错方式很难让人看到是这方面的原因。