使用liquibase构建gradle多项目构建

时间:2017-05-05 03:58:18

标签: java gradle groovy liquibase

我正在尝试在Gradle中创建一个构建, 该项目有两个子模块,每个子模块都有一个liquibase changelog.groovy 以下是项目结构

    +  Parent_Project_Folder
    |  - build.gradle
    |  + Sub-Project_Folder_1
    |  |  - build.gradle
    |  |  + src
    |  |  |  + main
    |  |  |  |  + resources
    |  |  |  |  |  + com
    |  |  |  |  |  |  + parentProject
    |  |  |  |  |  |  |  + subProject1
    |  |  |  |  |  |  |  |  + changelog.groovy
    |  + Sub-Project_Folder_2
    |  |  - build.gradle
    |  |  + src
    |  |  |  + main
    |  |  |  |  + resources
    |  |  |  |  |  + com
    |  |  |  |  |  |  + parentProject
    |  |  |  |  |  |  |  + subProject2
    |  |  |  |  |  |  |  |  + changelog.groovy

我编写了一个Gradle任务,它调用Parent_Project_Folder

中的方法

的build.gradle

def createDatabase (databaseServerUrl, dbUsername, dbPassword, projectControlDBName, changeLogFilePath) {
    def mysql = buildscript.configurations.classpath.find { it.toString().contains("mysql-connector-java") }
    URLClassLoader loader = GroovyObject.class.classLoader
    loader.addURL(file(mysql).toURL())

    println("createDatabase CurrentPath " + System.getProperty("user.dir"))

    def db = [url : "jdbc:mysql://localhost:3306/",
              user: dbUsername, password: dbPassword, driver: 'com.mysql.jdbc.Driver']
    def sql = groovy.sql.Sql.newInstance(db.url, db.user, db.password, db.driver)

    def row = sql.firstRow('SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = \'' + projectControlDBName + '\'')

    if (row == null) {
        sql.execute("CREATE DATABASE " + projectControlDBName)
        println("Database Created: " + projectControlDBName)
    } else {
        println("Database \'" + projectControlDBName + "\' already exists")
    }

    Connection connection = DriverManager.getConnection(databaseServerUrl +
            projectControlDBName + '?nullNamePatternMatchesAll=true&useSSL=false' +
            "&user=" + dbUsername +
            "&password=" + dbPassword)
    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection))

    Liquibase liquibase = new Liquibase(changeLogFilePath, new FileSystemResourceAccessor(), database)

    liquibase.update(new Contexts(), new LabelExpression())

    database.close()

    println("Database schema created: \'" + minervaControlDBName + "\'")
}


task createAllProjectDatabases {
    doLast{
        def projectControlDBName = "dbName"
        def mySqlUsername = "username"
        def mySqlPassword = "password"
        def changeLogFilePath = 'src/main/resources/com/parentProject/subProject1/changelog.groovy'
        def databaseServerUrl = 'jdbc:mysql://localhost/'

        createDatabase(databaseServerUrl, mySqlUsername, mySqlPassword, projectControlDBName, changeLogFilePath)
    }
}

以下是Sub-Project_Folder_2的 changelog.groovy

package com.parentProject.subProject1

databaseChangeLog {
    changeSet(id: '1234', author: 'name') {
        sqlFile(path: 'src/main/resources/com/parentProj/subProject1/common-schema.mysql.sql')
        sqlFile(path: 'src/main/resources/com/parentProj/subProject1/control-schema.mysql.sql')
        rollback {
            sqlFile(path: 'src/main/resources/com/parentProj/subProject1/control-schema-rollback.mysql.sql')
        }
    }
}

所以问题是,当我从父build.gradle运行任务时,liquibase任务无法从changelog.groovy找到sql文件,因为它认为它在父文件夹中运行而changelog.groovy是引用子项目级别的路径。

我无法更改changelog.groovy中的路径,因为构建应该可以在不构建整个项目的情况下从子模块运行。(因为子模块应该能够构建不构建其他子模块)

有没有人对如何解决这个问题有任何建议?

谢谢:)

0 个答案:

没有答案