无法使用hibernate3-maven-plugin-3.0生成hbm2ddl

时间:2012-02-14 12:23:25

标签: hibernate hbm2ddl

我已更新到更新版本的hibernate3-maven-plugin。尝试使用下面提到的插件时出现以下错误。

非常感谢解决这个问题的任何指示。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>3.0</version>

    <executions>
        <execution>
            <id>create sql schema</id>
            <phase>process-test-resources</phase>
            <goals>
                <goal>hbm2ddl</goal>
            </goals>
            <configuration>
                <componentProperties>
                    <persistenceunit>${app.module}</persistenceunit>
                    <drop>false</drop>
                    <create>true</create>
                    <outputfilename>${app.sql}-create.sql</outputfilename>
                    <skip>${db.schema.gen.skip}</skip>
                </componentProperties>
            </configuration>
        </execution>

        <execution>
            <id>drop sql schema</id>
            <phase>process-test-resources</phase>
            <goals>
                <goal>hbm2ddl</goal>
            </goals>
            <configuration>
                <componentProperties>
                    <persistenceunit>${app.module}</persistenceunit>
                    <drop>true</drop>
                    <create>false</create>
                    <outputfilename>${app.sql}-drop.sql</outputfilename>
                    <skip>${db.schema.gen.skip}</skip>
                </componentProperties>
            </configuration>
        </execution>
    </executions>
</plugin>

[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (create sql schema) on project sample: There was an error creating the AntRun task. NullPointerException -> [Help 1]org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (create sql schema) on project framework: There was an error creating the AntRun task.

3 个答案:

答案 0 :(得分:12)

配置方式改为直接使用ant hibernate工具插件。所以配置与ant插件的格式完全相同,而不需要额外的taskDef。 jpaconfiguration。有关详细信息,请参阅hibernate ant工具参考文档:http://docs.jboss.org/tools/3.3.0.Final/en/hibernatetools/html_single/index.html#d0e4651

对于具有jpa配置的hbm2ddl,您可以使用以下命令:

<plugin>
    <!-- run "mvn hibernate3:hbm2ddl" to generate a schema -->
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>hibernate3-maven-plugin</artifactId>
    <version>3.0</version>

    <configuration>
        <hibernatetool>
            <jpaconfiguration persistenceunit="unitname" />

            <hbm2ddl export="false" create="true"
                update="true" format="true" outputfilename="schemaDiff.ddl" />

        </hibernatetool>
    </configuration>
</plugin>

失败时,会有“target / antrun / build-main.xml”文件配置hibernate工具。对于上面的示例,如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<project name="maven-antrun-" default="main"  >
<target name="main">
  <taskdef classname="org.hibernate.tool.ant.EnversHibernateToolTask" name="hibernatetool"/>
  <mkdir dir="/home/xxx/workspace/projectname/target/sql/hibernate3"/>
  <hibernatetool destdir="/home/xxx/workspace/projectname/target/sql/hibernate3">
    <jpaconfiguration persistenceunit="schemaDiff"/>
    <hbm2ddl update="true" export="false" outputfilename="schemaDiff.ddl" format=
"true" create="true"/>
  </hibernatetool>
</target>
</project>

答案 1 :(得分:3)

我有同样的问题,最后通过遵循这个例子(http://www.celinio.net/techblog/?p=1125)并通过单独指定插件的hibernate deps来解决它。这是因为在我的情况下,我有一个单独的域对象模块,它只使用JPA2(没有特定的hibernate引用),所以我需要为DDL生成拉入deps,而不用担心它们会影响这个模块的依赖。

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>hibernate3-maven-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <components>
                    <component>
                        <name>hbm2ddl</name>
                        <implementation>jpaconfiguration</implementation>
                    </component>
                </components>
                <hibernatetool>
                    <classpath>
                        <path location="${project.build.directory}/classes" />
                        <path location="${project.basedir}/src/main/resources/META-INF/" />
                    </classpath>
                    <jpaconfiguration persistenceunit="Configuration" />
                    <hbm2ddl create="true" export="false" drop="true"
                        outputfilename="configuration.sql" format="true" console="true" />
                </hibernatetool>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate.javax.persistence</groupId>
                    <artifactId>hibernate-jpa-2.0-api</artifactId>
                    <version>1.0.0.Final</version>
                </dependency>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                    <version>3.6.7.Final</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

答案 2 :(得分:3)

我的工作如下:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>3.0</version>
<executions>
    <execution>
        <id>create-schema</id>
        <phase>process-test-resources</phase>
        <goals>
            <goal>run</goal>
        </goals>
        <configuration>
            <hibernatetool destdir="${project.basedir}">
                <classpath>
                    <path
                        location="${project.basedir}/src/main/resources/mappings/" />
                </classpath>
                <configuration
                    configurationfile="${project.basedir}/src/test/resources/hibernate.cfg.xml" />
                <hbm2ddl create="true" export="false"
                    drop="true" outputfilename="schema.sql"
                    format="true" console="false" />
            </hibernatetool>
        </configuration>
    </execution>
</executions>

基本思想是使用目标'run'然后配置hibernatetool来运行你想要的导出器。因此,您可以通过在标记内添加更多导出器配置来一次运行多个导出器。有关详细信息,请查看此处http://docs.jboss.org/tools/2.0.0.GA/hibernatetools/en/html_single/index.htmlhttp://mojo.codehaus.org/hibernate3-maven-plugin/examples/run-multiple-goals.html。 我花了几个小时才弄明白。希望有所帮助!