Gradle and dbmaintain

Introduction

dbmaintain is plugin (best for my opinion) that allowes to safely migrate dbscheme. It has excellent support for Maven and Ant but not for Gradle. But Gradle has excellent support for Ant, so we can call dbmaintain ant tasks from Gradle build script.

A small note - I prefer to not contain any libraries in my source code repository (Git currently), so dbmaintain will be loaded from the maven's central repository.

Disclaimer

This text don't cover work with dbmaintain. If you need more information about this powerful db migration tool, please visit an official site: http://www.dbmaintain.org/

Solution

In first step, we need add dbmaintain Ant task into our gradle.build, but only for build time. dbmaintain.jar shouldn't be placed into WAR.

Add a new custom configuration (you can named it as you wish, for example dbmaintain):

configurations {
	antClasspath
}

And add dbmaintain to antClasspath configuration:

dependencies {
	antClasspath "org.dbmaintain:dbmaintain:2.4" 
}

Add new task dbmaintain, that declare custom Ant tasks from dbmaintain (you can do it after dependencies block):

ant.taskdef(
   resource: "dbmaintain-anttasks.xml", 
   classpath: configurations.antClasspath.asPath
)

Now we can use dbmantain's Ant tasks. Make recreate database gradle task:

task recreateDatabase << {
	def sqlFolder = "${rootDir}/src/main/sql"
	def config = "${rootDir}/src/test/resources/dbmaintain.properties"
	ant.clearDatabase(configFile: config)	 
	ant.updateDatabase(scriptLocations: sqlFolder, configFile: config)
}

Now you can recreate database with Gradle:

gradle recreateDatabase

If you want to include this task into build lifecycle you should make processTestResource (default task from java plugin) depends on our new task:

processTestResources.dependsOn recreateDatabase
leonidv Almost 11 years ago