Loading data from a properties file that will be used by Liquibase in Maven build

I can run Liquibase change log through maven build ( liquibase:update goal) without any problems. Now I would like Liquibase to use the credentials and database URLs loaded from the properties file (db.properties) depending on the Maven profile selected:

 |-- 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 three property files will look like this:

 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 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 would like them to be loaded from the external properties file. I tried different approaches to no avail:

1. I used the Maven resource element in the POM file:

 <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:

  <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 Liquibase: update the maven target, I get this error:

 [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 appears that the database properties mentioned in the POM file cannot be resolved.

Any idea how this can be achieved?

+6
source share
1 answer

I have succeeded. The key was to use the maven filter element in combination with the resource element, as described in the Liquibase Documentation .

It is also important to include the resource target in the maven command:

 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 will look like this:

 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 will look like this:

 changeLogFile: changelog/db.changelog-master.xml driver: ${database.driver} url: ${database.url} username: ${database.username} password: ${database.password} verbose: true 

The POM file will look like this:

 <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> 
+14
source

All Articles