Maven中dependencyManagement的一点点说明

maven中dependencyManagement的使用无非就是统一控制依赖的版本,但是在一些情况下modules自身指定的版本是无效的
例如:


//parent 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>love.llnngroupId>
    <artifactId>mavendepartifactId>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>webmodule>
        <module>servicemodule>
    modules>
    <packaging>pompackaging>

    <name>mavendepname>

    <dependencyManagement>
        
        <dependencies>
            <dependency>
                <groupId>org.mybatisgroupId>
                <artifactId>mybatisartifactId>
                <version>3.1.0version>
            dependency>
        dependencies>
        
    dependencyManagement>
project>

//web 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mavendepartifactId>
        <groupId>love.llnngroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>webartifactId>
    <packaging>warpackaging>
    <name>webname>

    <dependencies>
        <dependency>
            <groupId>love.llnngroupId>
            <artifactId>serviceartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>
    dependencies>
project>


//service 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mavendepartifactId>
        <groupId>love.llnngroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>serviceartifactId>
    <packaging>jarpackaging>

    <name>servicename>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.0version>
        dependency>
    dependencies>
project>

如下是结构图
Maven中dependencyManagement的一点点说明_第1张图片

如果存在“片段1”,web打包之后mybatis版本是3.1.0
执行

mvn dependency:tree

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ web ---
[INFO] love.llnn:web:war:1.0-SNAPSHOT
[INFO] \- love.llnn:service:jar:1.0-SNAPSHOT:compile
[INFO]    \- org.mybatis:mybatis:jar:3.1.0:compile (version managed from 3.4.0)

原因是:mybais对于web项目是间接依赖,间接依赖时dependencyManagement优先级更高!!!!!

解决方法是:所有的版本依赖都在parent项目中控制

如果根pom还有parent时,间接依赖时dependencyManagement的优先级别高!!!!,此时的解决方法修改parent的parent pomxml,或者在parent中把它的parent重写掉

你可能感兴趣的:(maven,工具使用)