Search code examples
javamavenmaven-bom

Create a BOM file


I have a Maven-based multi-module Java project that builds a number of jar files (libraries) for use by other applications.

There are 5 modules in this project, each creating a separate jar file. They all have the same version number. I would like the applications that use my libraries to be able to import a BOM file so that they can then import each individual dependency without having to specify the version number individually.

I've read lots of pages on the internet about how to use a BOM file, but I couldn't find out how to create one. Is there a Maven plugin or something that generates a BOM file along with the jar files when you build the project? Or is the BOM file generated in some other way? My question is how to I go about creating the BOM file for my libraries?


Solution

  • I faced the same problem I ended with the following solution:

    I've created an example project in github (link at the end) with 3 modules where you could put your code (They all of them depend on the parent pom).

    Then I added a fourth module, with no code, just to generate the BOM (abelaneiros-bom). This module only contains a pom.xml.

    Inside the POM I put all the dependencies (my 3 modules + AWS SDK) as dependencyManagent to probe I could import my module but also third party libraries.

    The rest is done with flatten-maven-plugin plugin:

    <?xml version="1.0" encoding="UTF-8"?>
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>example.parent</groupId>
            <artifactId>abelaneiros-example</artifactId>
            <version>${revision}</version>
        </parent>
    
        <groupId>example</groupId>
        <artifactId>abelaneiros-bom</artifactId>
        <packaging>pom</packaging>
    
        <dependencyManagement>
            <dependencies>
                <!-- My modules -->
                <dependency>
                    <groupId>example</groupId>
                    <artifactId>abelaneiros-common</artifactId>
                    <version>${revision}</version>
                </dependency>
                <dependency>
                    <groupId>example</groupId>
                    <artifactId>abelaneiros-secretsmanager</artifactId>
                    <version>${revision}</version>
                </dependency>
                <dependency>
                    <groupId>example</groupId>
                    <artifactId>abelaneiros-ssm</artifactId>
                    <version>${revision}</version>
                </dependency>
    
                <!-- AWS -->
                <dependency>
                    <groupId>software.amazon.awssdk</groupId>
                    <artifactId>bom</artifactId>
                    <version>${aws.java.sdk-version}</version>
                    <type>pom</type>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>flatten-maven-plugin</artifactId>
                    <version>${org.codehaus.mojo.flatten.maven.plugin-version}</version>
                    <configuration>
                        <updatePomFile>true</updatePomFile>
                        <outputDirectory>target</outputDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <id>flatten</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>flatten</goal>
                            </goals>
                            <configuration>
                                <updatePomFile>true</updatePomFile>
                                <flattenMode>bom</flattenMode>
                                <pomElements>
                                    <dependencyManagement>expand</dependencyManagement>
                                    <properties>remove</properties>
                                    <repositories>remove</repositories>
                                    <distributionManagement>remove</distributionManagement>
                                </pomElements>
                            </configuration>
                        </execution>
                        <execution>
                            <id>flatten.clean</id>
                            <phase>clean</phase>
                            <goals>
                                <goal>clean</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    You can see the whole code in: https://github.com/abelaneiros/example-maven-generate-bom