Các giải pháp là viết một nhiệm vụ gradle gọi liquibase diffChangeLog
Tạo liquibase.gradle tệp trong thư mục gốc của dự án, thêm phần mở rộng liquibase-hibernate và viết tác vụ gradle gọi liquibase diffChangeLog lệnh.
configurations {
liquibase
}
dependencies {
liquibase group: 'org.liquibase.ext', name: 'liquibase-hibernate4', version: 3.5
}
//loading properties file.
Properties liquibaseProps = new Properties()
liquibaseProps.load(new FileInputStream("src/main/resources/liquibase-task.properties"))
Properties applicationProps = new Properties()
applicationProps.load(new FileInputStream("src/main/resources/application.properties"))
task liquibaseDiffChangelog(type: JavaExec) {
group = "liquibase"
classpath sourceSets.main.runtimeClasspath
classpath configurations.liquibase
main = "liquibase.integration.commandline.Main"
args "--changeLogFile=" + liquibaseProps.getProperty('liquibase.changelog.path')+ buildTimestamp() +"_changelog.xml"
args "--referenceUrl=hibernate:spring:" + liquibaseProps.getProperty('liquibase.domain.package') + "?dialect=" + applicationProps.getProperty('spring.jpa.properties.hibernate.dialect')
args "--username=" + applicationProps.getProperty('spring.datasource.username')
args "--password=" + applicationProps.getProperty('spring.datasource.password')
args "--url=" + applicationProps.getProperty('spring.datasource.url')
args "--driver=com.mysql.jdbc.Driver"
args "diffChangeLog"
}
def buildTimestamp() {
def date = new Date()
def formattedDate = date.format('yyyyMMddHHmmss')
return formattedDate
}
LƯU Ý:Tôi đã sử dụng các tệp thuộc tính để truyền các đối số cho lệnh liquibase, bạn có thể thêm các giá trị trực tiếp, nhưng đó không phải là một phương pháp hay.
Tiếp theo, bạn cần áp dụng liquibase.gradle từ bên trong build.gradle của dự án tập tin. và thêm phần phụ thuộc liquibase
apply from: 'liquibase.gradle'
//code omitted
dependencies {
compile (group: 'org.liquibase', name: 'liquibase-core', version: "3.4.2")
}
Sau bước này, liquibase sẽ được thiết lập hoàn chỉnh.