Unable to generate hbm2ddl using hibernate3-maven-

2019-02-08 23:24发布

问题:

I have updated to a newer version of hibernate3-maven-plugin. I get the following error trying to use the plugin mentioned below.

Would appreciate any pointers in resolving this issue.

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

回答1:

The way of configuration changed to direct usage of the ant hibernate tool plugin. So the configuration is exactly the same format like the ant plugin without the need of additional taskDef for e.g. jpaconfiguration. See hibernate ant tool references documentation: http://docs.jboss.org/tools/3.3.0.Final/en/hibernatetools/html_single/index.html#d0e4651 for more information.

For a hbm2ddl with a jpa configuration you could use the following:

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

On failures there is the "target/antrun/build-main.xml" file which configures the hibernate tools. For the above example this looks like following:

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


回答2:

I had this same issue, and in the end it was solved by following this example (http://www.celinio.net/techblog/?p=1125) and by specifying the hibernate deps for the plugin alone. This is because in my case I have a separate domain object module which only uses JPA2 (no specific hibernate references), so I needed to pull in the deps for the DDL generation, without worrying about them affecting the dependents of this module.

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


回答3:

I have make it work as following:

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

The basic idea is to use the goal 'run' and then configure hibernatetool to run the exporters you want. So you can run multiple exporters in one go by adding more exporters configuration inside the tag. For more information, look here http://docs.jboss.org/tools/2.0.0.GA/hibernatetools/en/html_single/index.html and http://mojo.codehaus.org/hibernate3-maven-plugin/examples/run-multiple-goals.html. It took me a couple of hours to figure out. Hope that help!