javaspringspring-bootmavendependencies

How is the maven version specified in Spring Boot Starters?


I've read the reference but I don't understand how versionId is set in the dependency. For example the MongoDB starter specifies

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.mongodb</groupId>
                    <artifactId>mongo-java-driver</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jcl-over-slf4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

But the version tag is missing. How is the version of mongodb-driver determined when using these starts?


Solution

  • This is related not only to starters, if you have many modules in your project they have different dependencies. As regular practice, there is a parent pom.xml with general settings.

    If you see a dependency in child maven module like below:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
    

    There is a configured dependency in parent pom like:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.5</version>
    </dependency>
    

    Usually, it's more flexible to configure versions with properties:

    <properties>
        <commons-lang3.version>3.5</commons-lang3.version>
    </properties>
    
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons-lang3.version}</version>
    </dependency>
    

    MongoDb version is defined in spring-boot-dependencies using Dependency Management mechanism.

    <properties>
        <mongodb.version>3.8.0-beta2</mongodb.version>
    <properties>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongodb-driver</artifactId>
                <version>${mongodb.version}</version>
            </dependency>
        <dependencies>
    <dependencyManagement>
    

    Dependency management is a mechanism for centralizing dependency information. When you have a set of projects that inherits a common parent it's possible to put all information about the dependency in the common POM and have simpler references to the artifacts in the child POMs.