5
votes

I am learning Liquibase and Spring Boot so I've created a simple project with Spring Initializr.

In the POM.xml file I've added:

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

I've specified as property file the application.properties so all the configuration of my application can happen in a single file.

When I run any liquibase-maven-plugin task from IntelliJ I get different errors, here's an example running the changeLogSync task:

[ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:3.4.1:changelogSync (default-cli) on project simpleTest: The changeLogFile must be specified

If I add the right keys in the application.properties I am able to make it work.

For example I've found that liquibase-maven-plugin will not read the spring.datasource.url property but it will only read the url property.

For this reason my application.properties will have to be something similar:

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

If I follow this pattern I'll end up having several keys with slightly different names but with the same values in my application.properties and this solution is clearly very ugly and inefficient.

What is an efficient and maintainable way to configure and use Liquibase Maven Plugin in Spring Boot?

Edit after the answer received from 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 after the edit:

[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]
3

3 Answers

5
votes

Liquibase maven plugin supports configuration injection through pom.xml.

So you can use properties-maven-plugin to include your properties from application.properties (or use yaml-properties-maven-plugin if you are using application.yml), and then inject them into the liquibase configuration:

Example:

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

Now you can inject these properties in liquibase configuration:

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

I also needed to set the logicalFilePath to ensure that the changelog path inferred by spring boot integration and the maven plugin where the same.

3
votes

application.properties settings are very fast to have an up and running application but not the best solution in terms of flexibility

My advice is to configure a datasource using @Configuration, example here

And then configure liquibase passing datasource defined above as follows

@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;
    }
}

In this case you just need liquibase-core dependency as follows

    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
    </dependency>

A simpler alternative is to configure liquibase outside the application with no maven plugin.

Download library, or install it with some package manager, and launch a command line with all settings

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

Anyway the problem you have now is because you've written this:

url=${spring.datasource.url}

I don't know where did you find this syntax but try to replicate connections url and replace with the following

url=jdbc:h2:file:./target/test

do the same for other settings

3
votes

It is a very common occurrence in many projects.

When you use multiple plug-ins/library, each expect certain properties from environment config where key names are defined in their native nomenclature.

There is no standardization for this issue.

In order to avoid providing same values to multiple properties, which is error prone, you are recommended to use references.

# Keys needed for liquibase maven plugin
url=${spring.datasource.url}

UPDATE

I noticed you are encountering exception when running liquibase maven plugin which is of course runs outside of spring context. The solution I provided earlier works within spring context, that is when you have application spun up.

For given scenario, use maven filter resource files feature. So your command will change to

mvn liquibase:generateChangeLog resources:resources

And your setup will look like below:

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

application.properties

[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@
[email protected]@

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>

Please refer to my github project for complete working solution. Look at filter.properties file where you have common properties defined, and then same is referred in application.properties file.

NOTE: Since this is a spring project, you can't use ${propertyName} for maven filter file as its reserved property placeholder syntax for spring, but then use @propertyName@. For non-spring project ${propertyName} will work out of the box.