如何在 Spring Boot 中配置 Maven Liquibase 插件?

Pit*_*tto 4 java liquibase maven spring-boot

我正在学习 Liquibase 和 Spring Boot,所以我用Spring Initializr创建了一个简单的项目。

在 POM.xml 文件中,我添加了:

    <plugin>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-maven-plugin</artifactId>
        <version>3.4.1</version>
        <configuration>
            <propertyFile>src/main/resources/application.properties</propertyFile>
        </configuration>
    </plugin>
Run Code Online (Sandbox Code Playgroud)

我已将 application.properties 指定为属性文件,因此我的应用程序的所有配置都可以在单个文件中进行。

当我从 IntelliJ 运行任何 liquibase-maven-plugin 任务时,我遇到了不同的错误,这是一个运行 changeLogSync 任务的示例:

[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.4.1:changelogSync (default-cli) on project simpleTest: The changeLogFile must be specified
Run Code Online (Sandbox Code Playgroud)

如果我在 application.properties 中添加了正确的键,我就能让它工作。

例如,我发现 liquibase-maven-plugin 不会读取spring.datasource.url属性,但它只会读取url属性。

出于这个原因,我的 application.properties 必须是类似的:

environment                         = JUnit
spring.datasource.url               = jdbc:h2:file:./target/test
spring.datasource.driver-class-name = org.h2.Driver
spring.datasource.username          = sa
spring.datasource.password          = sa
spring.liquibase.change-log         = classpath:/db/changelog/db.changelog-master.yaml
spring.h2.console.enabled           = true
spring.h2.console.path              = /h2-console


# Keys needed for liquibase maven plugin
url                                 = jdbc:h2:file:./target/test
username                            = sa
password                            = sa
Run Code Online (Sandbox Code Playgroud)

如果我遵循这种模式,我最终会在 application.properties 中得到几个名称略有不同但值相同的键,而这个解决方案显然非常丑陋且效率低下。

在 Spring Boot 中配置和使用 Liquibase Maven 插件的有效且可维护的方法是什么?

在收到 Amith Kumar 的回答后进行编辑:

environment=JUnit
spring.datasource.url=jdbc:h2:file:./target/glossary-test
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=sa
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.yaml
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
url=${spring.datasource.url}
changeLogFile=${spring.liquibase.change-log}
username=${spring.datasource.username}
password=${spring.datasource.password}
Run Code Online (Sandbox Code Playgroud)

修改后报错:

[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.4.1:dropAll (default-cli) on project test: Error setting up or running Liquibase: liquibase.exception.DatabaseException: java.lang.RuntimeException: Cannot find database driver: Driver class was not specified and could not be determined from the url (${spring.datasource.url}) -> [Help 1]
Run Code Online (Sandbox Code Playgroud)

lor*_*non 5

Liquibase maven 插件支持通过 pom.xml 进行配置注入。

因此,您可以使用properties-maven-plugin从 application.properties 中包含您的属性(如果您使用的是 application.yml,则使用yaml-properties-maven-plugin),然后将它们注入到 liquibase 配置中:

例子:

<plugin>
    <groupId>it.ozimov</groupId>
    <artifactId>yaml-properties-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
                    <execution>
                            <phase>initialize</phase>
                            <goals>
                                    <goal>read-project-properties</goal>
                            </goals>
                            <configuration>
                                    <files>
                                            <file>src/main/resources/application.yml</file>
                                    </files>
                            </configuration>
                    </execution>
     </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

现在您可以在 liquibase 配置中注入这些属性:

<plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <changeLogFile>src/main/resources/db/changelog/db.changelog-master.yaml</changeLogFile>
                <driver>${spring.datasource.driverClassName}</driver>
                <url>${spring.datasource.url}</url>
                <username>${spring.datasource.username}</username>
                <password>${spring.datasource.password}</password>
                <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                <databaseChangeLogTableName>DATABASECHANGELOG</databaseChangeLogTableName>
                <databaseChangeLogLockTableName>DATABASECHANGELOGLOCK</databaseChangeLogLockTableName>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>javax.xml.bind</groupId>
                    <artifactId>jaxb-api</artifactId>
                    <version>2.3.0</version>
                </dependency>
            </dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我还需要设置logicalFilePath以确保 spring boot 集成和 maven 插件推断的更改日志路径相同。


Val*_*oMC 4

application.properties设置启动并运行应用程序的速度非常快,但就灵活性而言并不是最佳解决方案

我的建议是使用配置数据源@Configuration,示例在这里

然后配置liquibase传递上面定义的数据源如下

@Configuration
public class LiquibaseConfigurer {

    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource oltpDataSource;

    @Bean
    @DependsOn
    public SpringLiquibase liquibase() {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setChangeLog("classpath:liquibase/liquibase-changelog.xml");
        liquibase.setDataSource(oltpDataSource);
        return liquibase;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您只需要liquibase-core如下依赖

    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

一个更简单的替代方案是在应用程序外部配置 liquibase,无需使用 Maven 插件。

下载库,或使用某些包管理器安装它,然后启动包含所有设置的命令行

liquibase --driver=org.h2.Driver \
     --classpath=/path/to/h2/driver.jar \
     --changeLogFile=/db/changelog/db.changelog-master.yaml \
     --url="jdbc:h2:file:./target/glossary-test" \
     --username=sa \
     --password=sa \
     --logLevel=debug \
     migrate
Run Code Online (Sandbox Code Playgroud)

不管怎样,你现在遇到的问题是因为你写了这个:

url=${spring.datasource.url}
Run Code Online (Sandbox Code Playgroud)

我不知道您在哪里找到此语法,但尝试复制连接 url 并替换为以下内容

url=jdbc:h2:file:./target/test
Run Code Online (Sandbox Code Playgroud)

对其他设置执行相同操作