0

我想用 maven 生成模式 sql 脚本。

这是我的持久性文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="mypersistance"
        transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>
            <property name="hibernate.archive.autodetection" value="class"></property>
        </properties>
        <description>Persistance descriptor</description>
        <class>test.sofiane.beans.Code</class>
    </persistence-unit>
</persistence>

休眠配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="mySessionFactory">
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/test</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.default_schema">public</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
    </session-factory>
</hibernate-configuration>

pom中的插件

<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <!-- Hibernatetool will generate everything before running tests -->
            <phase>compile</phase>
            <configuration>
                <target>
                    <echo message="Ant target, through maven-antrun-plugin, started" />
                    <property name="maven_compile_classpath" refid="maven.compile.classpath" />
                    <property name="maven_test_classpath" refid="maven.test.classpath" />
                    <path id="hibernatetool.path">
                        <pathelement path="${maven_compile_classpath}" />
                        <pathelement path="${maven_test_classpath}" />
                    </path>
                    <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"
                        classpathref="hibernatetool.path" />
                    <property name="generatedByHibernate.outputDirectory"
                        value="${project.build.directory}/generated/hibernatetool" />
                    <mkdir dir="${generatedByHibernate.outputDirectory}" />

                    <hibernatetool destdir="${generatedByHibernate.outputDirectory}">
                        <classpath>
                            <path location="${project.build.directory}/classes/test/sofiane/beans" />
                        </classpath>
                        <configuration
                            configurationfile="${project.build.directory}/classes/hibernate.cfg.xml" />
                        <hbm2ddl export="true" drop="true" create="true"
                            outputfilename="helloworld.ddl" format="true" />
                    </hibernatetool>

                    <echo message="Ant target, through maven-antrun-plugin, terminated" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

pom 工作正常并生成 helloworld.ddl 但不幸的是为空!

请问有什么想法吗?

4

1 回答 1

0

我的第一个建议是您最好使用 thehibernate3-maven-plugin而不是 the,maven-antrun-plugin因为它包含您需要的所有内容,并且比您必须使用的所有配置更易于使用(有关 的更多信息,maven-antrun-plugin请参见此处hibernate3-maven-plugin)。

然后,为了解决您的问题,我认为您可以在这篇文章和答案中找到问题的答案,因为似乎给出的所有元素都会引导您使您的配置正常工作。

并且不要忘记在编译阶段之后绑定 hibernate3-maven-plugin 的运行,例如在进程类阶段(参见生命周期参考),然后运行mvn process-classes

于 2015-05-05T09:09:59.040 回答