gpt4 book ai didi

maven - 使用两个数据库的maven的liquibase无法正常工作

转载 作者:行者123 更新时间:2023-12-04 17:26:04 25 4
gpt4 key购买 nike

这篇文章描述了如何使用liquibase从maven更新两个数据库:
liquibase using maven with two databases
但是,当我在pom.xml(包括在下面)中尝试完全相同的配置时,它不起作用。运行'mvn liquibase:update'时出现此错误:

The driver has not been specified either as a parameter or in a properties file.

将verbose设置为true运行,我得到:
[INFO] Settings----------------------------
[INFO] driver: null
[INFO] url: null
[INFO] username: null
[INFO] password: null

所以看起来配置没有被传递给liquibase。如果配置部分在外部执行中,则可以正常工作,但这会破坏具有多个执行的整个想法-您可能希望对不同的执行使用不同的配置。
  <plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-plugin</artifactId>
<version>1.9.5.0</version>

<executions>
<execution>
<phase>process-resources</phase>
<id>one</id>
<goals>
<goal>update</goal>
</goals>
<configuration>
<changeLogFile>src/main/resources/liquibase/changelog-master.xml</changeLogFile>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://127.0.0.1:3306/${database.development}</url>
<username>wifi_offload_app</username>
<password />
<dropFirst>${liquibase.dropFirst}</dropFirst>
</configuration>
</execution>
<execution>
<phase>process-resources</phase>
<id>two</id>
<goals>
<goal>update</goal>
</goals>
<configuration>
<changeLogFile>src/main/resources/liquibase/changelog-master-sbr.xml</changeLogFile>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://127.0.0.1:3307/${database.development.sbr}</url>
<username>wifi_offload_app</username>
<password />
<dropFirst>${liquibase.dropFirst}</dropFirst>
</configuration>
</execution>
</executions>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.12</version>
</dependency>
</dependencies>
</plugin>

谢谢,
哈里

尝试从Mark的评论中选择“mvn process-resources”后进行更新。这是mvn更新两个数据库的输出。
[hariharan@msg9]$ mvn process-resources
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Offload Manager Service
[INFO] task-segment: [process-resources]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 12 resources
[INFO] [liquibase:update {execution: one}]
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:mysql://127.0.0.1:3306/db1
Mar 7, 2012 4:11:47 PM liquibase.database.template.JdbcTemplate comment
INFO: Create Database Lock Table
Mar 7, 2012 4:11:47 PM liquibase.database.template.JdbcTemplate comment
INFO: Lock Database
Mar 7, 2012 4:11:47 PM liquibase.lock.LockHandler acquireLock
INFO: Successfully acquired change log lock
Mar 7, 2012 4:11:47 PM liquibase.database.template.JdbcTemplate comment
INFO: Create Database Change Log Table
Mar 7, 2012 4:11:47 PM liquibase.database.AbstractDatabase checkDatabaseChangeLogTable
INFO: Creating database history table with name: `DATABASECHANGELOG`
Mar 7, 2012 4:11:48 PM liquibase.database.AbstractDatabase getRanChangeSetList
INFO: Reading from `DATABASECHANGELOG`
Mar 7, 2012 4:11:48 PM liquibase.database.template.JdbcTemplate comment
INFO: Changeset liquibase/changelogs/1328053095_tables.xml::1328053095-1::hariharan::(MD5Sum: bf33ef8ba7352e8c0864e9457ee642b)
Mar 7, 2012 4:11:49 PM liquibase.database.template.JdbcTemplate comment
INFO: Changeset liquibase/changelogs/1328053095_tables.xml::1328053095-2::hariharan::(MD5Sum: 7cc4f84be549dbcea3589d85df3e059)
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Changeset liquibase/changelogs/1328053095_tables.xml::1328053095-3::hariharan::(MD5Sum: 59821447f5744410a1c36bccd16196f6)
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Release Database Lock
Mar 7, 2012 4:11:50 PM liquibase.lock.LockHandler releaseLock
INFO: Successfully released change log lock
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Release Database Lock
Mar 7, 2012 4:11:50 PM liquibase.lock.LockHandler releaseLock
INFO: Successfully released change log lock
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] [liquibase:update {execution: two}]
[INFO] ------------------------------------------------------------------------
[INFO] Executing on Database: jdbc:mysql://127.0.0.1:3307/db2
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Create Database Lock Table
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Lock Database
Mar 7, 2012 4:11:50 PM liquibase.lock.LockHandler acquireLock
INFO: Successfully acquired change log lock
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Create Database Change Log Table
Mar 7, 2012 4:11:50 PM liquibase.database.AbstractDatabase checkDatabaseChangeLogTable
INFO: Creating database history table with name: `DATABASECHANGELOG`
Mar 7, 2012 4:11:50 PM liquibase.database.AbstractDatabase getRanChangeSetList
INFO: Reading from `DATABASECHANGELOG`
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Changeset liquibase/changelogs-sbr/1330991147_tables.xml::1330991147-1::hariharan::(MD5Sum: 8cf69599bcf338b57d7fe1ecc1471d86)
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Changeset liquibase/changelogs-sbr/1330991147_tables.xml::1330991147-2::hariharan::(MD5Sum: cf9a9717987fda5deba07a7a943673e1)
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Release Database Lock
Mar 7, 2012 4:11:50 PM liquibase.lock.LockHandler releaseLock
INFO: Successfully released change log lock
Mar 7, 2012 4:11:50 PM liquibase.database.template.JdbcTemplate comment
INFO: Release Database Lock
Mar 7, 2012 4:11:50 PM liquibase.lock.LockHandler releaseLock
INFO: Successfully released change log lock
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6 seconds
[INFO] Finished at: Wed Mar 07 16:11:50 PST 2012
[INFO] Final Memory: 23M/180M
[INFO] ------------------------------------------------------------------------

