springboot分离式打包 springboot自带的打包方式 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 简单分离式打包方式 将原有的打包方式注释
增加新的打包方式 Start-Class 的地方需要改成项目的启动类的类路径
exclude 需要注意根据各自的需要使用
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 > </plugin >
assembly 自定义依赖打包 在这里,这一部分除了第一次部署的时候使用,其他时候都是注释掉的。src/main/assembly 是一个源文件夹,package.xml中定义了配置文件,依赖jar的打包方式
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 仅供参考:
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版本 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 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.txtnohup $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
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 %~dp0cd ../set APP_HOME=%cd% cd toolsset ETCPATH=%APP_HOME% \etcset MAIN_FUNC=cn.demo.thf.Appset MIN_MEM=128 mset MAX_MEM=256 mset COREDUMP_PATH=%APP_HOME% \logsset 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
最终的目录结构类似于下面这样