我在Windows 7上使用Gradle 2.7。我想在运行
时运行我的Liquibase插件gradle build
所以我有下面的代码在我的构建。gradle文件
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile (
'commons-beanutils:commons-beanutils:1.8.3',
'commons-collections:commons-collections:3.2.1',
'org.apache.commons:commons-dbcp2:2.1.1',
'commons-lang:commons-lang:2.6',
'commons-logging:commons-logging:1.2',
'org.apache.commons:commons-pool2:2.4.2',
'org.directwebremoting:dwr:3.0.0-RELEASE',
'org.apache.httpcomponents:httpclient:4.5.1',
'org.codehaus.jackson:jackson-core-asl:1.9.13',
'org.codehaus.jackson:jackson-mapper-asl:1.9.13',
'net.sf.json-lib:json-lib:2.4:jdk15',
'jstl:jstl:1.2',
'log4j:log4j:1.2.15',
'joda-time:joda-time:2.8.2',
'org.mybatis:mybatis:3.3.0',
'org.mybatis:mybatis-spring:1.2.3',
'mysql:mysql-connector-java:5.1.36',
'org.springframework:spring-aop:4.2.1.RELEASE',
'org.springframework:spring-aspects:4.2.1.RELEASE',
'org.springframework:spring-beans:4.2.1.RELEASE',
'org.springframework:spring-context:4.2.1.RELEASE',
'org.springframework:spring-context-support:4.2.1.RELEASE',
'org.springframework:spring-core:4.2.1.RELEASE',
'org.springframework:spring-expression:4.2.1.RELEASE',
'org.springframework:spring-instrument:4.2.1.RELEASE',
'org.springframework:spring-instrument-tomcat:4.2.1.RELEASE',
'org.springframework:spring-jdbc:4.2.1.RELEASE',
'org.springframework:spring-jms:4.2.1.RELEASE',
'org.springframework:spring-messaging:4.2.1.RELEASE',
'org.springframework:spring-test:4.2.1.RELEASE',
'org.springframework:spring-tx:4.2.1.RELEASE',
'org.springframework:spring-web:4.2.1.RELEASE',
'org.springframework:spring-webmvc:4.2.1.RELEASE'
)
...
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/my_db'
username 'username'
password 'poassword'
}
runList = "main"
}
}
testClasses.dependsOn update
但是我得到以下错误
For more information, use the --logLevel flag
:update FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':update'.
> liquibase.exception.LiquibaseException: Unexpected error running Liquibase: java.lang.RuntimeException: Cannot find database driver: com.mysql.jdbc.Driver
考虑到驱动程序在我的类路径中(参见上面的"'mysql:mysql-connector-java:5.1.36'"在依赖列表中),它需要去哪里才能让Liquibase识别它?
编辑:根据建议,我尝试了下面的
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
但是得到以下错误…
$ gradle build
FAILURE: Build failed with an exception.
* Where:
Build file 'C:UsersmyuserDropboxcb_workspacexxmyprojectbuild.gradle' line: 126
* What went wrong:
Could not compile build file 'C:UsersmyuserDropboxcb_workspacexxmyprojectbuild.gradle'.
> startup failed:
build file 'C:UsersmyuserDropboxcb_workspacexxmyprojectbuild.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
查看liquibase插件源代码,看起来您需要将依赖项添加到buildscript
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'mysql:mysql-connector-java:5.1.36'
}
}