简体   繁体   English

使用Liquibase构建Gradle多项目

[英]Gradle multi-project build with liquibase

I am trying to create a build in Gradle, The project has two submodules and each submodule has a liquibase changelog.groovy The following is the project structure 我试图在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

I have written a Gradle task which calls a method in the Parent_Project_Folder 我写了一个Gradle任务,它在Parent_Project_Folder中调用一个方法

build.gradle 的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)
    }
}

The following is the Sub-Project_Folder_2's changelog.groovy 以下是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')
        }
    }
}

So the problem is that, when I run the task from the parent build.gradle, the liquibase task cannot find the sql files from the changelog.groovy because it thinks it is running in the parent folder and the changelog.groovy is referencing the path from the subproject level. 因此,问题在于,当我从父build.gradle运行任务时,liquibase任务无法从changelog.groovy中找到sql文件,因为它认为它在父文件夹中运行并且changelog.groovy正在引用路径。从子项目级别开始。

I cannot change the paths in changelog.groovy as the build should be runnable from the sub-modules without building the whole project.(as in the submodule should be able to be built w/o building the other submodule) 我无法更改changelog.groovy中的路径,因为构建应该可以在不构建整个项目的情况下从子模块运行(因为在子模块中应该可以构建而无需构建另一个子模块)

Does anyone have any suggestions on how I can work around this? 有人对我如何解决此问题有任何建议吗?

Thank you :) 谢谢 :)

I ended up solving this myself. 我最终自己解决了这个问题。

I was unable to get the inheritance of the task working as I needed. 我无法按需获取任务的继承。 So I ended up writing a couple of tasks for the sub-projects in the main 'build.gradle' file. 因此,我最终在主“ build.gradle”文件中为子项目编写了一些任务。 These tasks will perform the specific build required for the sub-projects. 这些任务将执行子项目所需的特定构建。

There is a main task that I have written which builds the whole project. 我写了一个主要任务来构建整个项目。

Not the best solution, but it solves my problem. 不是最好的解决方案,但是它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM