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>hibernate4-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>export</goal>
- </goals>
- </execution>
- </executions>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>export</goal>
+ </goals>
+ </execution>
+ </executions>
+</plugin>
+```
+
<p>
But be aware, that in this case the database-url, that is
build in the application is the same that is used while testing, where
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>hibernate4-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>export</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <url><![CDATA[jdbc:mysql://localhost/test-db]]></url>
- </configuration>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>export</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 plugin-configuration, to be sure to have a separate database for
testing):
</p>
- <pre class="prettyprint linenums lang-html">
-<properties>
- <hibernate.connection.driver_class>org.hsqldb.jdbcDriver</hibernate.connection.driver_class>
- <hibernate.dialect>org.hibernate.dialect.HSQLDialect</hibernate.dialect>
- <hibernate.connection.url><![CDATA[jdbc:hsqldb:res:org.my.path.production_db]]></hibernate.connection.url>
- <hibernate.connection.username>sa</hibernate.connection.username>
- <hibernate.connection.password></hibernate.connection.password>
-</properties>
+```
+<properties>
+ <hibernate.connection.driver_class>org.hsqldb.jdbcDriver</hibernate.connection.driver_class>
+ <hibernate.dialect>org.hibernate.dialect.HSQLDialect</hibernate.dialect>
+ <hibernate.connection.url><![CDATA[jdbc:hsqldb:res:org.my.path.production_db]]></hibernate.connection.url>
+ <hibernate.connection.username>sa</hibernate.connection.username>
+ <hibernate.connection.password></hibernate.connection.password>
+</properties>
...
-<plugins>
+<plugins>
...
- <plugin>
- <groupId>de.juplo</groupId>
- <artifactId>hibernate4-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>export</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <url><![CDATA[jdbc:hsqldb:target/db/testdb;shutdown=true]]></url>
- </configuration>
- </plugin>
-
-<plugins>
- </pre>
+ <plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>export</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <url><![CDATA[jdbc:hsqldb:target/db/testdb;shutdown=true]]></url>
+ </configuration>
+ </plugin>
+
+<plugins>
+```
+
<p>
This way, you can reuse the same properties to provide a
default-configurationthe, that is build into your application, and
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>hibernate4-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>export</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <driverClassName>org.hsqldb.jdbcDriver</driverClassName>
- <hibernateDialect>org.hibernate.dialect.HSQLDialect</hibernateDialect>
- <url><![CDATA[jdbc:hsqldb:target/db/fotos;shutdown=true]]></url>
- <username>sa</username>
- <password></password>
- </configuration>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>export</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <driverClassName>org.hsqldb.jdbcDriver</driverClassName>
+ <hibernateDialect>org.hibernate.dialect.HSQLDialect</hibernateDialect>
+ <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>hibernate4-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>export</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <password>${my-password-property}</password>
- </configuration>
-</plugin>
-</pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>export</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 hibernate4:export</pre>
+
+```
+mvn -X compile hibernate4:create
+```
+
<p>
(The <code>compile</code> might be necessary, because <code>hibernate4-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>hibernate4-maven-plugin</artifactId>
- <version>${project.version}</version>
- <configuration>
- <force>true</force>
- </configuration>
-</plugin></pre>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <configuration>
+ <force>true</force>
+ </configuration>
+</plugin>
+```
+
<p>
Or you may specify <code>-Dhibernate.export.force=true</code> at the command line,
if you want to force hibernate4-maven-plugin only once.
This can be achieved with the command-line parameter
<code>-Dhibernate.export.target=SCRIPT</code> or with the following configuration:
</p>
- <pre class="prettyprint linenums lang-html">
-<configuration>
- <target>SCRIPT</target>
-</configuration></pre>
+
+```
+<configuration>
+ <target>SCRIPT</target>
+</configuration>
+```
+
<p>
But even when no database is to be created, hibernate always needs to know
the dialect. Hence, the plugin will fail if this parameter is missing!
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] Total time: 2 seconds
[INFO] Finished at: Thu Nov 29 11:31:14 CET 2012
[INFO] Final Memory: 32M/342M
-[INFO] ------------------------------------------------------------------------</pre>
+[INFO] ------------------------------------------------------------------------
+```
+
<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>hibernate4-maven-plugin</artifactId>
- <version>${project.version}</version>
- <executions>
- <execution>
- <goals>
- <goal>export</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>hibernate4-maven-plugin</artifactId>
+ <version>${project.version}</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>export</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
hibernate4-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.export.skipped}</skip>
- </configuration>
-</plugin></pre>
+
+```
+<plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>dbunit-maven-plugin</artifactId>
+ <configuration>
+ <skip>${hibernate.export.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 hibernate4:create -Dhibernate.schema.force=true</pre>
+
+```
+mvn hibernate4: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>hibernate4-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-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>hibernate4-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
...
- <configuration>
- <scanDependencies>none</scanDependencies>
- </configuration>
-</plugin></pre>
+ <configuration>
+ <scanDependencies>none</scanDependencies>
+ </configuration>
+</plugin>
+```
## No annotated classes found
export-goal, 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>hibernate4-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
...
- <configuration>
- <skip>false</skip>
- </configuration>
-</plugin></pre>
+ <configuration>
+ <skip>false</skip>
+ </configuration>
+</plugin>
+```
+
<p>
Or, if you want the export-goal 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>hibernate4-maven-plugin</artifactId>
+
+```
+<plugin>
+ <groupId>de.juplo</groupId>
+ <artifactId>hibernate4-maven-plugin</artifactId>
...
- <configuration>
- <skip>${foo.bar}</skip>
- </configuration>
-</plugin></pre>
+ <configuration>
+ <skip>${foo.bar}</skip>
+ </configuration>
+</plugin>
+```
+
<p>
This way, the export-goal 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>