Maven myBatis Plugin

Maven myBatis Plugin

Recently I got chance to learn maven myBatis plugin which is very handy  to handle database in different environment.Below I have tried to given some of quick tips about myBatis plugin which will help you in your project.

1.1 pom.xml Plugin configuration
To use Migration Maven plugin in your project you have to configure your pom.xml file like this:
...
<plugins>
<plugin>
<groupId>org.mybatis.maven</groupId>
<artifactId>maven-migration-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<repository> [migration repository path] </repository>
</configuration>
<dependencies>
[ add your jdbc driver depencency ]
</dependencies>
</plugin>
...
<plugins>


1.2   The migration:status goal
   This goal prints the current migration status of database. A tipical output could be:
mvn migrate:status -Dmigration.path=/path/to/migration/repository
...
[INFO] Executing Apache Migration StatusCommand
[INFO] ID Applied At Description
[INFO] ================================================================================
[INFO] 20100400000001 ...pending... create changelog
[INFO] 20100400000002 ...pending... first migration
[INFO] 20100400000003 ...pending... second migration
[INFO]
...

1.3 The migration:check goal

checks the current status of your database migration and fails if one or more script are pending. A typical use of
this goal is check the migration status into your maven build life cycle:
<plugin>
<groupId>org.mybatis.maven</groupId>
<artifactId>maven-migration-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<repository> [migration repository path] </repository>
</configuration>
<executions>
<execution>
<id>migration-chack</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency> [your jdbc dependency] </dependency>
</dependencies>
</plugin>
and then
mvn clean test
this goal fails if migration plugin founds one or more pending script. To skip the migration check set the
properties migration.skip like this:
mvn -Dmigration.skip=true clean test





1.4 Folder Structure
./drivers
Place your JDBC driver .jar or .zip files in this directory.  Upon running a migration, the drivers will be dynamically loaded.
./environments
In the environments folder you will find .properties files that represent your database instances.  By default a development.properties file is created for you to configure your development time database properties.You can also create test.properties and production.properties files. The environment can be specified when running a migration by using the --env=<environment> option (without the path or ".properties" part).
The default environment is "development".
./scripts
This directory contains your migration SQL files.  These are the files that contain your DDL to both upgrade and downgrade your database structure.  By default, the directory will contain the script to
create the changelog table, plus one empty "first" migration script. To create a new migration script, use the "new" command.  To run all pending migrations, use the "up" command.  To undo the last migration applied, use the "down" command etc.
1.5  To Create New Query
mvn migration:new -Dmigration.description=my_test_schema_migration

Configuration Required:





·         Check Repository path in pom.xml;
·         In test folder make sure we have two folded “environments” and “scripts
·         Now use mvn migration:new 
-Dmigration.description=my_newtest_schema_migration
·         To Check migration status use below given query
mvn migration:status -Dmigration.path=/path/to/repository
[INFO] Executing Apache Migration StatusCommand
[INFO] ID Applied At Description
[INFO] ================================================================================
[INFO] 20100400000001 2010-04-24 22:51:16 create changelog
[INFO] 20100400000002 2010-04-24 22:51:17 first migration
[INFO] 20100400000003 ...pending... my_newtest_schema_migration

·         Now finally we can execute mvn migration: up command to execute above created script
mvn migration:up  -Dmigration.path=/path/to/repository
mvn migration:status -Dmigration.path=/path/to/repository

[INFO] Executing Apache Migration StatusCommand
[INFO] ID Applied At Description
[INFO] ================================================================================
Examples

[INFO] 20100400000001 2010-04-24 22:51:16 create changelog
[INFO] 20100400000002 2010-04-24 22:51:17 first migration

[INFO] 20100400000003 2010-04-24 23:14:07 my_newtest_schema_migration

Skip Unit Test in Maven install lifeCycle
mvn install -Dmaven.test.skip=true


Kindly share your feedback/comment below.

Comments