Configuration Examples
Configuration Through A Configuration-File
The most simple way to configure the plugin is, to put all the hibernate-configuration in a hibernate.properties- or a hibernate.cfg.xml-file on your classpath or in the persistence.xml-file of your JPA-configuration, just like you would do, if you are not using the hibernate-maven-plugin.
Doing so, the only additionally configuration needed, to activat the plugin is the following entry in the plugins-section of your pom.xml:
<plugin> <groupId>de.juplo</groupId> <artifactId>hibernate-maven-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <goals> <goal>create</goal> </goals> </execution> </executions> </plugin>
This would create the configured database. If you want it to be droped beforehand, you have to add the goal drop:
<plugin> <groupId>de.juplo</groupId> <artifactId>hibernate-maven-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <goals> <goal>drop</goal> <goal>create</goal> </goals> </execution> </executions> </plugin>
A correspondin goal for the command update is missing in this version, but we are planning to implement it in near feature.
In order to let this configuration work, your configuration file must contain a complete valid configuration for the database, that you want to use. A simple example hibernate.properties-file may look like this:
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=
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, where the database is droped and recreated by the plugin. Because of that, you should never fire up this configuration on your production system, or your database might be erased!
A better approach is, to specify a different url for testing like in the following snippet:
<plugin> <groupId>de.juplo</groupId> <artifactId>hibernate-maven-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <goals> <goal>drop</goal> <goal>create</goal> </goals> </execution> </executions> <configuration> <url><![CDATA[jdbc:mysql://localhost/test-db]]></url> </configuration> </plugin>
Configuration properties, that are set in the configuration-section of the plugin-configuration cannnot be overwritten elsewere (for details see Configuration-Method-Precedence). You never can overwrite them by accident when specifying a property on the commandline or in your settings.xml.
Configuration through maven-properties
Alternatively, it is possible, to configure the plugin via maven-properties. Each relevant configuration-option has a corresponding maven-property (for a full list see the Documentation of the goal create). These are named after the Hibernate JDBC Properties:
- hibernate.connection.driver_class
- hibernate.dialect
- hibernate.connection.url
- hibernate.connection.username
- hibernate.connection.password
So, instead of writing the hibernate-configuration in the properties-file, like above, you could put it in the properties-section of your pom.xml.
Thogether with the plugin-definition from above, the following would be a complete configuration (again, the database-url was overwritten in the plugin-configuration, to be sure to have a separate database for testing):
<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> ... <plugin> <groupId>de.juplo</groupId> <artifactId>hibernate-maven-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <goals> <goal>drop</goal> <goal>create</goal> </goals> </execution> </executions> <configuration> <url><![CDATA[jdbc:hsqldb:target/db/testdb;shutdown=true]]></url> </configuration> </plugin> <plugins>
This way, you can reuse the same properties to provide a default-configurationthe, that is build into your application, and overwrite the database-url, that is used during testing to prevent accidential drops of your production database.
Configuration through the plugin-configuration
A third way for configuring the plugin is the plugin-configuration. The relevant configuration-parameters are:
- driver
- dialect
- url
- username
- password
The equivalent of the configuration from the last section would look like this:
<plugin> <groupId>de.juplo</groupId> <artifactId>hibernate-maven-plugin</artifactId> <version>2.1.0</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>
The parameter hibernateProperties (name of the hibernate-properties-file to use, defaults to hibernate.properties) can only be configured through this approach.
For more explanations, see the Documentation of the goal create.
Configuration-Method-Precedence
The configuration is gathered in a fix order:
- hibernate.properties
- hibernate.cfg.xml
- persistence.xml
- maven-properties
- plugin-configuration
If you are in doubt about where a configuration-value comes from, run maven with the debug-output enabled: mvn -X hibernate:create and be aware, that maven-properties can be overwitten on the command-line, in your ~/.m2/settings.xml and in a profile.
The plugin-configuration comes last and overwrites everything else. That way, you can be sure, that a configuration-value, that is specified in the plugin-configuration will never be overwritten by any other configuration-method.
If you need to overwrite plugin-configuration-values with maven-properties, you can use maven-properties in the plugin-configuration:
<plugin> <groupId>de.juplo</groupId> <artifactId>hibernate-maven-plugin</artifactId> <version>2.1.0</version> <executions> <execution> <goals> <goal>drop</goal> <goal>create</goal> </goals> </execution> </executions> <configuration> <password>${my-password-property}</password> </configuration> </plugin>