maven项目部署实践

使用maven构建spring boot项目,且使用maven插件打包不包含依赖的瘦身jar包

标准pom

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>

<!--默认打包类型为jar-->
<groupId>com.demo</groupId>
<artifactId>server-demo</artifactId>
<version>1.0.0</version>

<!-- Spring Boot 父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M5</version>
</parent>

<build>
<plugins>

<plugin>
<!--打包不包含依赖的thin jar包,并指定启动类、classpath,并指定不打包资源-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.Application</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
<!--不打包资源文件,copy-resource插件会拷贝到相应的目录去-->
<excludes>
<exclude>bin/*</exclude>
<exclude>config/*</exclude>
</excludes>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<!--不编译test类-->
<skip>true</skip>
</configuration>
</plugin>

<!--不执行单元测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

<!--拷贝依赖 copy-dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/release/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

<!--拷贝资源文件 copy-resources -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<encoding>UTF-8</encoding>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<!--拷贝脚本和配置文件-->
<include>bin/*</include>
<include>config/*</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/target</directory>
<filtering>true</filtering>
<includes>
<!--拷贝thin jar包-->
<include>*.jar</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}</directory>
<filtering>true</filtering>
<includes>
<!--拷贝其他配置文件-->
<include>others.conf</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.basedir}/release</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<!--当资源文件使用${} 或 @@ 引用pom里的变量,执行替换-->
<delimiters>
<delimiter>${*}</delimiter><!-- to keep the default behavior -->
<delimiter>@</delimiter><!-- to add Ant-like tokens style -->
</delimiters>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>jar</nonFilteredFileExtension>
<nonFilteredFileExtension>bat</nonFilteredFileExtension>
<nonFilteredFileExtension>bytes</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>

<!--执行clean的时候删除logs目录-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>logs</directory>
</fileset>
</filesets>
</configuration>
</plugin>

</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--当资源文件使用${} 或 @@ 引用pom里的变量,执行替换-->
<filtering>true</filtering>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<!--当资源文件使用${} 或 @@ 引用pom里的变量,执行替换-->
<filtering>true</filtering>
</testResource>
</testResources>
</build>

<profiles>

<profile>
<id>develop-qj</id>
<activation></activation>
<properties></properties>
</profile>

<profile>
<id>local</id>
<activation></activation>
<properties></properties>
</profile>
<profile>
<id>test</id>
<activation></activation>
<properties></properties>
</profile>

<profile>
<id>product</id>
<activation></activation>
<properties></properties>
</profile>

</profiles>

<!--指定项目依赖jar包-->
<dependencies></dependencies>

<!--统一管理依赖的版本-->
<dependencyManagement></dependencyManagement>

<!--指定下载jar包的远程仓库-->
<repositories>
<repository>
<id>nexus</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<!--指定下载插件的远程仓库-->
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

</project>

maven打包

执行 mvn clean package -Pprofile

依次执行:

maven-clean-plugin:3.0.0:clean

删除target目录和本地logs目录

maven-resources-plugin:3.0.2:resources

拷贝src/main/resources下的资源文件到target/classes目录下

maven-compiler-plugin:3.7.0:compile

编译src/main/java 类文件到target/classes目录下

maven-jar-plugin:3.1.0:jar

构建thin jar包到target目录下

maven-dependency-plugin:3.0.1:copy-dependencies

拷贝依赖jar包到release/lib目录下

maven-resources-plugin:3.0.2:copy-resources

拷贝自定义资源文件到release目录下

打包后的项目和相关资源文件依赖jar包都放在release目录下:

发布

rysnc脚本:其中test_env是在~/.ssh/config下定义的机器别名

1
2
3
4
5
touch ./release/application.pid
chmod a+x ./release/bin/*.sh
release_dir=/home/server-demo/release/
zip -r server-demo-release.zip ./release/
rsync -zvrP --delete --rsync-path="mkdir -p $release_dir && rm -rf $release_dir/*.jar && rsync" ./server-demo-release.zip test_env:$release_dir

此时,release目录包含了要运行的java程序的所有组件,将此目录压缩成zip包,使用rsync脚本上传到相应的服务器,启动即可。

根据需求可将release目录下的文件分开上传,例如lib目录下的依赖jar包没有更新就不需要每次重复校验上传了。

-------------本文结束感谢您的阅读-------------
Good for you!