Maven profiles : filtering for hsqldb and MySQL
Many developers like to use a lightweight embedded database such as Hsqldb or Derby in their development environment. But usually the application is deployed using another database such as MySQL – or Oracle, DB2 etc. Maven profiles are a way to segregate resources for each environment and to switch easily between them.
The Spring Framework allows you to put environment specific settings in Java properties files. You can read them in to your application context with a single line. First for convenience add the p namespace to your config file.
1 |
<context:property-placeholder location="classpath:testdb.properties" /> |
The testdb.properties file can contain actual values for each key. Instead we are going to use placeholders that will be expanded by maven.
testdb.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# this is filtered by maven jdbc.driverClassName=${jdbc.driverClassName} jdbc.url=${jdbc.url} jdbc.username=${jdbc.username} jdbc.password=${jdbc.password} jpaVendorAdapter.showSql=${jpaVendorAdapter.showSql} jpaVendorAdapter.generateDdl=${jpaVendorAdapter.generateDdl} jpaVendorAdapter.database=${jpaVendorAdapter.database} jpaVendorAdapter.databasePlatform=${jpaVendorAdapter.databasePlatform} # set in spring context jpaProperties instead of persistence.xml hibernate.dialect=${hibernate.dialect} hibernate.show_sql=${hibernate.show_sql} hibernate.format_sql=${hibernate.format_sql} hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size} hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} persistence.xml=${persistence.xml} |
Here is another properties file with actual values. Maven will filter each of the placeholders in testdb.propeties with the value for each matching key in this file. The maven profile in use triggers this.
hibernatejpa-mysql.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/composerdb jdbc.username=root jdbc.password=penguin69 jpaVendorAdapter.showSql=true jpaVendorAdapter.generateDdl=true jpaVendorAdapter.database=MYSQL jpaVendorAdapter.databasePlatform=org.hibernate.dialect.MySQL5InnoDBDialect # set in spring context jpaProperties instead of persistence.xml hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.jdbc.batch_size=100 hibernate.hbm2ddl.auto=update persistence.xml=META-INF/test-hibernate-persistence.xml |
In your pom.xml you define your profiles. You set up environment specific dependencies within your profile. Then you can specify which resource folders are filtered, those that are not and what file contains the actual values.
Here is a profile for MySQL that enables the filtering.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<profile> <id>hibernatejpa-mysql</id> <activation> <activeByDefault>true</activeByDefault> </activation> <dependencies> <dependency> <groupId>org.hibernate.java-persistence</groupId> <artifactId>jpa-api</artifactId> <version>2.0-cr-1</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>3.5.0-Beta-2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.5.2</version> </dependency> <dependency> <groupId>jboss</groupId> <artifactId>jboss-archive-browsing</artifactId> <version>5.0.0alpha-200607201-119</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/hibernate</directory> </resource> <resource> <directory>src/test/hibernate</directory> <filtering>false</filtering> <includes> <include>**/**/test-context.xml</include> </includes> <targetPath>${project.build.directory}/test-classes</targetPath> </resource> <resource> <directory>src/test/resources/</directory> <filtering>true</filtering> <includes> <include>**/*.properties</include> </includes> <excludes> <exclude>**/*.sql</exclude> </excludes> <targetPath>${project.build.directory}/test-classes</targetPath> </resource> </resources> <!-- project.build.directory is ${basedir}/target --> <filters> <filter>${basedir}/src/main/filters/hibernatejpa-mysql.properties</filter> </filters> </build> </profile> |
Resources
The javadoc for LocalContainerEntityManagerFactoryBean
Resource Filtering in Maven