欢迎访问Spring Cloud中国社区

《重新定义Spring Cloud实战》由Spring Cloud中国社区倾力打造,基于Spring Cloud的Finchley.RELEASE版本,本书内容宽度足够广、深度足够深,而且立足于生产实践,直接从生产实践出发,包含大量生产实践的配置。欢迎加微信Software_King进群答疑,国内谁在使用Spring Cloud?欢迎登记

Spring Boot项目使用maven-assembly-plugin根据不同环境打包成tar.gz或者zip

geekidea · 1月前 · 464 ·

spring-boot-assembly

  1. 在spring boot项目中使用maven profiles和maven assembly插件根据不同环境打包成tar.gz或者zip
  2. 将spring boot项目中的配置文件提取到外部config目录中
  3. 将spring boot项目中的启动jar包移动到boot目录中
  4. 将spring boot项目中的第三方依赖jar包移动到外部lib目录中
  5. bin目录中是启动,停止,重启服务命令
  6. 打包后的目录结构类似于tomcat/maven目录结构

GITHUB项目主页

https://github.com/geekidea/spring-boot-assembly

主要插件

  1. maven-assembly-plugin
  2. maven-jar-plugin
  3. spring-boot-maven-plugin
  4. maven-dependency-plugin
  5. maven-resources-plugin

1.maven-assembly-plugin 配置assembly.xml文件路径

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

