Search code examples
mavenmaven-bom

Transitive dependency from maven BOM?


I'm working on a utility library that extends the rdf4j framework importing it using its BOM module like:

<groupId>com.example</groupId>
<artifactId>rdf4j-utils</artifactId>
<version>1.0</version>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.rdf4j</groupId>
            <artifactId>rdf4j-bom</artifactId>
            <version>3.0.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <!-- core rdf4j dependencies here -->

</dependencies>

Would it be possible to make the rdf4j version specified by rdf4j-utils available to importing modules so that additional rdf4j modules may be imported from the BOM without specifying the target version? E.g.:

<dependencies>

    <dependency>
        <groupId>com.example</groupId>
        <artifactId>rdf4j-utils</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse.rdf4j</groupId>
        <artifactId>rdf4j-repository-sparql</artifactId>
        <!-- !!! implicit rdf4j version as specified by rdf4j-utils -->
    </dependency>

</dependencies>


Solution

  • What you want is exactly solved by creating a bill of materials. So you could create your own BOM which imports the rdf4j one. Then you can put both dependencies in the dependencyManagement section and define the version as a single property to create a minimal maintenance setup.

    <properties>
      <rdf4j-utils-version>1.0</rdf4j-utils-version>
    </properties>
    
    <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>org.eclipse.rdf4j</groupId>
            <artifactId>rdf4j-bom</artifactId>
            <version>3.0.1</version>
            <type>pom</type>
            <scope>import</scope>
          </dependency>
          <dependency>
            <groupId>com.example</groupId>
            <artifactId>rdf4j-utils</artifactId>
            <version>${rdf4j-utils-version}</version>
          </dependency>
          <dependency>
            <groupId>org.eclipse.rdf4j</groupId>
            <artifactId>rdf4j-repository-sparql</artifactId>
            <version>${rdf4j-utils-version}</version>
          </dependency>
        </dependencies>
    <dependencyManagement>
    

    For a more complete example of a BOM, I find the spring-cloud-dependencies one a good inspiration - because it is nice and feature complete being a BOM with a parent pom and importing numerous other BOMs.