SpringBoot(十四) 多模块打包与部署

我们接上章节内容继续,上一章我们已经完成了多模块项目的创建以及运行,那我们线上环境该如何打包部署呢?

目标

打包部署SpringBoot多模块项目到外部运行Tomcat容器中。

构建项目

使用上一章的项目代码

分布打包方式

Maven Install

如果现在直接执行pager子模块项目打包命令(maven package) maven会提示我们没有找到所需要的user.jar,因为我们的pager模块项目的pom.xml配置文件内引入了com.ray.user模块

pom.xml

1
2
3
4
5
6
7
8
<dependencies>
<!--引入用户模块-->
<dependency>
<groupId>com.ray</groupId>
<artifactId>user</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

错误提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< com.ray:pager >----------------------------
[INFO] Building pager 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[WARNING] The POM for com.ray:user:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.404 s
[INFO] Finished at: 2018-07-31T11:36:05+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project pager: Could not resolve dependencies for project com.ray:pager:war:0.0.1-SNAPSHOT: Could not find artifact com.ray:user:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

我们打包时一定要注意要从完全被依赖的项目开始

Bean Module Maven Install

因为我们的bean子项目是完全被依赖的,所以我们先从这个项目开始,点开Maven Projects后我们找到bean项目,展开Lifecycle选择 install命令双击开始执行打包,当我们执行maven install命令时会直接将我们工作空间内的项目直接生成jar包并且添加到本地maven仓库,这样我们在项目中就可以直接依赖使用,双击install命令后查看控制台输出日志,如下图所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< com.ray:bean >----------------------------
[INFO] Building bean 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ bean ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ bean ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ bean ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ bean ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ bean ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ bean ---
[INFO] Building jar: F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\target\bean-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ bean ---
[INFO] Installing F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\target\bean-1.0-SNAPSHOT.jar to F:\drive\MVN\com\ray\bean\1.0-SNAPSHOT\bean-1.0-SNAPSHOT.jar
[INFO] Installing F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\pom.xml to F:\drive\MVN\com\ray\bean\1.0-SNAPSHOT\bean-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.023 s
[INFO] Finished at: 2018-07-31T11:43:00+08:00
[INFO] ------------------------------------------------------------------------

可以看到我们的控制台已经输出了打包成功的日志,那我们可以直接查看是否已经存在,如下图所示:

可以看到已经存在,这样我们user模块就可以执行下面的打包操作,因为步骤一样我这里就不做一一解释了

User Module Maven Package

Lifecycle-install 同样的方式打包user项目(注意:所有被依赖都打包完成后才可以打包依赖项目)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< com.ray:user >----------------------------
[INFO] Building user 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ user ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ user ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ user ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ user ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ user ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ user ---
[INFO] Building jar: F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\target\user-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ user ---
[INFO] Installing F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\target\user-1.0-SNAPSHOT.jar to F:\drive\MVN\com\ray\user\1.0-SNAPSHOT\user-1.0-SNAPSHOT.jar
[INFO] Installing F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\pom.xml to F:\drive\MVN\com\ray\user\1.0-SNAPSHOT\user-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.714 s
[INFO] Finished at: 2018-07-31T13:29:53+08:00
[INFO] ------------------------------------------------------------------------

Pager Module Maven Package

所有的jar包依赖都执行打包完成后,我们开始进行pager项目的打包,因为我们的pager项目是一个web项目,我们只是需要生成后的war包部署到外部容器而已所以我们需要一个命令 Lifecycle-package

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------------< com.ray:pager >----------------------------
[INFO] Building pager 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ pager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ pager ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ pager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ pager ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ pager ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:3.1.0:war (default-war) @ pager ---
[INFO] Packaging webapp
[INFO] Assembling webapp [pager] in [F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\target\pager]
[INFO] Processing war project
[INFO] Copying webapp resources [F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\src\main\webapp]
[INFO] Webapp assembled in [898 msecs]
[INFO] Building war: F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\target\pager.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.409 s
[INFO] Finished at: 2018-07-31T13:27:20+08:00
[INFO] ------------------------------------------------------------------------

修改包名称

我们可以看到控制台输出我们打包成功的日志,我们可以去target目录下查看已经有一个 pager-0.0.1-SNAPSHOT.war,打包的名称可以通过添加finalName参数修改,如下所示

1
2
3
4
5
<name>pager</name>
<description>Demo project for Spring Boot</description>
<build>
<finalName>pager</finalName>
</build>


先来执行clean命令后再执行package命令,这时target就会有一个pager.war存在。

统一打包方式(带root标签)

我们上面讲的是分步打包,我们可以在项目中执行maven package命令,这样所有的子模块以及父模块都会参与打包并且在在target目录下生成对应模块的jar或者war

下面我们来执行父模块package命令查看效果如下图所示:

父模块package命令


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] springboot2-14_02 [pom]
[INFO] bean [jar]
[INFO] user [jar]
[INFO] pager [war]
[INFO]
[INFO] ---------------------< com.ray:springboot2-14_02 >----------------------
[INFO] Building springboot2-14_02 1.0-SNAPSHOT [1/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] ----------------------------< com.ray:bean >----------------------------
[INFO] Building bean 1.0-SNAPSHOT [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ bean ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ bean ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ bean ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ bean ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ bean ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ bean ---
[INFO] Building jar: F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\bean\target\bean-1.0-SNAPSHOT.jar
[INFO]
[INFO] ----------------------------< com.ray:user >----------------------------
[INFO] Building user 1.0-SNAPSHOT [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ user ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ user ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ user ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ user ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ user ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ user ---
[INFO] Building jar: F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\user\target\user-1.0-SNAPSHOT.jar
[INFO]
[INFO] ---------------------------< com.ray:pager >----------------------------
[INFO] Building pager 0.0.1-SNAPSHOT [4/4]
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ pager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ pager ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ pager ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ pager ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ pager ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:3.1.0:war (default-war) @ pager ---
[INFO] Packaging webapp
[INFO] Assembling webapp [pager] in [F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\target\pager]
[INFO] Processing war project
[INFO] Copying webapp resources [F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\src\main\webapp]
[INFO] Webapp assembled in [198 msecs]
[INFO] Building war: F:\JetBrains\IdeaProjects\SpringBoot\put\springboot2-14_02\pager\target\pager.war
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] springboot2-14_02 1.0-SNAPSHOT ..................... SUCCESS [ 0.010 s]
[INFO] bean ............................................... SUCCESS [ 2.103 s]
[INFO] user ............................................... SUCCESS [ 0.288 s]
[INFO] pager 0.0.1-SNAPSHOT ............................... SUCCESS [ 1.320 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.243 s
[INFO] Finished at: 2018-07-31T12:15:27+08:00
[INFO] ------------------------------------------------------------------------

部署至Tomcat

将生成的 pager.war 复制到tomcat目录下 webapps

启动Tomcat

访问项目

访问地址:IP:端口 + 包名 + 路径
http://192.168.2.172:8080/pager/user/index?name=Ray

-------------- 本文结束  感谢您的阅读 --------------