配置Liquibase多数据库迁移时已有properties仍报数据库URL未指定
Liquibase Maven多数据库迁移配置报错
问题现象
执行Liquibase迁移update命令时收到报错:
The database URL has not been specified either as a parameter or in a properties file.
配置需求为通过executions标签实现多数据库连接的迁移逻辑,添加该标签之前,所有迁移功能均可正常运行,添加后出现POM无法识别加载对应properties配置文件的问题。
现有项目POM核心配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.petapilot</groupId> <artifactId>migrations</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Migrations</name> <description>Demo project for Spring Boot</description> <properties> <java.version>15</java.version> <start-class>com.petapilot.migrations.MigrationsApplication</start-class> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <version>4.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>${start-class}</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>4.3.1</version> <executions> <execution> <id>postgre-update</id> <phase>process-resources</phase> <configuration> <propertyFile>src/main/resources/liquibase_postgre.properties</propertyFile> </configuration> <goals> <goal>update</goal> </goals> </execution> <execution> <id>mysql-update</id> <phase>process-resources</phase> <configuration> <propertyFile>src/main/resources/liquibase.properties</propertyFile> </configuration> <goals> <goal>update</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
报错原因
Maven插件的配置遵循层级加载规则:
- 插件全局
<configuration>标签下定义的参数,会在任何场景下执行插件目标时被加载 - 写在
<execution>内部的<configuration>参数,只有当Maven执行到该execution绑定的生命周期阶段、匹配到对应execution的目标时,才会被加载
手动执行mvn liquibase:update命令时,插件只会读取全局配置下的参数,不会加载execution内部定义的propertyFile路径,因此无法识别配置文件内容,触发“未指定数据库URL”的报错。
当执行mvn clean package这类完整生命周期命令时,两个绑定到process-resources阶段的execution会按顺序执行,此时会加载各自内部的properties配置,理论上可以完成双库迁移。
解决方法
- 如果需要在手动执行插件目标时加载不同库的迁移配置,可将配置文件路径定义为Maven全局属性,在插件全局配置中引用:
在插件全局配置中添加<properties> <liquibase.propertyFile>src/main/resources/liquibase.properties</liquibase.propertyFile> </properties><propertyFile>${liquibase.propertyFile}</propertyFile>,手动执行时通过命令行参数指定配置文件路径即可,比如mvn liquibase:update -Dliquibase.propertyFile=src/main/resources/liquibase_postgre.properties - 如果仅需在项目构建阶段自动触发双库迁移,当前配置可直接使用,执行完整生命周期命令即可,无需单独手动调用liquibase插件目标。
内容的提问来源于stack exchange,提问作者Fabio
相关产品推荐
相关产品推荐

