Friday 18 March 2022

Quick guide to maven BOM

BOM stands for Bill of Materials. BOM is a special kind of POM, where we define the project dependencies here.

 

For example, if you are building an enterprise application, where it has more than one microservice. To maintain uniformity of dependencies across all the microservices, we can define a BOM and all the microservices inherit the dependencies from this BOM. BOM acts as a central place to maintain these dependencies.

 

 


Let’s define a BOM project in Eclipse

Step 1: Open Eclipse.

 

File -> new -> Maven Project




Select the check box ‘Create a simple project (skip archetype selection).

 

 


Click on Next button.

 

Update following details in ‘New Maven Project’ window.

a.   Group Id as ‘com.sample.app’

b.   Artifact Id as ‘connectors-bom’

c.    Version as 1.0.0

d.   Packaging type as pom

 

 


Click on Finish button.

 

Update pom.xml by adding some dependencies.

 

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>connectors-bom</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>


        <slf4j.version>2.15.0</slf4j.version>
        <guava.version>31.0.1-jre</guava.version>
        <lombok.version>1.18.22</lombok.version>
        <tomcat-jdbc>10.0.16</tomcat-jdbc>
        <jackson.version>2.13.1</jackson.version>
        <testng.version>7.5</testng.version>

    </properties>

    <dependencyManagement>
        <dependencies>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>${guava.version}</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>${jackson.version}</version>
            </dependency>

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>

            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
                <version>${tomcat-jdbc}</version>
            </dependency>


            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>${testng.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Install connectors-bom to the local repository

Navigate to the directory where pom.xml is located and execute the command ‘mvn clean install’.

 

Let’s create projects jdbc-connector and mysql-connector that inherit the dependencies from connectors-bom

 

How can I import the bom?

Approach 1 : Define the bom as a parent to this project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>jdbc-connector</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>com.sample.app</groupId>
        <artifactId>connectors-bom</artifactId>
        <version>1.0.0</version>
    </parent>
</project>

Approach 2: By importing the bom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sample.app</groupId>
    <artifactId>mysql-connector</artifactId>
    <version>1.0.0</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.sample.app</groupId>
                <artifactId>connectors-bom</artifactId>
                <version>1.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

You can download the examples of this tutorial from this link.





Previous                                                 Next                                                 Home

No comments:

Post a Comment