-
-
Save lifuzu/7969405 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.api.NamedDomainObjectContainer | |
import org.gradle.api.Plugin | |
import org.gradle.api.Project | |
import org.gradle.api.DefaultTask | |
import org.gradle.api.tasks.TaskAction | |
import liquibase.integration.commandline.Main | |
apply plugin: LiquibasePlugin | |
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.h2database:h2:1.3.159' | |
classpath 'org.liquibase:liquibase-core:2.0.3' | |
} | |
} | |
class ChangeLog | |
{ | |
def name | |
def file | |
def description | |
ChangeLog(String name) { | |
this.name = name | |
} | |
} | |
class Database | |
{ | |
def name | |
def url | |
def username | |
def password | |
Database(String name) { | |
this.name = name | |
} | |
} | |
class LiquibaseTask extends DefaultTask { | |
Database database | |
String command | |
def changeLogs | |
@TaskAction | |
def liquibaseAction() { | |
if(database == null) { | |
database = project.liquibase.defaultDatabase | |
} | |
if(changeLogs == null) { | |
changeLogs = project.liquibase.changelogs | |
} | |
changeLogs.each { changeLog -> | |
def args = [ | |
"--url=${database.url}", | |
"--password=${database.password}", | |
"--username=${database.username}", | |
"--changeLogFile=${changeLog.file.absolutePath}", | |
command | |
] | |
Main.main(args as String[]) | |
} | |
} | |
} | |
class LiquibaseExtension { | |
final NamedDomainObjectContainer<Database> databases | |
final NamedDomainObjectContainer<ChangeLog> changelogs | |
Database defaultDatabase | |
String context | |
LiquibaseExtension(databases, changelogs) { | |
this.databases = databases | |
this.changelogs = changelogs | |
} | |
def databases(Closure closure) { | |
databases.configure(closure) | |
} | |
def changelogs(Closure closure) { | |
println closure | |
changelogs.configure(closure) | |
println changelogs | |
} | |
} | |
class LiquibasePlugin implements Plugin<Project> { | |
void apply(Project project) { | |
// Create and install custom tasks | |
project.task('generateChangeLog', type: LiquibaseTask) { | |
group = 'Liquibase' | |
command = 'generateChangeLog' | |
} | |
project.task('changeLogSync', type: LiquibaseTask) { | |
group = 'Liquibase' | |
command = 'changeLogSync' | |
} | |
project.task('update', type: LiquibaseTask) { | |
group = 'Liquibase' | |
command = 'update' | |
} | |
// Create and install the extension object | |
project.extensions.create("liquibase", | |
LiquibaseExtension, | |
project.container(Database), | |
project.container(ChangeLog)) | |
} | |
} | |
liquibase { | |
changelogs { | |
main { | |
file = file('changelog.xml') | |
} | |
} | |
databases { | |
sandbox { | |
url = 'jdbc:h2:db/liquibase_workshop;FILE_LOCK=NO' | |
username = 'sa' | |
password = '' | |
} | |
} | |
defaultDatabase = databases.sandbox | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment