Introduction
You can invoke dbmigrate with the maven-exec-plugin from within a pom.xml This can be an alternative for the maven-sql-plugin to setup a new database (-Pdevelop-db) or to execute some scripts (-Preset-db) with maven. The example shows the invocation bound to a maven lifecycle phase (process-test-resources).
Details
Example pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.viaboxx.dbmigrate</groupId>
<artifactId>example</artifactId>
<packaging>jar</packaging>
<name>Integrate dbmigrate into a maven pom</name>
<!--
you can pass the property to dbmigrate to
determine the db_version after a new setup of the database
-->
<properties>
<database-version>1.0.1</database-version>
</properties>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.4-702.jdbc4</version>
</dependency>
<dependency>
<groupId>de.viaboxx</groupId>
<artifactId>dbmigrate</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
<profiles>
<profile>
<!--
The Profile "reset-db" is used on continues-integration builds
to:
- drop the schema and create all tables to have a clean and up-to-date
database for integration tests.
It demonstrates one possible integration of dbmigrate with the "java" goal
-->
<id>reset-db</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<systemProperties>
<systemProperty>
<key>to-version</key>
<value>${database-version}</value>
</systemProperty>
</systemProperties> <mainClass>com.agimatec.dbmigrate.AutoMigrationTool</mainClass>
<arguments>
<argument>-conf</argument>
<argument>etc/dbmigrate/db-reset.xml</argument>
<argument>-exit</argument>
<argument>false</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<!--
The Profile "develop-db" is used when a developer wants to
- create the database/role/user/schema the first time or after a schema-change
It demonstrates one possible integration of dbmigrate with the "exec" goal and another configuration.
-->
<id>develop-db</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<systemProperties>
<systemProperty>
<key>to-version</key>
<value>${database-version}</value>
</systemProperty>
</systemProperties>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>com.agimatec.dbmigrate.AutoMigrationTool</argument>
<argument>-conf</argument>
<argument>etc/dbmigrate/db-setup.xml</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
- the to-version system property will be used by dbmigrate to set the version in the database after the scripts have been executed
Example for db-setup.xml configuration file
<?xml version="1.0" encoding="UTF-8"?>
<config name="migration">
<!-- Optional: environment variables, can be used inside scripts as ${DB_USER}.
Predefined keys:
DB_USER : if exists, overrule user from JdbcConfig
DB_PASSWORD : if exists, overrule password from JdbcConfig
DB_SCHEMA : if exists, overrule schema name of jdbcUrl from JdbcConfig
DB_URL : if exists, overrule jdbcUrl from JdbcConfig
DB_DRIVER : if exists, overrule jdbcDriver from JdbcConfig
-->
<map name="env">
<String name="DB_USER" value="postgres"/>
<String name="DB_PASSWORD" value="syspassword"/>
<String name="DB_NAME" value="mydatabase"/>
<String name="DB_USER_MYDB" value="mydb"/>
<String name="DB_PASSWORD_MYDB" value="mypassword"/>
<String name="DB_DRIVER" value="org.postgresql.Driver"/>
<String name="DB_URL" value="jdbc:postgresql://localhost:5432/postgres"/>
</map>
<!-- Scripts = the source path for scripts to scan for -->
<file name="Scripts-Before-All" dir="cp://database/setup" file=""/>
<file name="Scripts-After-All" dir="cp://dbmigrate/verify" file=""/>
<map name="version-meta">
<Boolean name="auto-version" value="false"/>
</map>
</config>
example for the setup script 0.0.1_setup.sql in resource directory "database/setup" to create a new database and setup its schema:
-- SQL SCRIPT FOR dbmigrate TO CREATE DATABASE AND SETUP ROLES, USERS, SCHEMA
SET FAIL_ON_ERROR=false;
@../create-role.sql;
CONNECT ${DB_USER_MYDB}/${DB_PASSWORD_MYDB};
@../create-database.sql;
CONNECT ${DB_USER_MYDB}/${DB_PASSWORD_MYDB}@${DB_NAME};
@../drop-schema.sql;
SET FAIL_ON_ERROR=true;
@../schema.sql;
-- #version(${to-version});
- create-role.sql, create-database.sql, drop-schema.sql and schema.sql
are standard sql scripts (manually written or created by hibernate/any persistence framework) to create the user, database and tables. In this example the scripts are located in the parent directory "database"