Liquibase Maven Plugin

Liquibase provides a plugin for Maven which is located in the central Maven repository. In the below code, for simplicity, for now are passing the database connection string and credentials in pom.xml file.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.wesome.liquibase</groupId>
    <artifactId>liquibase</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>liquibase</name>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>17</maven.compiler.release>
    </properties>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>4.24.0</version>
                    <configuration>
                        <changeLogFile>changelog.mysql.xml</changeLogFile>
                        <url>jdbc:mysql://localhost:3306/appledb?createDatabaseIfNotExist=true</url>
                        <username>root</username>
                        <password>rootroot</password>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>8.0.16</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

changelog.mysql.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
	<changeSet author="wesome" id="1692446190573-1">
		<createTable catalogName="appledb" tableName="apple">
			<column autoIncrement="true" name="apple_id" type="BIGINT">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="apple_name" type="VARCHAR(255)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
		<rollback>
			<dropTable tableName="apple" />
		</rollback>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Fuji" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
		<rollback>
			<delete tableName="apple">
				<where>
					apple_name = 'Fuji'
				</where>
			</delete>
		</rollback>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-3">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Gala" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
		<rollback>
			<delete tableName="apple">
				<where>
					apple_name = 'Gala'
				</where>
			</delete>
		</rollback>
	</changeSet>
</databaseChangeLog>

run command

mvn liquibase:update

The apple table and corresponding data will be created and inserted into the Database, the same can be verified by running SQL query

SELECT * FROM appledb.apple;

Database details into liquibase.properties file

In the above example, we passed the database connection string and credentials in the pom.xml file, which is not a good practice, in this example, we will create a liquibase.properties file that will have all database related details.

 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
    <groupId>org.wesome.liquibase</groupId>
    <artifactId>liquibase</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>liquibase</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>17</maven.compiler.release>
    </properties>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-maven-plugin</artifactId>
                    <version>4.24.0</version>
                    <configuration>  
                       <propertyFile>liquibase.properties</propertyFile>  
                   </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>8.0.16</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

changelog.mysql.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
	<changeSet author="wesome" id="1692446190573-1">
		<createTable catalogName="appledb" tableName="apple">
			<column autoIncrement="true" name="apple_id" type="BIGINT">
				<constraints nullable="false" primaryKey="true" />
			</column>
			<column name="apple_name" type="VARCHAR(255)" />
			<column defaultValueComputed="CURRENT_TIMESTAMP" name="current_date_time" type="datetime" />
		</createTable>
		<rollback>
			<dropTable tableName="apple" />
		</rollback>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-2">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Fuji" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
		<rollback>
			<delete tableName="apple">
				<where>
					apple_name = 'Fuji'
				</where>
			</delete>
		</rollback>
	</changeSet>
	<changeSet author="wesome" id="1692701200879-3">
		<insert catalogName="appledb" tableName="apple">
			<column name="apple_name" value="Gala" />
			<column name="current_date_time" valueDate="now()" />
		</insert>
		<rollback>
			<delete tableName="apple">
				<where>
					apple_name = 'Gala'
				</where>
			</delete>
		</rollback>
	</changeSet>
</databaseChangeLog>

liquibase.properties

changeLogFile: changelog.mysql.xml
url: jdbc:mysql://localhost:3306/appledb?createDatabaseIfNotExist=true&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: rootroot

run command

mvn liquibase:update

The apple table and corresponding data will be created and inserted into the Database, the same can be verified by running SQL query

SELECT * FROM appledb.apple;

follow us on