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
+ 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
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
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.
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)
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. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With