Предположим, ваш проект имеет следующую структуру:
.
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── stackoverflow
│ │ └── Foo.java
│ └── resources
│ └── META-INF
│ └── persistence.xml
└── test
└── java
Что persistence.xml
содержит следующее:
<persistence>
<persistence-unit name="MyPu" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.stackoverflow.Foo</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:mem:Q4029456-1.0-SNAPSHOT"/>
<property name="hibernate.connection.user" value="APP"/>
<property name="hibernate.connection.password" value="APP"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
</properties>
</persistence-unit>
</persistence>
Тогда следующая конфигурация экспортирует схему как часть сборки:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q4029456</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.2.144</version>
</dependency>
...
</dependencies>
<repositories>
<repository>
<id>repository.jboss.org-public</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>jpaconfiguration</implementation>
</component>
</components>
<componentProperties>
<drop>true</drop>
<!-- optional, but useful for later inspection -->
<outputfilename>schema.ddl</outputfilename>
<persistenceunit>MyPu</persistenceunit>
</componentProperties>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.2.144</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Замените jpaconfiguration
и persitence.xml
на annotationconfiguration
и src/main/resources/hibernate.cfg.xml
, если вы не используете JPA.
Ниже выписка из полученной продукции:
$ mvn process-classes
[INFO] Scanning for projects...
...
[INFO] --- hibernate3-maven-plugin:2.2:hbm2ddl (default) @ Q4029456 ---
...
drop table Foo if exists;
create table Foo (id bigint generated by default as identity, name varchar(255), primary key (id));
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...