2.assembly.xml打包配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <assembly>
  3. <!-- 可自定义,这里指定的是项目环境 -->
  4. <!-- spring-boot-assembly-local-1.0.RELEASE.tar.gz -->
  5. <id>${profileActive}-${project.version}</id>
  6. <!-- 打包的类型,如果有N个,将会打N个类型的包 -->
  7. <formats>
  8. <format>tar.gz</format>
  9. <!--<format>zip</format>-->
  10. </formats>
  11. <includeBaseDirectory>true</includeBaseDirectory>
  12. <fileSets>
  13. <!--
  14. 0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
  15. 0644->即用户具有读写权限,组用户和其它用户具有只读权限;
  16. -->
  17. <!-- 将src/bin目录下的所有文件输出到打包后的bin目录中 -->
  18. <fileSet>
  19. <directory>${basedir}/src/bin</directory>
  20. <outputDirectory>bin</outputDirectory>
  21. <fileMode>0755</fileMode>
  22. <includes>
  23. <include>**.sh</include>
  24. <include>**.bat</include>
  25. </includes>
  26. </fileSet>
  27. <!-- 指定输出target/classes中的配置文件到config目录中 -->
  28. <fileSet>
  29. <directory>${basedir}/target/classes</directory>
  30. <outputDirectory>config</outputDirectory>
  31. <fileMode>0644</fileMode>
  32. <includes>
  33. <include>application.yml</include>
  34. <include>application-${profileActive}.yml</include>
  35. <include>mapper/**/*.xml</include>
  36. <include>static/**</include>
  37. <include>templates/**</include>
  38. <include>*.xml</include>
  39. <include>*.properties</include>
  40. </includes>
  41. </fileSet>
  42. <!-- 将第三方依赖打包到lib目录中 -->
  43. <fileSet>
  44. <directory>${basedir}/target/lib</directory>
  45. <outputDirectory>lib</outputDirectory>
  46. <fileMode>0755</fileMode>
  47. </fileSet>
  48. <!-- 将项目启动jar打包到boot目录中 -->
  49. <fileSet>
  50. <directory>${basedir}/target</directory>
  51. <outputDirectory>boot</outputDirectory>
  52. <fileMode>0755</fileMode>
  53. <includes>
  54. <include>${project.build.finalName}.jar</include>
  55. </includes>
  56. </fileSet>
  57. <!-- 包含根目录下的文件 -->
  58. <fileSet>
  59. <directory>${basedir}</directory>
  60. <includes>
  61. <include>NOTICE</include>
  62. <include>LICENSE</include>
  63. <include>*.md</include>
  64. </includes>
  65. </fileSet>
  66. </fileSets>
  67. </assembly>

3.spring-boot-maven-plugin 排除启动jar包中依赖的jar包

  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. <configuration>
  5. <layout>ZIP</layout>
  6. <includes>
  7. <!-- 项目启动jar包中排除依赖包 -->
  8. <include>
  9. <groupId>non-exists</groupId>
  10. <artifactId>non-exists</artifactId>
  11. </include>
  12. </includes>
  13. </configuration>
  14. </plugin>

4.maven-jar-plugin 自定义maven jar打包内容

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>3.1.0</version>
  5. <configuration>
  6. <archive>
  7. <manifest>
  8. <!-- 项目启动类 -->
  9. <mainClass>Application</mainClass>
  10. <!-- 依赖的jar的目录前缀 -->
  11. <classpathPrefix>../lib</classpathPrefix>
  12. <addClasspath>true</addClasspath>
  13. </manifest>
  14. </archive>
  15. <includes>
  16. <!-- 只打包指定目录的文件 -->
  17. <include>io/geekidea/springboot/**</include>
  18. </includes>
  19. </configuration>
  20. </plugin>

5.maven-dependency-plugin 复制项目的依赖包到指定目录

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-dependency-plugin</artifactId>
  4. <version>3.1.0</version>
  5. <executions>
  6. <execution>
  7. <phase>prepare-package</phase>
  8. <goals>
  9. <goal>copy-dependencies</goal>
  10. </goals>
  11. <configuration>
  12. <outputDirectory>target/lib</outputDirectory>
  13. <overWriteReleases>false</overWriteReleases>
  14. <overWriteSnapshots>false</overWriteSnapshots>
  15. <overWriteIfNewer>true</overWriteIfNewer>
  16. <includeScope>compile</includeScope>
  17. </configuration>
  18. </execution>
  19. </executions>
  20. </plugin>

6.maven-resources-plugin

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-resources-plugin</artifactId>
  4. <version>3.1.0</version>
  5. </plugin>
  6. <resource>
  7. <directory>src/main/resources</directory>
  8. <filtering>true</filtering>
  9. <includes>
  10. <include>application.yml</include>
  11. <include>application-${profileActive}.yml</include>
  12. <include>mapper/**/*.xml</include>
  13. <include>static/**</include>
  14. <include>templates/**</include>
  15. <include>*.xml</include>
  16. <include>*.properties</include>
  17. </includes>
  18. </resource>

7.maven profiles配置

  1. <!--MAVEN打包选择运行环境-->
  2. <!-- 1:local(默认) 本地 2:dev:开发环境 3:test 4:uat 用户验收测试 5.pro:生产环境-->
  3. <profiles>
  4. <profile>
  5. <id>local</id>
  6. <properties>
  7. <profileActive>local</profileActive>
  8. </properties>
  9. <activation>
  10. <activeByDefault>true</activeByDefault>
  11. </activation>
  12. </profile>
  13. <profile>
  14. <id>dev</id>
  15. <properties>
  16. <profileActive>dev</profileActive>
  17. </properties>
  18. <activation>
  19. <activeByDefault>false</activeByDefault>
  20. </activation>
  21. </profile>
  22. <profile>
  23. <id>test</id>
  24. <properties>
  25. <profileActive>test</profileActive>
  26. </properties>
  27. <activation>
  28. <activeByDefault>false</activeByDefault>
  29. </activation>
  30. </profile>
  31. <profile>
  32. <id>uat</id>
  33. <properties>
  34. <profileActive>uat</profileActive>
  35. </properties>
  36. <activation>
  37. <activeByDefault>false</activeByDefault>
  38. </activation>
  39. </profile>
  40. <profile>
  41. <id>prod</id>
  42. <properties>
  43. <profileActive>prod</profileActive>
  44. </properties>
  45. <activation>
  46. <activeByDefault>false</activeByDefault>
  47. </activation>
  48. </profile>
  49. </profiles>

8.阿里云仓库配置

  1. <repositories>
  2. <!--阿里云仓库-->
  3. <repository>
  4. <id>aliyun</id>
  5. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  6. </repository>
  7. </repositories>

项目源码结构
  1. ├─bin
  2. restart.sh
  3. shutdown.sh
  4. startup.bat
  5. startup.sh
  6. ├─logs
  7. springboot-assembly.log
  8. ├─main
  9. ├─assembly
  10. assembly.xml
  11. ├─java
  12. └─io
  13. └─geekidea
  14. └─springboot
  15. └─assembly
  16. Application.java
  17. HelloController.java
  18. HelloService.java
  19. HelloServiceImpl.java
  20. └─resources
  21. application-dev.yml
  22. application-local.yml
  23. application-prod.yml
  24. application-test.yml
  25. application-uat.yml
  26. application.yml
  27. ├─mapper
  28. test.xml
  29. └─hello
  30. hello.xml
  31. ├─static
  32. index.html
  33. └─templates
  34. test.txt
  35. └─test
项目打包
  1. mvn clean package
使用maven assembly插件打包local环境后的压缩包,target目录下
  1. spring-boot-assembly-local-1.0.RELEASE.tar.gz
linux解压tar.gz
  1. tar -zxvf spring-boot-assembly-local-1.0.RELEASE.tar.gz
解压后的目录结构
  1. └─spring-boot-assembly
  2. LICENSE
  3. NOTICE
  4. README.md
  5. ├─bin
  6. restart.sh
  7. shutdown.sh
  8. startup.bat
  9. startup.sh
  10. ├─boot
  11. spring-boot-assembly.jar
  12. ├─config
  13. application-local.yml
  14. application.yml
  15. ├─mapper
  16. test.xml
  17. └─hello
  18. hello.xml
  19. ├─static
  20. index.html
  21. └─templates
  22. test.txt
  23. └─lib
  24. classmate-1.4.0.jar
  25. fastjson-1.2.54.jar
  26. hibernate-validator-6.0.13.Final.jar
  27. jackson-annotations-2.9.0.jar
  28. jackson-core-2.9.7.jar
  29. jackson-databind-2.9.7.jar
  30. jackson-datatype-jdk8-2.9.7.jar
  31. jackson-datatype-jsr310-2.9.7.jar
  32. jackson-module-parameter-names-2.9.7.jar
  33. javax.annotation-api-1.3.2.jar
  34. jboss-logging-3.3.2.Final.jar
  35. jul-to-slf4j-1.7.25.jar
  36. log4j-api-2.11.1.jar
  37. log4j-to-slf4j-2.11.1.jar
  38. logback-classic-1.2.3.jar
  39. logback-core-1.2.3.jar
  40. slf4j-api-1.7.25.jar
  41. snakeyaml-1.23.jar
  42. spring-aop-5.1.2.RELEASE.jar
  43. spring-beans-5.1.2.RELEASE.jar
  44. spring-boot-2.1.0.RELEASE.jar
  45. spring-boot-autoconfigure-2.1.0.RELEASE.jar
  46. spring-boot-starter-2.1.0.RELEASE.jar
  47. spring-boot-starter-json-2.1.0.RELEASE.jar
  48. spring-boot-starter-logging-2.1.0.RELEASE.jar
  49. spring-boot-starter-tomcat-2.1.0.RELEASE.jar
  50. spring-boot-starter-web-2.1.0.RELEASE.jar
  51. spring-context-5.1.2.RELEASE.jar
  52. spring-core-5.1.2.RELEASE.jar
  53. spring-expression-5.1.2.RELEASE.jar
  54. spring-jcl-5.1.2.RELEASE.jar
  55. spring-web-5.1.2.RELEASE.jar
  56. spring-webmvc-5.1.2.RELEASE.jar
  57. tomcat-embed-core-9.0.12.jar
  58. tomcat-embed-el-9.0.12.jar
  59. tomcat-embed-websocket-9.0.12.jar
  60. validation-api-2.0.1.Final.jar

window启动,会打开浏览器,访问项目测试路径
  1. bin/startup.bat
linux启动,停止,重启
  1. sh bin/startup.sh 启动项目
  2. sh bin/shutdown.sh 停止服务
  3. sh bin/restart.sh 重启服务
startup.sh 脚本中的主要内容
  • 配置项目名称及项目启动jar名称,默认项目名称与启动jar名称一致
  1. APPLICATION="spring-boot-assembly"
  2. APPLICATION_JAR="${APPLICATION}.jar"
  • JAVA_OPTION配置
    • JVM Configuration
    • -Xmx256m:设置JVM最大可用内存为256m,根据项目实际情况而定,建议最小和最大设置成一样。
    • -Xms256m:设置JVM初始内存。此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存
    • -Xmn512m:设置年轻代大小为512m。整个JVM内存大小=年轻代大小 + 年老代大小 + 持久代大小。持久代一般固定大小为64m,所以增大年轻代,将会减小年老代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8
    • -XX:MetaspaceSize=64m:存储class的内存大小,该值越大触发Metaspace GC的时机就越晚
    • -XX:MaxMetaspaceSize=320m:限制Metaspace增长的上限,防止因为某些情况导致Metaspace无限的使用本地内存,影响到其他程序
    • -XX:-OmitStackTraceInFastThrow:解决重复异常不打印堆栈信息问题
  1. JAVA_OPT="-server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m"
  2. JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"

执行启动命令:后台启动项目,并将日志输出到项目根目录下的logs文件夹下

  1. nohup java ${JAVA_OPT} -jar ${BASE_PATH}/boot/${APPLICATION_JAR} --spring.config.location=${CONFIG_DIR} > ${LOG_PATH} 2>&1 &

最终执行jar包的命令

  1. nohup java -server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m -XX:-OmitStackTraceInFastThrow -jar /opt/spring-boot-assembly/boot/spring-boot-assembly.jar --spring.config.location=/opt/spring-boot-assembly/config/ > /opt/spring-boot-assembly/logs/spring-boot-assembly.log 2>&1 &
  • nohup:在后台运行jar包,然后将运行日志输出到指定位置
  • -server:指定JVM参数
  • -jar /opt/spring-boot-assembly/boot/spring-boot-assembly.jar:指定启动的jar包
  • 启动命令中指定的启动jar包路径,配置文件路径,日志路径都是绝对路径
  • 可在任何位置执行start.sh,shutdown.sh,restart.sh脚本
  • —spring.config.location:指定配置文件目录或者文件名称,如果是目录,以/结束
  • > /opt/spring-boot-assembly/logs/spring-boot-assembly.log:指定日志输出路径
  • 2>&1 & :将正常的运行日志和错误日志合并输入到指定日志,并在后台运行

shutdown.sh停服脚本,实现方式:找到当前项目的PID,然后kill -9

  1. PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk '{ print $2 }')
  2. kill -9 ${PID}

日志记录

项目启动日志存储路径,一个项目只有一个启动日志文件
  1. logs/spring-boot-assembly_startup.log
  1. ================================================ 2018-12-12 12:36:56 ================================================
  2. application name: spring-boot-assembly
  3. application jar name: spring-boot-assembly.jar
  4. application bin path: /opt/spring-boot-assembly/bin
  5. application root path: /opt/spring-boot-assembly
  6. application log path: /opt/spring-boot-assembly/logs/spring-boot-assembly.log
  7. application JAVA_OPT : -server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m -XX:-OmitStackTraceInFastThrow
  8. application background startup command: nohup java -server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m -XX:-OmitStackTraceInFastThrow -jar /opt/spring-boot-assembly/boot/spring-boot-assembly.jar --spring.config.location=/opt/spring-boot-assembly/config/ > /opt/spring-boot-assembly/logs/spring-boot-assembly.log 2>&1 &
  9. application pid: 11596
项目运行日志存储路径,最近一次启动项目的运行日志
  1. logs/spring-boot-assembly.log
  1. . ____ _ __ _ _
  2. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  3. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  4. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
  5. ' |____| .__|_| |_|_| |_\__, | / / / /
  6. =========|_|==============|___/=/_/_/_/
  7. :: Spring Boot :: (v2.1.0.RELEASE)
  8. 2018-12-12 23:28:58.420 INFO 11596 --- [ main] o.s.boot.SpringApplication : Starting application on VM_0_17_centos with PID 11596 (started by root in /opt/spring-boot-assembly)
  9. 2018-12-12 23:28:58.442 INFO 11596 --- [ main] o.s.boot.SpringApplication : The following profiles are active: local
  10. 2018-12-12 23:29:01.355 INFO 11596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8100 (http)
  11. 2018-12-12 23:29:01.437 INFO 11596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
  12. 2018-12-12 23:29:01.437 INFO 11596 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
  13. 2018-12-12 23:29:01.461 INFO 11596 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
  14. 2018-12-12 23:29:01.646 INFO 11596 --- [ main] o.a.c.c.C.[.[localhost].[/example] : Initializing Spring embedded WebApplicationContext
  15. 2018-12-12 23:29:01.647 INFO 11596 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3028 ms
  16. 2018-12-12 23:29:01.708 INFO 11596 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
  17. 2018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
  18. 2018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  19. 2018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*]
  20. 2018-12-12 23:29:01.713 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
  21. 2018-12-12 23:29:02.250 INFO 11596 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  22. 2018-12-12 23:29:03.179 INFO 11596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8100 (http) with context path '/example'
  23. 2018-12-12 23:29:03.182 INFO 11596 --- [ main] o.s.boot.SpringApplication : Started application in 5.844 seconds (JVM running for 6.547)
  24. spring.profiles.active = local
  25. contextPath = /example
  26. server.port = 8100
  27. hello = Hello Local
  28. http://localhost:8100/example/hello?name=123
项目历史运行日志存储路径,每启动一次项目,会将之前的运行日志移动到back目录
  1. `-- logs
  2. |-- back
  3. | |-- spring-boot-assembly_back_2018-12-12-23-30-10.log
  4. | `-- spring-boot-assembly_back_2018-12-12-23-36-56.log
  5. |-- spring-boot-assembly.log
  6. `-- spring-boot-assembly_startup.log
参考: