avatar

目录
springboot分离式打包

springboot分离式打包

springboot自带的打包方式

plaintext
1
2
3
4
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

springboot项目内嵌了服务中间件,打包的时候可以使用官方的插件,会得到一个jar,我们往往称之为“胖jar”,可以直接通过nohup java -jar xxx.jar 来运行,非常方便。但是这样的话,我们每次有一点改动就要打包一个几十MB,上百MB的jar包上传到服务器,我们能不能将项目依赖的jar包分离出来呢?

springboot 简单分离式打包方式

将原有的打包方式注释

xml
1
2
3
4
<!-- <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->

增加新的打包方式

Start-Class 的地方需要改成项目的启动类的类路径

exclude 需要注意根据各自的需要使用

xml
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
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
</manifest>
<manifestEntries>
<!-- 这里需要改成项目的启动类的类路径-->
<Start-Class>cn.demo.thf.App</Start-Class>
<Spring-Boot-Classes>/</Spring-Boot-Classes>
</manifestEntries>
</archive>
<excludes>
<!--将你不需要的配置文件剔除出去-->
<exclude>*.properties</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<!-- <configuration> 这里的配置是为了防止证书内的字符在打包的时候被转义导致不可用
<nonFilteredFileExtensions>
<nonFilteredFileExtension>p12</nonFilteredFileExtension>

<nonFilteredFileExtension>keystore</nonFilteredFileExtension>

</nonFilteredFileExtensions>
</configuration> -->
</plugin>

assembly 自定义依赖打包

在这里,这一部分除了第一次部署的时候使用,其他时候都是注释掉的。src/main/assembly 是一个源文件夹,package.xml中定义了配置文件,依赖jar的打包方式

xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/package.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
package.xml

仅供参考:

xml
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
<assembly>
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/resources</directory>
<outputDirectory>etc</outputDirectory>
</fileSet>
<fileSet>
<directory>etc</directory>
<outputDirectory>etc</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/scripts</directory>
<outputDirectory>tools</outputDirectory>
<includes>
<include>start.bat</include>
<include>start.sh</include>
<include>restart.sh</include>
<include>stop.sh</include>
<include>check.sh</include>
</includes>
<excludes>
<exclude>db/*</exclude>
</excludes>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>

启动脚本

这种情况下,项目的启动方式也变得不一样了,下面是启动脚本的示例

bash版本
bash
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
#!/bin/bash

#****1.check JDK******
if [ "$JAVA_HOME" == "" ]; then
echo "pls set JAVA_HOME first"
exit
fi
date_string=`date +"%Y-%m-%d %H:%M:%S"`
echo "-----start脚本开始---"$date_string"---------"
basepath=$(cd `dirname $0`/../; pwd)

echo "工作目录:"$basepath

APP_HOME=$basepath
filepath=$APP_HOME/logs
if [ ! -x "$filepath" ]; then
mkdir $filepath
fi
filepath=$APP_HOME/tmp
if [ ! -x "$filepath" ]; then
mkdir $filepath
fi
MIN_MEM=128m
MAX_MEM=256m
COREDUMP_PATH=${APP_HOME}/logs

# 这里仍然需要改动配置为项目的启动类路径
MAIN_FUNC=cn.demo.thf.App

date_string=`date +"%Y-%m-%d %H:%M:%S"`

CLASSPATH="-classpath .:${APP_HOME}/etc/:${APP_HOME}/lib/* "
JAVA_OPTS="-server -Xms${MIN_MEM} -Xmx${MAX_MEM} "
CORE_OPTS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${COREDUMP_PATH}"
ALL_JAVA_OPTIONS=${JAVA_OPTS}${CLASSPATH}${CORE_OPTS}


echo "-------"$date_string"----------">../logs/log.txt
#nohup java -server -Detcpath=$ETCPATH $MAIN_FUNC>>$APP_HOME/logs/log.txt 2>>$APP_HOME/logs/log.txt&
nohup $JAVA_HOME/bin/java ${ALL_JAVA_OPTIONS} -Djava.io.tmpdir=${APP_HOME}/tmp/ -Dspring.config.location=${APP_HOME}/etc/ $MAIN_FUNC>>$APP_HOME/logs/log.txt 2>>$APP_HOME/logs/log.txt&



echo ---------------start脚本结束!---------------
echo
#tailf $APP_HOME/logs/log.txt

bat版本

bat
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
@ echo off
cd %~dp0
cd ../
set APP_HOME=%cd%

cd tools
set ETCPATH=%APP_HOME%\etc

set MAIN_FUNC=cn.demo.thf.App
set MIN_MEM=128m
set MAX_MEM=256m
set COREDUMP_PATH=%APP_HOME%\logs

set CLASSPATH=-classpath %APP_HOME%\etc\;%APP_HOME%\lib\*
set JAVA_OPTS=-server -Xms%MIN_MEM% -Xmx%MAX_MEM%
set CORE_OPTS=-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%COREDUMP_PATH%"
set ALL_JAVA_OPTIONS=%JAVA_OPTS%%CLASSPATH%%CORE_OPTS%

java -classpath %APP_HOME%\etc\;%APP_HOME%\lib\* -Dspring.config.location=%APP_HOME%\etc\ %MAIN_FUNC%

echo "******************************"%APP_HOME***********************************"


pause


最终的目录结构类似于下面这样

1578995741120

文章作者: thf
文章链接: http://pcbopcbo.github.io/2020/01/14/20200114/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 漂泊的个人笔记

评论