2017-05-05 135 views
0

我試圖創建搖籃構建, 該項目有兩個子模塊和每個子模塊具有liquibase changelog.groovy 以下是項目結構搖籃多項目建立與liquibase

+ 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 

我寫了一個搖籃的任務,在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) 
    } 
} 

的FOLL調用一個方法虧欠的是子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作爲構建應該從子模塊可運行無需構建整個項目。(如輔助模塊應該能夠建立W/O建設等子模塊)

有沒有人對我如何解決這個問題有任何建議?

謝謝:)

回答

0

我最終自己解決了這個問題。

我無法得到我需要的任務繼承。 所以我最終爲主'build.gradle'文件中的子項目寫了幾個任務。這些任務將執行子項目所需的特定構建。

我寫的主要任務是構建整個項目。

不是最好的解決方案,但它解決了我的問題。