33<!-- TOC depthFrom:2 depthTo:3 -->
44
55- [ 1. 常见问题] ( #1-常见问题 )
6- - [ 1.1. 去哪儿找 maven dependency ?] ( #11-去哪儿找-maven-dependency- )
7- - [ 1.2. 如何指定编码?] ( #12-如何指定编码 )
8- - [ 1.3. 如何指定 JDK 版本?] ( #13-如何指定-jdk-版本 )
9- - [ 1.4. 如何避免将 dependency 打包到构件中?] ( #14-如何避免将-dependency-打包到构件中 )
10- - [ 1.5. 如何跳过单元测试] ( #15-如何跳过单元测试 )
11- - [ 1.6. IDEA 修改 JDK 版本后编译报错] ( #16-idea-修改-jdk-版本后编译报错 )
12- - [ 1.7. 重复引入依赖] ( #17-重复引入依赖 )
13- - [ 1.8. 如何引入本地 jar] ( #18-如何引入本地-jar )
14- - [ 1.9. 如何排除依赖] ( #19-如何排除依赖 )
6+ - [ 1.1. dependencies 和 dependencyManagement,plugins 和 pluginManagement 有什么区别?] ( #11-dependencies-和-dependencymanagementplugins-和-pluginmanagement-有什么区别 )
7+ - [ 1.2. IDEA 修改 JDK 版本后编译报错] ( #12-idea-修改-jdk-版本后编译报错 )
8+ - [ 1.3. 重复引入依赖] ( #13-重复引入依赖 )
9+ - [ 1.4. 如何打包一个可以直接运行的 Spring Boot jar 包] ( #14-如何打包一个可以直接运行的-spring-boot-jar-包 )
10+ - [ 1.5. 去哪儿找 maven dependency ?] ( #15-去哪儿找-maven-dependency- )
11+ - [ 1.6. 如何指定编码?] ( #16-如何指定编码 )
12+ - [ 1.7. 如何指定 JDK 版本?] ( #17-如何指定-jdk-版本 )
13+ - [ 1.8. 如何避免将 dependency 打包到构件中?] ( #18-如何避免将-dependency-打包到构件中 )
14+ - [ 1.9. 如何跳过单元测试] ( #19-如何跳过单元测试 )
15+ - [ 1.10. IDEA 修改 JDK 版本后编译报错] ( #110-idea-修改-jdk-版本后编译报错 )
16+ - [ 1.11. 重复引入依赖] ( #111-重复引入依赖 )
17+ - [ 1.12. 如何引入本地 jar] ( #112-如何引入本地-jar )
18+ - [ 1.13. 如何排除依赖] ( #113-如何排除依赖 )
1519- [ 2. 最佳实践] ( #2-最佳实践 )
1620 - [ 2.1. 通过 bom 统一管理版本] ( #21-通过-bom-统一管理版本 )
1721
1822<!-- /TOC -->
1923
2024## 1. 常见问题
2125
22- ### 1.1. 去哪儿找 maven dependency ?
26+ ### 1.1. dependencies 和 dependencyManagement,plugins 和 pluginManagement 有什么区别?
27+
28+ dependencyManagement 是表示依赖 jar 包的声明,即你在项目中的 dependencyManagement 下声明了依赖,maven 不会加载该依赖,dependencyManagement 声明可以被继承。
29+
30+ dependencyManagement 的一个使用案例是当有父子项目的时候,父项目中可以利用 dependencyManagement 声明子项目中需要用到的依赖 jar 包,之后,当某个或者某几个子项目需要加载该插件的时候,就可以在子项目中 dependencies 节点只配置 groupId 和 artifactId 就可以完成插件的引用。
31+
32+ dependencyManagement 主要是为了统一管理插件,确保所有子项目使用的插件版本保持一致,类似的还有 plugins 和 pluginManagement。
33+
34+ ### 1.2. IDEA 修改 JDK 版本后编译报错
35+
36+ ** 错误现象**
37+
38+ 修改 JDK 版本,指定 maven-compiler-plugin 的 source 和 target 为 1.8 。
39+
40+ 然后,在 Intellij IDEA 中执行 maven 指令,报错:
41+
42+ ```
43+ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:compile (default-compile) on project apollo-common: Fatal error compiling: 无效的目标版本: 1.8 -> [Help 1]
44+ ```
45+
46+ ** 错误原因**
47+
48+ maven 的 JDK 源与指定的 JDK 编译版本不符。
49+
50+ ** 排错手段**
51+
52+ - ** 查看 Project Settings**
53+
54+ Project SDK 是否正确
55+
56+ <div align =" center " ><img src =" https://raw.githubusercontent.com/dunwu/images/master/snap/20181127203324.png " /></div >
57+
58+ SDK 路径是否正确
59+
60+ <div align =" center " ><img src =" https://raw.githubusercontent.com/dunwu/images/master/snap/20181127203427.png " /></div >
61+
62+ - ** 查看 Settings > Maven 的配置**
63+
64+ JDK for importer 是否正确
65+
66+ <div align =" center " ><img src =" https://raw.githubusercontent.com/dunwu/images/master/snap/20181127203408.png " /></div >
67+
68+ Runner 是否正确
69+
70+ <div align =" center " ><img src =" https://raw.githubusercontent.com/dunwu/images/master/snap/20181127203439.png " /></div >
71+
72+ ### 1.3. 重复引入依赖
73+
74+ 在 Idea 中,选中 Module,使用 <kbd >Ctrl+Alt+Shift+U</kbd >,打开依赖图,检索是否存在重复引用的情况。如果存在重复引用,可以将多余的引用删除。
75+
76+ ### 1.4. 如何打包一个可以直接运行的 Spring Boot jar 包
77+
78+ 可以使用 spring-boot-maven-plugin 插件
79+
80+ ``` xml
81+ <build >
82+ <plugins >
83+ <plugin >
84+ <groupId >org.springframework.boot</groupId >
85+ <artifactId >spring-boot-maven-plugin</artifactId >
86+ <executions >
87+ <execution >
88+ <goals >
89+ <goal >repackage</goal >
90+ </goals >
91+ </execution >
92+ </executions >
93+ </plugin >
94+ </plugins >
95+ </build >
96+ ```
97+
98+ 如果引入了第三方 jar 包,如何打包?
99+
100+ 首先,要添加依赖
101+
102+ ``` xml
103+ <dependency >
104+ <groupId >io.github.dunwu</groupId >
105+ <artifactId >dunwu-common</artifactId >
106+ <version >1.0.0</version >
107+ <scope >system</scope >
108+ <systemPath >${project.basedir}/src/main/resources/lib/dunwu-common-1.0.0.jar</systemPath >
109+ </dependency >
110+ ```
111+
112+ 接着,需要配置 spring-boot-maven-plugin 插件:
113+
114+ ``` xml
115+ <build >
116+ <plugins >
117+ <plugin >
118+ <groupId >org.springframework.boot</groupId >
119+ <artifactId >spring-boot-maven-plugin</artifactId >
120+ <executions >
121+ <execution >
122+ <goals >
123+ <goal >repackage</goal >
124+ </goals >
125+ </execution >
126+ </executions >
127+ <configuration >
128+ <includeSystemScope >true</includeSystemScope >
129+ </configuration >
130+ </plugin >
131+ </plugins >
132+ </build >
133+ ```
134+
135+ ### 1.5. 去哪儿找 maven dependency ?
23136
24137问:刚接触 maven 的新手,往往会有这样的疑问,我该去哪儿找 jar?
25138
29142- https://repository.apache.org
30143- https://mvnrepository.com
31144
32- ### 1.2 . 如何指定编码?
145+ ### 1.6 . 如何指定编码?
33146
34147问:众所周知,不同编码格式常常会产生意想不到的诡异问题,那么 maven 构建时如何指定 maven 构建时的编码?
35148
41154</properties >
42155```
43156
44- ### 1.3 . 如何指定 JDK 版本?
157+ ### 1.7 . 如何指定 JDK 版本?
45158
46159问:如何指定 maven 构建时的 JDK 版本?
47160
80193</build >
81194```
82195
83- ### 1.4 . 如何避免将 dependency 打包到构件中?
196+ ### 1.8 . 如何避免将 dependency 打包到构件中?
84197
85198答:指定 maven dependency 的 scope 为 ` provided ` ,这意味着:依赖关系将在运行时由其容器或 JDK 提供。
86199具有此范围的依赖关系不会传递,也不会捆绑在诸如 WAR 之类的包中,也不会包含在运行时类路径中。
87200
88- ### 1.5 . 如何跳过单元测试
201+ ### 1.9 . 如何跳过单元测试
89202
90203问:执行 mvn package 或 mvn install 时,会自动编译所有单元测试(src/test/java 目录下的代码),如何跳过这一步?
91204
92205答:在执行命令的后面,添加命令行参数 ` -Dmaven.test.skip=true ` 或者 ` -DskipTests=true `
93206
94- ### 1.6 . IDEA 修改 JDK 版本后编译报错
207+ ### 1.10 . IDEA 修改 JDK 版本后编译报错
95208
96209** 错误现象**
97210
@@ -129,11 +242,11 @@ Runner 是否正确
129242
130243<div align =" center " ><img src =" http://dunwu.test.upcdn.net/snap/20181127203439.png " /></div >
131244
132- ### 1.7 . 重复引入依赖
245+ ### 1.11 . 重复引入依赖
133246
134247在 Idea 中,选中 Module,使用 <kbd >Ctrl+Alt+Shift+U</kbd >,打开依赖图,检索是否存在重复引用的情况。
135248
136- ### 1.8 . 如何引入本地 jar
249+ ### 1.12 . 如何引入本地 jar
137250
138251问:有时候,需要引入在中央仓库找不到的 jar,但又想通过 maven 进行管理,那么应该如何做到呢?
139252答:可以通过设置 dependency 的 scope 为 system 来引入本地 jar。
@@ -152,7 +265,7 @@ Runner 是否正确
152265</dependency >
153266```
154267
155- ### 1.9 . 如何排除依赖
268+ ### 1.13 . 如何排除依赖
156269
157270问:如何排除依赖一个依赖关系?比方项目中使用的 libA 依赖某个库的 1.0 版。libB 以来某个库的 2.0 版,如今想统一使用 2.0 版,怎样去掉 1.0 版的依赖?
158271
@@ -179,39 +292,51 @@ Runner 是否正确
179292
180293### 2.1. 通过 bom 统一管理版本
181294
182- 采用类似 spring-framework-bom 的方式统一管理依赖版本。
295+ 采用类似 ` spring-boot-dependencies ` 的方式统一管理依赖版本。
183296
184- spring-framework-bom 形式:
297+ spring-boot-dependencies 的 pom.xml 形式:
185298
186299``` xml
187300<?xml version =" 1.0" encoding =" UTF-8" ?>
188301<project xmlns =" http://maven.apache.org/POM/4.0.0" xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
189- <modelVersion >4.0.0</modelVersion >
190- <groupId >org.springframework</groupId >
191- <artifactId >spring-framework-bom </artifactId >
192- <version >4.3.13 .RELEASE</version >
193- <packaging >pom</packaging >
302+ <modelVersion >4.0.0</modelVersion >
303+ <groupId >org.springframework.boot </groupId >
304+ <artifactId >spring-boot-dependencies </artifactId >
305+ <version >2.1.4 .RELEASE</version >
306+ <packaging >pom</packaging >
194307
195- ...
308+ <!-- 省略 -->
196309
197- <dependencyManagement >
310+ <!-- 依赖包版本管理 -->
311+ <dependencyManagement >
198312 <dependencies >
199- </dependency >
200- </dependencyManagement >
313+ <!-- 省略 -->
314+ </dependencies >
315+ </dependencyManagement >
316+
317+ <build >
318+ <!-- 插件版本管理 -->
319+ <pluginManagement >
320+ <plugins >
321+ <!-- 省略 -->
322+ </plugins >
323+ </pluginManagement >
324+ </build >
201325</project >
202326```
203327
204- 其他项目引入 spring-framework-bom 来管理 spring-framework 依赖版本的方式 :
328+ 其他项目引入 spring-boot-dependencies 来管理依赖版本的方式 :
205329
206330``` xml
207- <dependencyManagement >
331+ <dependencyManagement >
208332 <dependencies >
209- <dependency >
210- <groupId >org.springframework</groupId >
211- <artifactId >spring-framework-bom</artifactId >
212- <version >${spring.version}</version >
213- <type >pom</type >
214- <scope >import</scope >
215- </dependency >
333+ <dependency >
334+ <groupId >org.springframework.boot</groupId >
335+ <artifactId >spring-boot-dependencies</artifactId >
336+ <version >${spring-boot.version}</version >
337+ <type >pom</type >
338+ <scope >import</scope >
339+ </dependency >
340+ </dependencies >
216341</dependencyManagement >
217342```
0 commit comments