Maven "Bill Of Materials" Dependency 的使用

问题

使用Maven时,可能会意外混合不同版本的Spring JAR。例如,您可能会发现第三方库或另一个Spring项目将旧版本的传递依赖性拉入其中。如果您忘记自己明确声明直接依赖,则可能会出现各种意外问题。

解决方法

可以使用BOM,BOM是由Maven提供的功能,用以统一间接或者直接依赖的类库版本,强制某个类库使用某一个统一的版本。

使用示例

在项目的pom文件里,添加下面的这段代码:

 <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

这样就可以确保所有Spring依赖项(直接和传递)都在同一版本。而且还有一点就是,我们再添加spring的依赖项,就可以不用写版本号了。
比如我写的一个例子:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>     这个原来是要写版本号的。
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
发布了314 篇原创文章 · 获赞 113 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/dream_follower/article/details/100799745