【问题标题】:How to execute HibernateToolTask from Ant when Hibernate Tools is a Maven dependency当 Hibernate Tools 是 Maven 依赖项时,如何从 Ant 执行 HibernateToolTask
【发布时间】:2014-11-08 22:40:43
【问题描述】:

我正在使用 Hibernate Tools 在我的项目中生成 pojo 和 dao。它目前正在使用 Run > Hibernate Code Generation 在 Hibernate 透视图中工作... 但是,我想将此自动化作为更复杂构建的一部分,我需要在其中进行一些预处理,运行休眠代码生成并进行一些后处理。 我有一个用于执行此操作的 Ant 构建文件,但我不知道如何引用 Maven 依赖项 jar

<?xml version="1.0" ?>
<!DOCTYPE project>
<project name="Hibernate Tools hbm2java" default="gensrc">

    <path id="tools">
        <!--
            Here {
        -->
        <path location="lib/hibernate-tools-4.3.1.CR1.jar"/>
        <!-- more dependencies... -->
        <!--
            }
        -->
        ...
    </path>
    <taskdef name="gen-src" classname="org.hibernate.tool.ant.HibernateToolTask"
             classpathref="tools" />
    <target name="gensrc">
        ...
    </target>
</project>

我收到此警告:

taskdef class org.hibernate.tool.ant.HibernateToolTask cannot be found using the classloader AntClassLoader[]

随之而来的构建错误:

BUILD FAILED
/.../hibernate-gen.xml:16: taskdef class org.hibernate.tool.ant.HibernateToolTask cannot be found using the classloader AntClassLoader[]

如何从 Maven 依赖项中引用 jar 来调用 org.hibernate.tool.ant.HibernateToolTask​​?

【问题讨论】:

    标签: java eclipse maven ant hibernate-tools


    【解决方案1】:

    要自动生成 pojo,您可以在 pom.xml 文件中添加一个 maven-antrun-plugin 插件。在这个插件的任务部分,您可以直接调用您描述的 Ant 任务。

    <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>
    

    否则,您可以通过使用 Hibernate 工具任务生成 pojo 类来实现自动化。参考这个git project从hbm生成pojos。

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 2012-10-25
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 2023-04-04
      • 2011-11-04
      相关资源
      最近更新 更多