【问题标题】:spring-boot fails to load liquibase change on gradle buildspring-boot 无法在 gradle build 上加载 liquibase 更改
【发布时间】:2014-07-03 14:20:35
【问题描述】:

我正在使用 Spring-boot 1.1.1.RELEASE,并且我有一个在启动时创建的 H2 数据库。当我从 IntelliJ 运行我的主类时,一切正常。当我使用“gradle build test”运行时,我的集成测试出现错误:

Caused by: java.lang.IllegalStateException: Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml] (please add changelog or check your Liquibase configuration)
    at org.springframework.util.Assert.state(Assert.java:385)
    at org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration$LiquibaseConfiguration.checkChangelogExists(LiquibaseAutoConfiguration.java:80)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)

这是我的 gradle 文件:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'jacoco'
apply plugin: 'maven'

project.ext {
    springBootVersion = '1.1.1.RELEASE'
}

buildscript {
    repositories {
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
        maven { url "http://repo.spring.io/libs-milestone" }
        maven { url "http://repo.spring.io/libs-snapshot" }
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.1.RELEASE")

    }
}

jar {
    baseName = 'my-app'
    version = '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "http://repo.spring.io/libs-milestone" }
    maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
    maven { url "http://repo.spring.io/snapshot" }
    maven { url 'http://repo.spring.io/milestone' }
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
    compile("org.springframework.boot:spring-boot:1.0.1.RELEASE")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")
    compile("org.springframework.security:spring-security-web:4.0.0.M1")
    compile("org.springframework.security:spring-security-config:4.0.0.M1")
    compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.1.RELEASE')

    compile("org.springframework:spring-orm:4.0.0.RC1")
    compile("org.hibernate:hibernate-core:4.3.4.Final")
    compile("org.hibernate:hibernate-entitymanager:4.3.4.Final")
    compile("org.hibernate:hibernate-validator")

    compile("com.h2database:h2:1.3.172")
    compile("joda-time:joda-time:2.3")
    compile("org.codehaus.groovy.modules.http-builder:http-builder:0.7.1")
    compile('org.codehaus.groovy:groovy-all:2.2.1')
    compile('org.jadira.usertype:usertype.jodatime:2.0.1')
    compile("org.liquibase:liquibase-core")

    testCompile('org.spockframework:spock-core:1.0-groovy-2.0-SNAPSHOT') {
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }

    testCompile('org.spockframework:spock-spring:1.0-groovy-2.0-SNAPSHOT') {
        exclude group: 'org.spockframework', module: 'spock-core'
        exclude group: 'org.spockframework', module: 'spring-beans'
        exclude group: 'org.spockframework', module: 'spring-test'
        exclude group: 'org.codehaus.groovy', module: 'groovy-all'
    }
    testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion")
    testCompile('org.codehaus.groovy.modules.http-builder:http-builder:0.7+')
    testCompile("junit:junit")
}

jacocoTestReport {
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

我在 src/main/resources 和 src/test/resources 中都有一个 application.properties 文件,其中包含以下条目:

liquibase.changeLog=classpath:db/changelog/db.changelog-master.xml

然后在 src/main/resources/db/changelog 和 src/test/resources/db/changelog 我都有一个 db.changelog-master.xml 当我构建项目时,我看到 build/resources/testbuild/resources/mainapplication.properties, db/changelog/db.changelog-master.xml 那里。

这似乎是一个类路径问题,因为它在 IntelliJ 内而不是命令行中工作。谁能建议我在这里可能做错了什么?

【问题讨论】:

  • 我同意这听起来像是一个类路径问题。运行“gradle build”后,build/resources/main 和 build/resources/test 中有什么?
  • 我有master和build:/build/resources/.../db/changelog/db.changelog-master.xml, build/resources/.../application.properties其他预期的文件。所以,所有东西都放在那里。
  • 我假设您根本没有自定义 Gradle 的测试任务来更改其类路径?也许您可以在某个地方分享一个说明问题的项目?
  • 很遗憾,我无法分享整个项目。而且我的 build.gradle 文件上没有任何测试特定任务。我确实用我的 gradle 文件更新了我的帖子。

标签: intellij-idea spring-boot


【解决方案1】:

我想在此处添加我的修复(即使我的构建是 Maven)。我的application.properties 在我的根目录中,但它不会按照根目录的指示找到我的db.changelog.xml 类路径。它需要类路径来自我的/resources 目录。也许我的application.properties 在一个非常规的位置,但我认为它应该放在根目录中。

我的修复

虽然我的application.properties 的实际路径是这样的:
liquibase.change-log=classpath:/src/main/resources/liquibase/db.changelog.xml

需要这条路径才能找到它:
liquibase.change-log=classpath:/liquibase/db.changelog.xml

【讨论】:

    【解决方案2】:

    我可以在测试类路径上看到 spock,所以可能它仍然没有使用正确的测试上下文加载器(我希望他们能解决这个问题)。您需要手动将加载程序或初始化程序添加到您的@ContextConfigurationDocs here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-06
      • 2021-11-30
      • 2019-03-02
      • 2022-01-16
      • 2019-05-30
      • 2020-01-29
      • 2021-12-18
      • 2021-03-11
      相关资源
      最近更新 更多