How to get hibernate3-maven-plugin hbm2ddl to find

2019-04-15 18:07发布

问题:

I have a Java project I am building with Maven. I am now trying to get the hibernate3-maven-plugin to run the hbm2ddl tool to generate a schema.sql file I can use to create the database schema from my annotated domain classes. This is a JPA application that uses Hibernate as the provider.

In my persistence.xml file I call out the mysql driver:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

When I run Maven, I see it processing all my classes, but when it goes to output the schema, I get the following error:

ERROR org.hibernate.connection.DriverManagerConnectionProvider - JDBC Driver class not found: com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I have the MySQL driver as a dependency of this module. However it seems like the hbm2ddl tool cannot find it. I would have guessed that the Maven plugin would have known to search the local Maven file repository for this driver. What gives?

The relevant part of my pom.xml is this:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>hibernate3-maven-plugin</artifactId>
   <executions>
      <execution>
         <phase>process-classes</phase>
         <goals>
            <goal>hbm2ddl</goal>
          </goals>
      </execution>
   </executions>
   <configuration>
       <components>
          <component>
             <name>hbm2ddl</name>
             <implementation>jpaconfiguration</implementation>
          </component>
        </components>
        <componentProperties>
            <persistenceunit>my-unit</persistenceunit>
        </componentProperties>
   </configuration>       
</plugin>

回答1:

I figured it out. You have to add the corresponding JDBC driver as a dependency of the PLUGIN. Adding it as a dependency of the module does nothing. This seems surprising to me and kind of lame actually.

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <type>jar</type>
            <version>5.0.8</version>
        </dependency>
    </dependencies>