mysqlgradlespring-bootliquibasechangeset

Unable to generate difference from liquibase gradle plugin


I'm trying to implement liquibase in an existing SpringBoot project with MYSQL database. I want to be able to generate changesets which specify the differences when an entity is changed.

What I've done:

I've added liquibase dependencies and the gradle liquibase plugin in my build.gradle file. After making a domain change, I've run gradle generateChangeLog. The command executes successfully but nothing happens.

I read somewhere that this gradle plugin works only for the inmemory h2 database? Is that true? If yes then what alternative should I use to generate changelogs automatically.

I could not find a working SpringBoot gradle based example which uses MYSQL and has liquibase implemented WITH automatic change generation ability. It would be great if someone could provide that.

References:

https://github.com/stevesaliman/liquibase-workshop

https://github.com/liquibase/liquibase-gradle-plugin


Solution

  • The solutions is to write a gradle task which invokes liquibase diffChangeLog

    Create a liquibase.gradle file in the project root directory, add liquibase-hibernate extension and write a gradle task that invokes the liquibase diffChangeLog command.

    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
    }
    

    NOTE: I have used properties files to pass arguments to the liquibase command, you could add the values directly, but that would not be a good practice.

    Next, you would need to apply the liquibase.gradle file from within the project's build.gradle file. and add the liquibase dependency

    apply from: 'liquibase.gradle'
    //code omitted
    dependencies {
        compile (group: 'org.liquibase', name: 'liquibase-core', version: "3.4.2")
    }
    

    After this step liquibase would be setup completely.

    You can now use gradle liquibaseDiffChangeLog to generate changelogs.