Java 从属性文件加载数据以供 Maven 构建中的 Liquibase 使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22355725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading data from properties file to be used by Liquibase in Maven build
提问by Taoufik Mohdit
I can run Liquibase changelog through maven build (liquibase:update
goal) without any problems. Now I'd like Liquibase to use database credentials and URL loaded from a properties file (db.properties) depending on the selected Maven profile:
我可以通过 Maven 构建(liquibase:update
目标)运行 Liquibase 变更日志,没有任何问题。现在,我希望 Liquibase 使用从属性文件 ( db.properties)加载的数据库凭据和 URL,具体取决于所选的 Maven 配置文件:
|-- pom.xml
`-- src
`-- main
`-- resources
|-- local
| `-- db.properties
|-- dev
| `-- db.properties
|-- prod
| `-- db.properties
`-- db-changelog-master.xml
`-- db-changelog-1.0.xml
Each of the 3 properties files would look like the following:
3 个属性文件中的每一个都如下所示:
database.driver = oracle.jdbc.driver.OracleDriver
database.url = jdbc:oracle:thin:@<host_name>:<port_number>/instance
database.username = user
database.password = password123
Now, instead of these properties being defined in the POM file itself (as explained in the accepted answer of this question liquibase using maven with two databases does not work), I'd like them to be loaded from the external properties file. I have tried different approaches to no avail:
现在,而不是在 POM 文件本身中定义这些属性(如这个问题liquibase的接受答案中所述,使用带有两个数据库的 maven 不起作用),我希望它们从外部属性文件中加载。我尝试了不同的方法无济于事:
1. I used Maven's resource
element in the POM file:
1.我resource
在POM文件中使用了Maven的元素:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<changeLogFile>db.changelog-master.xml</changeLogFile>
<verbose>true</verbose>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources/local</directory>
</resource>
</resources>
</build>
<properties>
<liquibase.url>${database.url}</liquibase.url>
<liquibase.driver>${database.driver}</liquibase.driver>
<liquibase.username>${database.username}</liquibase.username>
<liquibase.password>${database.password}</liquibase.password>
</properties>
</profile>
<profile>
<id>dev</id>
<build>
<resources>
<resource>
<directory>src/main/resources/dev</directory>
</resource>
</resources>
</build>
<properties>
<liquibase.url>${database.url}</liquibase.url>
<liquibase.driver>${database.driver}</liquibase.driver>
<liquibase.username>${database.username}</liquibase.username>
<liquibase.password>${database.password}</liquibase.password>
</properties>
</profile>
</profiles>
2. I tried using the Properties Maven plugin:
2.我尝试使用Properties Maven插件:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/main/resources/local/db.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
When I run the liquibase:update maven goal, I get this this error:
当我运行 liquibase:update maven 目标时,我收到此错误:
[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.1.0:update (default-cli) on project my-project: The driver has not been specified either as a parameter or in a properties file
It seems that the database properties referred to in the POM file couldn't be resolved.
似乎无法解析 POM 文件中引用的数据库属性。
Any idea how this can be achieved ?
知道如何实现吗?
采纳答案by Taoufik Mohdit
I managed to get this working. The key was to use the maven filterelement in conjunction with the resourceelement as explained in Liquibase Documentation.
我设法让这个工作。关键是将 maven过滤器元素与资源元素结合使用,如Liquibase 文档中所述。
Also it's important to include the resourcesgoal in the maven command:
在 maven 命令中包含资源目标也很重要:
mvn resources:resources liquibase:update -Plocal
This is the file hierarchy I used:
这是我使用的文件层次结构:
|-- pom.xml
`-- src
`-- main
|-- resources
| `-- liquibase.properties
| |-- changelog
| `-- db-changelog-master.xml
| `-- db-changelog-1.0.xml
|-- filters
|-- local
| `-- db.properties
|-- dev
| `-- db.properties
The db.properties file would look like the following:
db.properties 文件如下所示:
database.driver = oracle.jdbc.driver.OracleDriver
database.url = jdbc:oracle:thin:@<host_name>:<port_number>/instance
database.username = user
database.password = password123
The liquibase.properties file would look like the following:
liquibase.properties 文件如下所示:
changeLogFile: changelog/db.changelog-master.xml
driver: ${database.driver}
url: ${database.url}
username: ${database.username}
password: ${database.password}
verbose: true
The POM file would look like the following:
POM 文件如下所示:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<propertyFile>target/classes/liquibase.properties</propertyFile>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<filters>
<filter>src/main/filters/local/db.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
<profile>
<id>dev</id>
<build>
<filters>
<filter>src/main/filters/dev/db.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</profile>
</profiles>