How do I include a classpath in my task?

Hi, I’m using Gradle 2.7 on Windows 7 with the Liquibase 1.1.1. plugin. I’m trying to connect to a local MySQL database, but am failing because the plugin cannot find the appropriate driver. Here is my task

liquibase {
  dependencies {
        classpath("mysql:mysql-connector-java:5.1.36")
  }
  activities {
    main {
        File propsFile = new File("${project.rootDir}/src/main/resources/liquibase.properties")
      Properties properties = new Properties()
      properties.load(new FileInputStream(propsFile))
      changeLogFile 'src/main/resources/db.changelog-1.0.xml'
      url 'jdbc:mysql://localhost:3306/xxx_db'
      username 'xxx'
      password 'xxx'
    }
    runList = "main"
  }
}

testClasses.dependsOn update

This task is failing however, because the MySQL driver, mysql:mysql-connector-java:5.1.36, cannot be found. Where do I put it in my script? Evidently, it doesn’t go in the “dependencies-compile” section.

Thanks, - Dave

Declare dependencies in buildscript{} block, you can use them in build script.

In this case, add following buildscript{} block in your build script.

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("mysql:mysql-connector-java:5.1.36")
  }
}

Hi,

This didn’t work. I’m uisng Gradle 2.7 one of the later versions. Anyhoo, I did as you suggested

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath("mysql:mysql-connector-java:5.1.36")
  }
}

liquibase {
  activities {
    main {
        File propsFile = new File("${project.rootDir}/src/main/resources/liquibase.properties")
      Properties properties = new Properties()
      properties.load(new FileInputStream(propsFile))
      changeLogFile 'src/main/resources/db.changelog-1.0.xml'
      url 'jdbc:mysql://localhost:3306/xxx_db'
      username 'xxx'
      password 'xxx'
    }
    runList = "main"
  }
}

testClasses.dependsOn update

but got the below error …

$ gradle build

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\myuser\Dropbox\cb_workspace\xxmyproject\build.gradle' line: 126

* What went wrong:
Could not compile build file 'C:\Users\myuser\Dropbox\cb_workspace\xxmyproject\build.gradle'.
> startup failed:
  build file 'C:\Users\myuser\Dropbox\cb_workspace\xxmyproject\build.gradle': 126: all buildscript {} blocks must appear before any plugins {} blocks in the script

  See https://docs.gradle.org/2.7/userguide/plugins.html#sec:plugins_block for information on the plugins {} block

   @ line 126, column 1.
     buildscript {
     ^

  1 error


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.657 secs

As error message shows, all buildscript{} block must appear at the beginning of the build script. So you have to sum up buildscript{} block.

So applying liquibase plugin with mysql dependency, put the following block in the beginning of your build script.

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'org.liquibase:liquibase-gradle-plugin:1.1.0'
    classpath "mysql:mysql-connector-java:5.1.36"
  }
}
apply plugin: 'org.liquibase.gradle'