一个依赖在引入的时候,是可以声明这个依赖的作用范围的。比如这个依赖只对本地起作用,比如只对测试起作用等等。作用域一共有compile,provided,system,test,import,runtime 这几个值。
简单总结一下:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
<scope>import</scope>
</dependency>
<dependencies>
<dependencyManagement>
Maven仲裁机制原则
提炼几点通俗易懂的解释如下
当在Maven项目中引入A的依赖,A的依赖通常又会引入B的jar包,B可能还会引入C的jar包。这样,当你在pom.xml文件中添加了A的依赖,Maven会自动的帮你把所有相关的依赖都添加进来。
比如,在Spring Boot项中,当引入了spring-boot-starter-web:
org.springframework.boot
spring-boot-starter-web
我们就可以理解为依赖的传递性。即当一个依赖需要另外一个依赖支撑时,Maven会帮我们把相应的依赖依次添加到项目当中。
这样的好处是,使用起来就非常方便,不用自己挨个去找依赖Jar包了。坏处是会引起Jar包冲突,我们后面会讲到。
依赖链路一:主要根据依赖的路径长短来决定引入哪个依赖(两个冲突的依赖)。
举例说明:
依赖链路一:A -> X -> Y -> Z(21.0)
依赖链路二:B -> Q -> Z(20.0)
项目中同时引入了A和B两个依赖,它们间接都引入了Z依赖,但由于B的依赖链路比较短,因此最终生效的是Z(20.0)版本。这就是最短路径优先原则。
如果两个依赖的路径一样,最短路径优先原则是无法进行判断的,此时需要使用最先声明优先原则,也就是说,谁的声明在前则优先选择。
举例说明:
依赖链路一:A -> X -> Z(21.0)
依赖链路二:B -> Q -> Z(20.0)
A和B最终都依赖Z,此时A的声明(pom中引入的顺序)优先于B,则针对冲突的Z会优先引入Z(21.0)。
多棵树构建顺序原则
这个功能在Maven的源码中是一个叫Reactor(反应堆)实现的。它主要做了一件事情就是决定一个项目中,多个子pom谁先进行build的顺序,这个出厂顺序很重要,在合并打包时,往往决定了最终谁会在多个pom之间胜出的问题。
Reactor的原则
多棵树(多个子pom)构建的顺序是按照被依赖方的要在前,依赖方在后的原则。
项目要保证这里是不能出现循环依赖的。
Reactor的原则图解
如下图子pom1 在被子pom2和子pom3同时依赖,所以子pom1最先被构建,子pom3没有人被依赖,所以最后构建。
springboot打包策略
springboot打包会打成一个Fatjar,所有的依赖都会放在BOOT-INF/lib/目录下。
springboot的打包是越靠后的构建pom越优先,是一种覆盖思想。
springboot对应的打包插件:
org.springframework.boot
spring-boot-maven-plugin
springboot对应的打包插件代码库:
https://github.com/spring-projects/spring-boot/tree/main/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin
对应的打包插件知识库:
https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/
在项目中调试的话需要在dependencies节点中引入:
org.springframework.boot
spring-boot-maven-plugin
测试工程:
https://github.com/lzc-alioo/springbootmvndemo
一次完整依赖分析(通过命令mvn dependency:tree -Dverbose可以查看仲裁说明):
~/work/gitstudy/springbootmvndemo mvn dependency:tree -Dverbose
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] springbootmvndemo [pom]
[INFO] module1 [jar]
[INFO] module2 [jar]
[INFO] start [jar]
[INFO]
[INFO] -------------------< com.example:springbootmvndemo >--------------------
[INFO] Building springbootmvndemo 0.0.1-SNAPSHOT [1/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ springbootmvndemo ---
[INFO] com.example:springbootmvndemo:pom:0.0.1-SNAPSHOT
[INFO]
[INFO] ------------------------< com.example:module1 >-------------------------
[INFO] Building module1 0.0.1-SNAPSHOT [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ module1 ---
[INFO] com.example:module1:jar:0.0.1-SNAPSHOT
[INFO] \- com.alibaba:fastjson:jar:1.2.46:compile
[INFO] +- com.squareup.retrofit2:retrofit:jar:2.1.0:provided
[INFO] | \- (com.squareup.okhttp3:okhttp:jar:4.9.3:provided - version managed from 3.3.0; omitted for duplicate)
[INFO] +- com.squareup.okhttp3:okhttp:jar:4.9.3:provided (version managed from 3.6.0)
[INFO] | +- com.squareup.okio:okio:jar:2.8.0:provided
[INFO] | | +- (org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.21:provided - version managed from 1.4.0; omitted for duplicate)
[INFO] | | \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.6.21:provided (version managed from 1.4.0)
[INFO] | \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.21:provided (version managed from 1.4.10)
[INFO] | +- (org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.6.21:provided - version managed from 1.6.21; omitted for duplicate)
[INFO] | \- org.jetbrains:annotations:jar:13.0:provided
[INFO] +- io.javaslang:javaslang:jar:2.0.6:provided
[INFO] | \- io.javaslang:javaslang-match:jar:2.0.6:provided
[INFO] \- org.glassfish.jersey.core:jersey-common:jar:2.35:provided (version managed from 2.23.2)
[INFO] +- jakarta.ws.rs:jakarta.ws.rs-api:jar:2.1.6:provided (version managed from 2.1.6)
[INFO] +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:provided (version managed from 1.3.5)
[INFO] +- org.osgi:org.osgi.core:jar:6.0.0:provided
[INFO] +- org.glassfish.hk2.external:jakarta.inject:jar:2.6.1:provided
[INFO] \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.3:provided
[INFO] +- org.osgi:osgi.core:jar:6.0.0:provided
[INFO] \- org.osgi:osgi.cmpn:jar:6.0.0:provided
[INFO]
[INFO] ------------------------< com.example:module2 >-------------------------
[INFO] Building module2 0.0.1-SNAPSHOT [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ module2 ---
[INFO] com.example:module2:jar:0.0.1-SNAPSHOT
[INFO] +- com.example:module1:jar:0.0.1-SNAPSHOT:compile
[INFO] | \- (com.alibaba:fastjson:jar:1.2.46:compile - omitted for conflict with 1.2.56)
[INFO] \- com.alibaba:fastjson:jar:1.2.56:compile
[INFO] +- com.squareup.retrofit2:retrofit:jar:2.1.0:provided
[INFO] | \- (com.squareup.okhttp3:okhttp:jar:4.9.3:provided - version managed from 3.3.0; omitted for duplicate)
[INFO] +- com.squareup.okhttp3:okhttp:jar:4.9.3:provided (version managed from 3.6.0)
[INFO] | +- com.squareup.okio:okio:jar:2.8.0:provided
[INFO] | | +- (org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.21:provided - version managed from 1.4.0; omitted for duplicate)
[INFO] | | \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.6.21:provided (version managed from 1.4.0)
[INFO] | \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.21:provided (version managed from 1.4.10)
[INFO] | +- (org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.6.21:provided - version managed from 1.6.21; omitted for duplicate)
[INFO] | \- org.jetbrains:annotations:jar:13.0:provided
[INFO] +- io.javaslang:javaslang:jar:2.0.6:provided
[INFO] | \- io.javaslang:javaslang-match:jar:2.0.6:provided
[INFO] +- org.glassfish.jersey.core:jersey-common:jar:2.35:provided (version managed from 2.23.2)
[INFO] | +- jakarta.ws.rs:jakarta.ws.rs-api:jar:2.1.6:provided (version managed from 2.1.6)
[INFO] | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:provided (version managed from 1.3.5)
[INFO] | +- org.osgi:org.osgi.core:jar:6.0.0:provided
[INFO] | +- org.glassfish.hk2.external:jakarta.inject:jar:2.6.1:provided
[INFO] | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.3:provided
[INFO] | +- org.osgi:osgi.core:jar:6.0.0:provided
[INFO] | \- org.osgi:osgi.cmpn:jar:6.0.0:provided
[INFO] \- joda-time:joda-time:jar:2.10:provided
[INFO]
[INFO] -------------------------< com.example:start >--------------------------
[INFO] Building start 0.0.1-SNAPSHOT [4/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.3.0:tree (default-cli) @ start ---
[INFO] com.example:start:jar:0.0.1-SNAPSHOT
[INFO] +- com.example:module1:jar:0.0.1-SNAPSHOT:compile
[INFO] | \- com.alibaba:fastjson:jar:1.2.46:compile
[INFO] | +- com.squareup.retrofit2:retrofit:jar:2.1.0:provided
[INFO] | | \- (com.squareup.okhttp3:okhttp:jar:4.9.3:provided - version managed from 3.3.0; omitted for duplicate)
[INFO] | +- com.squareup.okhttp3:okhttp:jar:4.9.3:provided (version managed from 3.6.0)
[INFO] | | +- com.squareup.okio:okio:jar:2.8.0:provided
[INFO] | | | +- (org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.21:provided - version managed from 1.4.0; omitted for duplicate)
[INFO] | | | \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.6.21:provided (version managed from 1.4.0)
[INFO] | | \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.21:provided (version managed from 1.4.10)
[INFO] | | +- (org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.6.21:provided - version managed from 1.6.21; omitted for duplicate)
[INFO] | | \- org.jetbrains:annotations:jar:13.0:provided
[INFO] | +- io.javaslang:javaslang:jar:2.0.6:provided
[INFO] | | \- io.javaslang:javaslang-match:jar:2.0.6:provided
[INFO] | \- org.glassfish.jersey.core:jersey-common:jar:2.35:provided (version managed from 2.23.2)
[INFO] | +- jakarta.ws.rs:jakarta.ws.rs-api:jar:2.1.6:provided (version managed from 2.1.6)
[INFO] | +- (jakarta.annotation:jakarta.annotation-api:jar:1.3.5:provided - version managed from 1.3.5; omitted for duplicate)
[INFO] | +- org.osgi:org.osgi.core:jar:6.0.0:runtime (scope not updated to runtime)
[INFO] | +- org.glassfish.hk2.external:jakarta.inject:jar:2.6.1:provided
[INFO] | \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.3:provided
[INFO] | +- org.osgi:osgi.core:jar:6.0.0:provided
[INFO] | \- org.osgi:osgi.cmpn:jar:6.0.0:provided
[INFO] +- com.example:module2:jar:0.0.1-SNAPSHOT:compile
[INFO] | +- (com.example:module1:jar:0.0.1-SNAPSHOT:compile - version managed from 0.0.1-SNAPSHOT; omitted for duplicate)
[INFO] | \- (com.alibaba:fastjson:jar:1.2.56:compile - omitted for conflict with 1.2.46)
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.7.1:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.7.1:compile (version managed from 2.7.1; scope not updated to compile)
[INFO] | | +- (org.springframework:spring-core:jar:5.3.21:compile - version managed from 5.3.21; omitted for duplicate)
[INFO] | | \- org.springframework:spring-context:jar:5.3.21:compile (version managed from 5.3.21)
[INFO] | | +- org.springframework:spring-aop:jar:5.3.21:compile (version managed from 5.3.21)
[INFO] | | | +- (org.springframework:spring-beans:jar:5.3.21:compile - version managed from 5.3.21; omitted for duplicate)
[INFO] | | | \- (org.springframework:spring-core:jar:5.3.21:compile - version managed from 5.3.21; omitted for duplicate)
[INFO] | | +- org.springframework:spring-beans:jar:5.3.21:compile (version managed from 5.3.21)
[INFO] | | | \- (org.springframework:spring-core:jar:5.3.21:compile - version managed from 5.3.21; omitted for duplicate)
[INFO] | | +- (org.springframework:spring-core:jar:5.3.21:compile - version managed from 5.3.21; omitted for duplicate)
[INFO] | | \- org.springframework:spring-expression:jar:5.3.21:compile (version managed from 5.3.21)
[INFO] | | \- (org.springframework:spring-core:jar:5.3.21:compile - version managed from 5.3.21; omitted for duplicate)
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.1:compile (version managed from 2.7.1; scope not updated to compile)
[INFO] | | \- (org.springframework.boot:spring-boot:jar:2.7.1:compile - version managed from 2.7.1; omitted for duplicate)
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.1:compile (version managed from 2.7.1)
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.11:compile (version managed from 1.2.11)
[INFO] | | | +- ch.qos.logback:logback-core:jar:1.2.11:compile (version managed from 1.2.11)
[INFO] | | | \- (org.slf4j:slf4j-api:jar:1.7.36:compile - version managed from 1.7.32; omitted for duplicate)
[INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile (version managed from 2.17.2)
[INFO] | | | +- (org.slf4j:slf4j-api:jar:1.7.36:compile - version managed from 1.7.35; omitted for duplicate)
[INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile (version managed from 2.17.2)
[INFO] | | | \- (org.osgi:org.osgi.core:jar:4.3.1:provided - omitted for conflict with 6.0.0)
[INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile (version managed from 1.7.36)
[INFO] | | \- (org.slf4j:slf4j-api:jar:1.7.36:compile - version managed from 1.7.36; omitted for duplicate)
[INFO] | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile (version managed from 1.3.5)
[INFO] | +- org.springframework:spring-core:jar:5.3.21:compile (version managed from 5.3.21; scope not updated to compile)
[INFO] | | \- org.springframework:spring-jcl:jar:5.3.21:compile (version managed from 5.3.21)
[INFO] | \- org.yaml:snakeyaml:jar:1.30:compile (version managed from 1.30)
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.7.1:runtime
[INFO] | +- (org.springframework.boot:spring-boot:jar:2.7.1:runtime - version managed from 2.7.1; omitted for duplicate)
[INFO] | \- (org.springframework.boot:spring-boot-autoconfigure:jar:2.7.1:runtime - version managed from 2.7.1; omitted for duplicate)
[INFO] +- org.projectlombok:lombok:jar:1.18.24:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.7.1:test
[INFO] | +- (org.springframework.boot:spring-boot-starter:jar:2.7.1:test - version managed from 2.7.1; omitted for duplicate)
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.7.1:test (version managed from 2.7.1)
[INFO] | | \- (org.springframework.boot:spring-boot:jar:2.7.1:test - version managed from 2.7.1; omitted for duplicate)
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.1:test (version managed from 2.7.1)
[INFO] | | +- (org.springframework.boot:spring-boot:jar:2.7.1:test - version managed from 2.7.1; omitted for duplicate)
[INFO] | | +- (org.springframework.boot:spring-boot-test:jar:2.7.1:test - version managed from 2.7.1; omitted for duplicate)
[INFO] | | \- (org.springframework.boot:spring-boot-autoconfigure:jar:2.7.1:test - version managed from 2.7.1; omitted for duplicate)
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.7.0:test (version managed from 2.7.0)
[INFO] | | +- net.minidev:json-smart:jar:2.4.8:test (version managed from 2.4.7)
[INFO] | | | \- net.minidev:accessors-smart:jar:2.4.8:test
[INFO] | | | \- org.ow2.asm:asm:jar:9.1:test
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.36:compile (version managed from 1.7.33; scope not updated to compile)
[INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:test (version managed from 2.3.3)
[INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:test (version managed from 1.2.2)
[INFO] | +- org.assertj:assertj-core:jar:3.22.0:test (version managed from 3.22.0)
[INFO] | +- org.hamcrest:hamcrest:jar:2.2:runtime (version managed from 2.2; scope not updated to runtime)
[INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test (version managed from 5.8.2)
[INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test (version managed from 5.8.2)
[INFO] | | | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | | | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test (version managed from 1.8.2)
[INFO] | | | | \- (org.apiguardian:apiguardian-api:jar:1.1.2:test - omitted for duplicate)
[INFO] | | | \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test (version managed from 5.8.2)
[INFO] | | | +- (org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test - version managed from 5.8.2; omitted for duplicate)
[INFO] | | | \- (org.apiguardian:apiguardian-api:jar:1.1.2:test - omitted for duplicate)
[INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test (version managed from 5.8.2)
[INFO] | | +- org.junit.platform:junit-platform-engine:jar:1.8.2:test (version managed from 1.8.2)
[INFO] | | | +- (org.opentest4j:opentest4j:jar:1.2.0:test - omitted for duplicate)
[INFO] | | | +- (org.junit.platform:junit-platform-commons:jar:1.8.2:test - version managed from 1.8.2; omitted for duplicate)
[INFO] | | | \- (org.apiguardian:apiguardian-api:jar:1.1.2:test - omitted for duplicate)
[INFO] | | +- (org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test - version managed from 5.8.2; omitted for duplicate)
[INFO] | | \- (org.apiguardian:apiguardian-api:jar:1.1.2:test - omitted for duplicate)
[INFO] | +- org.mockito:mockito-core:jar:4.5.1:test (version managed from 4.5.1)
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.12.11:test (version managed from 1.12.9)
[INFO] | | | +- net.java.dev.jna:jna:jar:5.8.0:runtime
[INFO] | | | +- (net.java.dev.jna:jna-platform:jar:5.8.0:test - omitted for conflict with 5.7.0)
[INFO] | | | +- com.google.code.findbugs:findbugs-annotations:jar:3.0.1:test
[INFO] | | | \- com.google.code.findbugs:jsr305:jar:3.0.2:runtime (scope not updated to runtime)
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.12.11:test (version managed from 1.12.9)
[INFO] | | | +- (net.java.dev.jna:jna:jar:5.8.0:test - omitted for duplicate)
[INFO] | | | +- (net.java.dev.jna:jna-platform:jar:5.8.0:test - omitted for conflict with 5.7.0)
[INFO] | | | +- (com.google.code.findbugs:findbugs-annotations:jar:3.0.1:test - omitted for duplicate)
[INFO] | | | \- (com.google.code.findbugs:jsr305:jar:3.0.2:test - omitted for duplicate)
[INFO] | | \- org.objenesis:objenesis:jar:3.2:test
[INFO] | +- org.mockito:mockito-junit-jupiter:jar:4.5.1:test (version managed from 4.5.1)
[INFO] | | +- (org.mockito:mockito-core:jar:4.5.1:test - version managed from 4.5.1; omitted for duplicate)
[INFO] | | \- (org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test - version managed from 5.8.2; omitted for duplicate)
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test (version managed from 1.5.0)
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- (org.springframework:spring-core:jar:5.3.21:test - version managed from 5.3.21; omitted for duplicate)
[INFO] | +- org.springframework:spring-test:jar:5.3.21:test (version managed from 5.3.21)
[INFO] | | \- (org.springframework:spring-core:jar:5.3.21:test - version managed from 5.3.21; omitted for duplicate)
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.9.0:test (version managed from 2.9.0)
[INFO] \- org.springframework.boot:spring-boot-maven-plugin:jar:2.7.1:compile
[INFO] +- org.springframework.boot:spring-boot-buildpack-platform:jar:2.7.1:runtime (version managed from 2.7.1)
[INFO] | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.3:runtime (version managed from 2.13.3)
[INFO] | | +- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.3:runtime (version managed from 2.13.3)
[INFO] | | \- com.fasterxml.jackson.core:jackson-core:jar:2.13.3:runtime (version managed from 2.13.3)
[INFO] | +- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.3:runtime (version managed from 2.13.3)
[INFO] | | +- (com.fasterxml.jackson.core:jackson-core:jar:2.13.3:runtime - version managed from 2.13.3; omitted for duplicate)
[INFO] | | \- (com.fasterxml.jackson.core:jackson-databind:jar:2.13.3:runtime - version managed from 2.13.3; omitted for duplicate)
[INFO] | +- net.java.dev.jna:jna-platform:jar:5.7.0:runtime (scope not updated to runtime)
[INFO] | | \- (net.java.dev.jna:jna:jar:5.7.0:runtime - omitted for conflict with 5.8.0)
[INFO] | +- org.apache.commons:commons-compress:jar:1.21:runtime
[INFO] | | \- (org.osgi:org.osgi.core:jar:6.0.0:runtime - omitted for duplicate)
[INFO] | +- org.apache.httpcomponents:httpclient:jar:4.5.13:runtime (version managed from 4.5.13)
[INFO] | | +- org.apache.httpcomponents:httpcore:jar:4.4.15:runtime (version managed from 4.4.13)
[INFO] | | \- commons-codec:commons-codec:jar:1.15:runtime (version managed from 1.11)
[INFO] | +- (org.springframework:spring-core:jar:5.3.21:runtime - version managed from 5.3.21; omitted for duplicate)
[INFO] | \- org.tomlj:tomlj:jar:1.0.0:runtime
[INFO] | +- org.antlr:antlr4-runtime:jar:4.7.2:runtime
[INFO] | \- (com.google.code.findbugs:jsr305:jar:3.0.2:runtime - omitted for duplicate)
[INFO] +- org.springframework.boot:spring-boot-loader-tools:jar:2.7.1:runtime (version managed from 2.7.1)
[INFO] | +- (org.apache.commons:commons-compress:jar:1.21:runtime - omitted for duplicate)
[INFO] | \- (org.springframework:spring-core:jar:5.3.21:runtime - version managed from 5.3.21; omitted for duplicate)
[INFO] \- org.sonatype.plexus:plexus-build-api:jar:0.0.7:runtime
[INFO] +- org.codehaus.plexus:plexus-utils:jar:1.5.8:runtime
[INFO] | \- org.codehaus.plexus:plexus-interpolation:jar:1.3:runtime
[INFO] \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9:runtime
[INFO] +- junit:junit:jar:4.13.2:runtime (version managed from 3.8.1)
[INFO] | \- org.hamcrest:hamcrest-core:jar:2.2:runtime (version managed from 1.3)
[INFO] | \- (org.hamcrest:hamcrest:jar:2.2:runtime - version managed from 2.2; omitted for duplicate)
[INFO] +- (org.codehaus.plexus:plexus-utils:jar:1.0.4:runtime - omitted for conflict with 1.5.8)
[INFO] \- classworlds:classworlds:jar:1.1-alpha-2:runtime
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for springbootmvndemo 0.0.1-SNAPSHOT:
[INFO]
[INFO] springbootmvndemo .................................. SUCCESS [ 0.941 s]
[INFO] module1 ............................................ SUCCESS [ 0.106 s]
[INFO] module2 ............................................ SUCCESS [ 0.043 s]
[INFO] start .............................................. SUCCESS [ 0.272 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.876 s
[INFO] Finished at: 2022-07-05T23:18:13+08:00
[INFO] ------------------------------------------------------------------------
结论:
经过分析springboog打包依旧遵循「二、单个Jar包内的依赖竞争」这一段落提到的3个原则:
依赖传导原则 、最短路径优先原则、最先声明优先原则。
不同子pom进行构建时会发现引入fastjson版本并不一致,而最终springboot项目打好的fat包中只有一个fastjson版本,那只是因为我们只关心了含main方法的启动类所在module生成的target文件(这个module通常命名是xxx-start,是可以直接运行的程序),也就是说最终生成的fat jar,我们只是使用了这个命名为xxx-start的module进行生成的而已。
参考文章:
https://blog.csdn.net/ttzommed/article/details/114984341