×
☰ See All Chapters

Liquibase example using Maven

In this tutorial we will learn to create liquibase example using maven step by step. We will learn to execute liquibase from maven command.

Before creating example, you should have installed the below prerequisites:

  1. JDK and java path set up in environment variables 

  2. Data base like MySQL, Oracle etc... we have used MySQL 

  3. Liquibase installation, refer our previous tutorial to install liquibase and never miss out any steps in that. 

  4. Maven and hence support for maven commands. 

Follow the below steps to create and execute liquibase.

Step 1: Create java Project using Maven

In the command prompt execute the following maven command to generate Maven supported Java project named as “LiquibaseExampleUsingMaven”.

mvn archetype:generate -DgroupId=com.java4coding -DartifactId=LiquibaseExampleUsingMaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

liquibase-example-using-maven-0
 

This command creates a new maven Java project with the name “LiquibaseExampleUsingMaven”, with complete directory structure.

Step 2: Convert to eclipse project

To import Maven project into Eclipse IDE, in terminal, navigate inside “LiquibaseExampleUsingMaven” project (folder should has pom.xml file), and issue mvn eclipse:eclipse command.

liquibase-example-using-maven-1
 

Import converted project into Eclipse IDE: In Eclipse IDE, Choose File –> Import –> General -> Existing Projects into Workspace –> Choose your project folder location.

liquibase-example-using-maven-2
 

Step 3: Add the resources folder

We have to create a folder to place resources of a project and this folder has to be added to the classpath. To add the folder and then to add the folder to the classpath, follow the below steps:

Create resource folder inside main folder

liquibase-example-using-maven-3
 

Right click on resource folder>Build Path>Use as Source Folder

liquibase-example-using-maven-4
 

Resources folder will be added to classpath and updated eclipse project structure will be as shown below:

liquibase-example-using-maven-5
 

Step 4: Create files changelog_1.0.xml and liquibase.properties inside resources folder. In liquibase.properties file provide the information according to your database installation. We have used MySQL 8.0.

liquibase.properties

driver=com.mysql.cj.jdbc.Driver

url=jdbc:mysql://localhost:3306/liquibase

username=root

password=root

changelog_1.0.xml

<databaseChangeLog

        xmlns="https://www.liquibase.org/xml/ns/dbchangelog"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.liquibase.org/xml/ns/dbchangelog

                        https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">

 

        <changeSet id="create_student" author="manu.manjunatha">

                <createTable tableName="student">

                        <column name="id" type="int">

                                <constraints primaryKey="true" nullable="false" />

                        </column>

                        <column name="name" type="varchar(50)">

                                <constraints nullable="false" />

                        </column>

                </createTable>

        </changeSet>

 

        <changeSet id="create_employee" author="manu.manjunatha">

                <createTable tableName="employee">

                        <column name="id" type="int">

                                <constraints primaryKey="true" nullable="false" />

                        </column>

                        <column name="firstname" type="varchar(50)">

                                <constraints nullable="false" />

                        </column>

                        <column name="lastname" type="varchar(50)">

                                <constraints nullable="false" />

                        </column>

                </createTable>

        </changeSet>

 

</databaseChangeLog>

Step 5: Upadte pom.xml

pom.xml

<project xmlns="https://maven.apache.org/POM/4.0.0"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>

        <groupId>com.java4coding</groupId>

        <artifactId>LiquibaseDemo</artifactId>

        <packaging>jar</packaging>

        <version>1.0-SNAPSHOT</version>

        <name>LiquibaseDemo</name>

        <url>https://maven.apache.org</url>

        <properties>

                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        </properties>

 

        <dependencies>

                <dependency>

                        <groupId>mysql</groupId>

                        <artifactId>mysql-connector-java</artifactId>

                        <version>8.0.16</version>

                </dependency>

 

        </dependencies>

 

        <build>

                <finalName>liquibase-demo</finalName>

                <plugins>

                        <plugin>

                                <groupId>org.apache.maven.plugins</groupId>

                                <artifactId>maven-compiler-plugin</artifactId>

                                <version>3.8.1</version>

                                <configuration>

                                        <source>1.8</source>

                                        <target>1.8</target>

                                </configuration>

                        </plugin>

 

                        <plugin>

                                <groupId>org.liquibase</groupId>

                                <artifactId>liquibase-maven-plugin</artifactId>

                                <version>3.4.2</version>

                                <configuration>

                                        <propertyFile>src/main/resources/liquibase.properties</propertyFile>

                                        <changeLogFile>src/main/resources/changelog_1.0.xml</changeLogFile>

                                </configuration>

                        </plugin>

                </plugins>

        </build>

</project>

Final project directory structure:

liquibase-example-using-maven-6
 

Step 6: Execute the maven command “mvn liquibase:update” to apply database changes.

liquibase-example-using-maven-7
 

Step 7: Check the database for output

liquibase-example-using-maven-8
 

All Chapters
Author