hibernate3-maven:从父项目编译时,在子项目中找不到 JDBC 驱动程序

发布于 2024-12-07 16:20:10 字数 6808 浏览 0 评论 0原文

问题: 12:03:10,126 错误 org.hibernate.tool.hbm2ddl.SchemaExport - 架构导出不成功 org.hibernate.HibernateException:未找到 JDBC 驱动程序类:com.mysql.jdbc.Driver

我有一个项目分为模块:ParentProject 和 ChildModule。当我尝试编译 ChildModule 的 pom.xml 时,一切正常,maven 成功连接到数据库并创建表。然而,当从 ParentProject 编译时,我得到了上面提到的错误(在执行 hbm2ddl 时)。有什么想法有什么问题吗?

这是我的 pom.xml 文件:

ParentProject pom.xml:

<project ... >
<build>
        <plugins>

            <plugin>
                <!-- JDK version used to compile project -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

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

        </plugins>
    </build>

    <repositories>
        <repository>
            <id>JBOSS</id>
            <name>JBoss Repository</name>
            <url>http://repository.jboss.org/maven2/</url>
        </repository>
        <repository>
            <id>Codehaus Snapshots</id>
            <url>http://snapshots.repository.codehaus.org/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>Codehaus Snapshots</id>
            <url>http://snapshots.repository.codehaus.org/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

    <dependencyManagement>
        <dependencies>
        ...
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>../ChildModule</module>
    </modules>  
</project>

ChildModule pom.xml:

<project ... >

    <parent>
        <groupId>com.somepackage</groupId>
        <artifactId>ChildModule</artifactId>
        <version>0.1</version>
        <relativePath>../ParentProject/pom.xml</relativePath>
    </parent>

    ...

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

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

                    <execution>
                        <id>generate-entities</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>hbm2java</goal>
                        </goals>
                        <configuration>
                            <components>
                                <component>
                                    <name>hbm2java</name>
                                    <implementation>configuration</implementation>
                                    <outputDirectory>${generated-source-dir}</outputDirectory>
                                </component>
                            </components>
                            <componentProperties>                               
                                <configurationFile>src/main/resources/hibernate.cfg.xml</configurationFile>
                                <jdk5>true</jdk5>
                            </componentProperties>
                        </configuration>
                    </execution>

                    <execution>
                        <id>generate-schema</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>hbm2ddl</goal>
                        </goals>

                        <configuration>
                            <componentProperties>
                                <outputfilename>schema.ddl</outputfilename>
                                <drop>true</drop>
                                <ejb3>false</ejb3>
                            </componentProperties>
                        </configuration>

                    </execution>

                </executions>               
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.17</version>
                    </dependency>
                    <dependency>
                        <groupId>cglib</groupId>
                        <artifactId>cglib-nodep</artifactId>
                        <version>2.2.2</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>


    <dependencies>

    ...

    </dependencies>

    <properties>
        <generated-source-dir>generated-sources/hibernate3</generated-source-dir>
        <generated-resource-dir>generated-resources/hibernate3</generated-resource-dir>
    </properties>

</project>

Problem:
12:03:10,126 ERROR org.hibernate.tool.hbm2ddl.SchemaExport - schema export unsuccessful org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver

I have a project divided into modules: ParentProject and ChildModule. When I try to compile pom.xml of ChildModule everything works fine, maven successfully connects to the database an creates the tables. However, when compiling from ParentProject I got error mentioned above (while executing hbm2ddl). Any ideas what is the problem?

Here are my pom.xml files:

ParentProject pom.xml:

<project ... >
<build>
        <plugins>

            <plugin>
                <!-- JDK version used to compile project -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

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

        </plugins>
    </build>

    <repositories>
        <repository>
            <id>JBOSS</id>
            <name>JBoss Repository</name>
            <url>http://repository.jboss.org/maven2/</url>
        </repository>
        <repository>
            <id>Codehaus Snapshots</id>
            <url>http://snapshots.repository.codehaus.org/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>Codehaus Snapshots</id>
            <url>http://snapshots.repository.codehaus.org/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>true</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

    <dependencyManagement>
        <dependencies>
        ...
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>../ChildModule</module>
    </modules>  
</project>

ChildModule pom.xml:

<project ... >

    <parent>
        <groupId>com.somepackage</groupId>
        <artifactId>ChildModule</artifactId>
        <version>0.1</version>
        <relativePath>../ParentProject/pom.xml</relativePath>
    </parent>

    ...

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

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

                    <execution>
                        <id>generate-entities</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>hbm2java</goal>
                        </goals>
                        <configuration>
                            <components>
                                <component>
                                    <name>hbm2java</name>
                                    <implementation>configuration</implementation>
                                    <outputDirectory>${generated-source-dir}</outputDirectory>
                                </component>
                            </components>
                            <componentProperties>                               
                                <configurationFile>src/main/resources/hibernate.cfg.xml</configurationFile>
                                <jdk5>true</jdk5>
                            </componentProperties>
                        </configuration>
                    </execution>

                    <execution>
                        <id>generate-schema</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>hbm2ddl</goal>
                        </goals>

                        <configuration>
                            <componentProperties>
                                <outputfilename>schema.ddl</outputfilename>
                                <drop>true</drop>
                                <ejb3>false</ejb3>
                            </componentProperties>
                        </configuration>

                    </execution>

                </executions>               
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.17</version>
                    </dependency>
                    <dependency>
                        <groupId>cglib</groupId>
                        <artifactId>cglib-nodep</artifactId>
                        <version>2.2.2</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>


    <dependencies>

    ...

    </dependencies>

    <properties>
        <generated-source-dir>generated-sources/hibernate3</generated-source-dir>
        <generated-resource-dir>generated-resources/hibernate3</generated-resource-dir>
    </properties>

</project>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮萍、无处依 2024-12-14 16:20:10

您父级的插件不依赖于 mysql-connector-java,而子级却有。我建议将此依赖项添加(甚至移动)到父级的插件中。

Your parent's plugin doesn't have dependencies to mysql-connector-java, which child has. I suggest to add (or even move) this dependencies to parent's plugin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文