最佳答案

您的构建是否需要一次性更新2个数据库?另一种方法是使用Maven配置文件,如下所示:

mvn process-resources
mvn -Pdb2 process-resources

项目文件
|-- pom.xml
`-- src
`-- main
`-- resources
|-- com
| `-- myspotontheweb
| `-- db
| `-- changelog
| |-- db-changelog-1.0.xml
| `-- db-changelog-master.xml
`-- liquibase.properties

我更喜欢使用Maven的generate-resources阶段来创建一个liquibase属性文件。

pom.xml

在末尾包含两个概要文件,这些概要文件包含与两个数据库关联的属性。该解决方案可扩展到任意数量的数据库环境。

另一个需要注意的事项是liquibase使用目标目录下的填充属性
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb.db</groupId>
<artifactId>liquibase-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.162</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>2.0.1</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<propertyFile>target/classes/liquibase.properties</propertyFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>db1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<liquibase.url>jdbc:h2:target/db1/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
<liquibase.driver>org.h2.Driver</liquibase.driver>
<liquibase.username>user</liquibase.username>
<liquibase.password>pass</liquibase.password>
</properties>
</profile>
<profile>
<id>db2</id>
<properties>
<liquibase.url>jdbc:h2:target/db2/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
<liquibase.driver>org.h2.Driver</liquibase.driver>
<liquibase.username>user</liquibase.username>
<liquibase.password>pass</liquibase.password>
</properties>
</profile>
</profiles>
</project>

liquibase.properties

这是模板文件,其中填充了特定环境的概要文件属性值。
# Database credentials
url=${liquibase.url}
driver=${liquibase.driver}
username=${liquibase.username}
password=${liquibase.password}

# Liquibase changelog
changeLogFile=com/myspotontheweb/db/changelog/db-changelog-master.xml

更新

另一种策略是直接运行该插件,而不是将其集成到Maven生命周期中。
mkdir target
mvn liquibase:update
mvn -Pdb2 liquibase:update

同样,您使用配置文件来控制属性设置。

pom.xml

区别在于,插件设置由配置文件中设置的属性控制。在目标目录下没有创建更多属性文件以将其发送到jar中。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb.db</groupId>
<artifactId>liquibase-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.162</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<url>${liquibase.url}</url>
<driver>${liquibase.driver}</driver>
<username>${liquibase.username}</username>
<password>${liquibase.password}</password>
<changeLogFile>src/main/resources/com/myspotontheweb/db/changelog/db-changelog-master.xml</changeLogFile>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>db1</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<liquibase.url>jdbc:h2:target/db1/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
<liquibase.driver>org.h2.Driver</liquibase.driver>
<liquibase.username>user</liquibase.username>
<liquibase.password>pass</liquibase.password>
</properties>
</profile>
<profile>
<id>db2</id>
<properties>
<liquibase.url>jdbc:h2:target/db2/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
<liquibase.driver>org.h2.Driver</liquibase.driver>
<liquibase.username>user</liquibase.username>
<liquibase.password>pass</liquibase.password>
</properties>
</profile>
</profiles>
</project>

关于maven - 使用两个数据库的maven的liquibase无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com