![](/img/trans.png)
[英]How to properly configure Liquibase in Spring Boot with Maven plugin so that a I can run diff command using latest version
[英]How can I configure Maven Liquibase plugin in 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>
我已將 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
如果我在 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
如果我遵循這種模式,我最終會在我的 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}
修改后報錯:
[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]
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>
現在您可以在 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>
我還需要設置logicalFilePath以確保 spring boot 集成和 maven 插件推斷的更改日志路徑相同。
這在很多項目中都是很常見的。
當您使用多個插件/庫時,每個插件/庫都期望來自環境配置的某些屬性,其中鍵名以其本機命名法定義。
這個問題沒有標准化。
為了避免為多個屬性提供相同的值,容易出錯,建議您使用引用。
# Keys needed for liquibase maven plugin
url=${spring.datasource.url}
我注意到您在運行 liquibase maven 插件時遇到異常,這當然是在 spring 上下文之外運行的。 我之前提供的解決方案適用於 spring 上下文,即應用程序啟動時。
對於給定的場景,使用 maven 過濾資源文件功能。 所以你的命令將更改為
mvn liquibase:generateChangeLog 資源:資源
您的設置將如下所示:
src/main/filters/filter.properties
db.url=jdbc:h2:file:./target/glossary-test
db.username=sa
db.password=sa
db.driver=org.h2.Driver
db.lb.changeLogFile=classpath:/db/changelog/db.changelog-master.yaml
應用程序屬性
spring.datasource.url=@db.url@
spring.datasource.username=@db.username@
spring.datasource.password=@db.password@
spring.datasource.driver-class-name=@db.driver@
url=@db.url@
username=@db.username@
password=@db.password@
driver=@db.driver@
changeLogFile=@db.lb.changeLogFile@
pom.xml
<build>
......
<plugins
......
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.3</version>
<configuration>
<propertyFile>target/classes/application.properties</propertyFile>
</configuration>
</plugin>
</plugins>
<filters>
<filter>src/main/filters/filter.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
請參考我的 github項目以獲得完整的工作解決方案。 查看filter.properties
文件,其中定義了通用屬性,然后在application.properties
文件中引用了相同的屬性。
注意:由於這是一個 spring 項目,因此您不能將
${propertyName}
用於 maven 過濾器文件作為其 spring 的保留屬性占位符語法,而是使用@propertyName@
。 對於非春季項目${propertyName}
將開箱即用。
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;
}
}
在這種情況下,您只需要liquibase-core
依賴項如下
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
一個更簡單的替代方法是在應用程序之外配置 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
無論如何,您現在遇到的問題是因為您已經編寫了以下內容:
url=${spring.datasource.url}
我不知道你在哪里找到這個語法但嘗試復制連接 url 並替換為以下內容
url=jdbc:h2:file:./target/test
對其他設置做同樣的事情
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.