Doing so, the only additionally configuration needed, to activat the plugin
is the following entry in the <code>plugins</code>-section of your <code>pom.xml</code>:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>create</goal>
- </goals>
- </execution>
- </executions>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>create</goal>
+ </goals>
+ </execution>
+ </executions>
+</plugin>
+```
+
<p>
This would create the configured database.
If you want it to be droped beforehand, you have to add the goal
<code>drop</code>:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>drop</goal>
- <goal>create</goal>
- </goals>
- </execution>
- </executions>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>drop</goal>
+ <goal>create</goal>
+ </goals>
+ </execution>
+ </executions>
+</plugin>
+```
+
<p>
A correspondin goal for the command <code>update</code> is missing in this
version, but we are planning to implement it in near feature.
to use.
A simple example <code>hibernate.properties</code>-file may look like this:
</p>
- <pre class="prettyprint linenums lang-properties">
+
+```
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.connection.url=jdbc:h2:file:./target/db
hibernate.connection.driver_class=org.h2.Driver
hibernate.connection.username=sa
hibernate.connection.password=
-</pre>
+```
+
<p>
But be aware, that using this configuration-approach the database-url,
that is build in the application is the same that is used while testing,
A better approach is, to specify a different url for testing like in the
following snippet:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>drop</goal>
- <goal>create</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <url><![CDATA[jdbc:mysql://localhost/test-db]]></url>
- </configuration>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>drop</goal>
+ <goal>create</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <url><![CDATA[jdbc:mysql://localhost/test-db]]></url>
+ </configuration>
+</plugin>
+```
+
<p>
Configuration properties, that are set in the <code>configuration</code>-section
of the plugin-configuration cannnot be overwritten elsewere (for details
The equivalent of the configuration from the last section would look
like this:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>drop</goal>
- <goal>create</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <driver>org.hsqldb.jdbcDriver</driver>
- <dialect>org.hibernate.dialect.HSQLDialect</dialect>
- <url><![CDATA[jdbc:hsqldb:target/db/fotos;shutdown=true]]></url>
- <username>sa</username>
- <password></password>
- </configuration>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>drop</goal>
+ <goal>create</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <driver>org.hsqldb.jdbcDriver</driver>
+ <dialect>org.hibernate.dialect.HSQLDialect</dialect>
+ <url><![CDATA[jdbc:hsqldb:target/db/fotos;shutdown=true]]></url>
+ <username>sa</username>
+ <password></password>
+ </configuration>
+</plugin>
+```
+
<p>
The parameter <strong>hibernateProperties</strong> (name of the hibernate-properties-file
to use, defaults to <strong>hibernate.properties</strong>) can only be configured through
If you need to overwrite plugin-configuration-values with
maven-properties, you can use maven-properties in the plugin-configuration:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>drop</goal>
- <goal>create</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <password>${my-password-property}</password>
- </configuration>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>drop</goal>
+ <goal>create</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <password>${my-password-property}</password>
+ </configuration>
+</plugin>
+```
enabled.
For example, by executing:
</p>
- <pre class="prettyprint linenums lang-html">
-mvn -X compile hibernate:create</pre>
+
+```
+mvn -X compile hibernate:create
+```
+
<p>
(The <code>compile</code> might be necessary, because <code>hibernate-maven-plugin</code>
has to scan the compiled classes for annotations!)
you can force it to do so, if you set the parameter <code>force</code> to
<code>true</code>:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
- <version>${project.version}</version>
- <configuration>
- <force>true</force>
- </configuration>
-</plugin></pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <configuration>
+ <force>true</force>
+ </configuration>
+</plugin>
+```
+
<p>
Or you may specify <code>-Dhibernate.schema.force=true</code> at the command line,
if you want to force hibernate-maven-plugin only once.
<code>-Dhibernate.schema.execute=false</code> or with the following
configuration:
</p>
- <pre class="prettyprint linenums lang-html">
-<configuration>
- <execute>false</execute>
-</configuration></pre>
+
+```
+<configuration>
+ <execute>false</execute>
+</configuration>
+```
+
<p>
But even when no database is to be created, hibernate always needs to know
the dialect. Hence, the plugin will still fail, if this parameter is also
Since it will not be able to see the needed dependency, it will fail with
an error-message like:
</p>
- <pre class="prettyprint">
+
+```
[INFO] Gathered hibernate-configuration (turn on debugging for details):
[INFO] hibernate.connection.username = sa
[INFO] hibernate.connection.password =
[INFO] Finished at: Mon Mar 07 19:06:54 CET 2016
[INFO] Final Memory: 11M/215M
[INFO] ------------------------------------------------------------------------
-[ERROR] Failed to execute goal de.juplo:hibernate-maven-plugin:${project.version}:drop (default) on project hibernate4-properties-test: Could not open the JDBC-connection: Unable to load class [org.h2.Driver]: Could not load requested class : org.h2.Driver -> [Help 1]
+[ERROR] Failed to execute goal de.juplo:hibernate-maven-plugin:${project.version}:drop (default) on project hibernate4-properties-test: Could not open the JDBC-connection: Unable to load class [org.h2.Driver]: Could not load requested class : org.h2.Driver -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
-[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException</pre>
+[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
+```
+
<p>
A quick workaround for this error would be, to delete the runtime-constraint
for the jdbc-driver-dependency.
A much cleaner way is, to (additionally) ad the dependency, to the
plugin-definition:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>drop</goal>
- <goal>create</goal>
- </goals>
- </execution>
- </executions>
- <dependencies>
- <dependency>
- <groupId>org.hsqldb</groupId>
- <artifactId>hsqldb</artifactId>
- <version>2.2.8</version>
- </dependency>
- </dependencies>
-</plugin></pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>drop</goal>
+ <goal>create</goal>
+ </goals>
+ </execution>
+ </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>2.2.8</version>
+ </dependency>
+ </dependencies>
+</plugin>
+```
+
<p>
This is also the best way, if you use a different jdbc-driver for
testing, than in production.
For example, the dbunit-plugin can be configured to skip its excecution, if
hibernate-maven-plugin was skipped like this:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>dbunit-maven-plugin</artifactId>
- <configuration>
- <skip>${hibernate.schema.skipped}</skip>
- </configuration>
-</plugin></pre>
+
+```
+<plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>dbunit-maven-plugin</artifactId>
+ <configuration>
+ <skip>${hibernate.schema.skipped}</skip>
+ </configuration>
+</plugin>
+```
## The database will not be recreated after a manual drop/clean
unnecessary drop-create-cycle might take a long time. The plugin will
report that like this:
</p>
- <pre class="prettyprint">
+
+```
[INFO] No modified annotated classes found and dialect unchanged.
-[INFO] Skipping schema generation!</pre>
+[INFO] Skipping schema generation!
+```
+
<p>
If one always uses <code>mvn clean</code> for cleanup, this will not happen.
Otherwise the recreation must be <a href="./force.html">forced</a>:
</p>
- <pre class="prettyprint">
-mvn hibernate:create -Dhibernate.schema.force=true</pre>
+```
+mvn hibernate:create -Dhibernate.schema.force=true
+```
## The hibernate:create goal is not executed, when tests are skipped
<code>maven.test.skip</code> is set to <code>true</code>. If you need it to be always
executed, you can configure that explicitly like this:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
...
- <configuration>
- <skip>false</skip>
- </configuration>
-</plugin></pre>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+</plugin>
+```
+
<p>
Background-information for this design-decission can be found on the extra
page <a href="./skip.html">To skip or not to skip: that is the question</a>...
or set <code>scanDependencies</code> to <code>none</code> in the configuration
of the plugin like this:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
...
- <configuration>
- <scanDependencies>none</scanDependencies>
- </configuration>
-</plugin></pre>
+ <configuration>
+ <scanDependencies>none</scanDependencies>
+ </configuration>
+</plugin>
+```
## No annotated classes found
<p>
Example configuration for two goals:
</p>
- <pre class="prettyprint linenums lang-html">
-<executions>
- <execution>
- <id>Create Drop-Schema</id>
- <phase>test-compile</phase>
- <goals>
- <goal>drop</goal>
- </goals>
- <configuration>
- <outputFile>db-schema/drop-schema.ddl</outputFile>
- </configuration>
- </execution>
- <execution>
- <id>Create Create-Schema</id>
- <phase>test-compile</phase>
- <goals>
- <goal>create</goal>
- </goals>
- <configuration>
- <outputFile>db-schema/create-schema.ddl</outputFile>
- </configuration>
- </execution>
-</executions>
-</pre>
+
+```
+<executions>
+ <execution>
+ <id>Create Drop-Schema</id>
+ <phase>test-compile</phase>
+ <goals>
+ <goal>drop</goal>
+ </goals>
+ <configuration>
+ <outputFile>db-schema/drop-schema.ddl</outputFile>
+ </configuration>
+ </execution>
+ <execution>
+ <id>Create Create-Schema</id>
+ <phase>test-compile</phase>
+ <goals>
+ <goal>create</goal>
+ </goals>
+ <configuration>
+ <outputFile>db-schema/create-schema.ddl</outputFile>
+ </configuration>
+ </execution>
+</executions>
+```
even if the tests are skipped you can configure that explicitly,
by setting the configuration-parameter <code>skip</code> to false:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
...
- <configuration>
- <skip>false</skip>
- </configuration>
-</plugin></pre>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+</plugin>
+```
+
<p>
Or, if you want the plugin to be executed by default and to be skipped
if you say so, you can bind the value of the configuration-parameter
<code>skip</code> to a custom property. For example:
</p>
- <pre class="prettyprint linenums lang-html">
-<plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate-maven-plugin</artifactId>
...
- <configuration>
- <skip>${foo.bar}</skip>
- </configuration>
-</plugin></pre>
+ <configuration>
+ <skip>${foo.bar}</skip>
+ </configuration>
+</plugin>
+```
+
<p>
This way, the plugin would be skipped, if you set the property
<code>foo.bar</code> to <code>true</code>. For example, if you specify <code>-Dfoo.bar=true</code>