Spring Boot项目使用maven

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 | Gitee

主要插件

  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 7 <!-- 打包的类型,如果有N个,将会打N个类型的包 --> 8 <formats> 9 <format>tar.gz</format> 10 <!--<format>zip</format>--> 11 </formats> 12 13 <includeBaseDirectory>true</includeBaseDirectory> 14 15 <fileSets> 16 <!-- 17 0755->即用户具有读//执行权限,组用户和其它用户具有读写权限; 18 0644->即用户具有读写权限,组用户和其它用户具有只读权限; 19 --> 20 21 <!-- 将src/bin目录下的所有文件输出到打包后的bin目录中 --> 22 <fileSet> 23 <directory>${basedir}/src/bin</directory> 24 <outputDirectory>bin</outputDirectory> 25 <fileMode>0755</fileMode> 26 <includes> 27 <include>**.sh</include> 28 <include>**.bat</include> 29 </includes> 30 </fileSet> 31 32 <!-- 指定输出target/classes中的配置文件到config目录中 --> 33 <fileSet> 34 <directory>${basedir}/target/classes</directory> 35 <outputDirectory>config</outputDirectory> 36 <fileMode>0644</fileMode> 37 <includes> 38 <include>application.yml</include> 39 <include>application-${profileActive}.yml</include> 40 <include>mapper/**/*.xml</include> 41 <include>static/**</include> 42 <include>templates/**</include> 43 <include>*.xml</include> 44 <include>*.properties</include> 45 </includes> 46 </fileSet> 47 48 <!-- 将第三方依赖打包到lib目录中 --> 49 <fileSet> 50 <directory>${basedir}/target/lib</directory> 51 <outputDirectory>lib</outputDirectory> 52 <fileMode>0755</fileMode> 53 </fileSet> 54 55 <!-- 将项目启动jar打包到boot目录中 --> 56 <fileSet> 57 <directory>${basedir}/target</directory> 58 <outputDirectory>boot</outputDirectory> 59 <fileMode>0755</fileMode> 60 <includes> 61 <include>${project.build.finalName}.jar</include> 62 </includes> 63 </fileSet> 64 65 <!-- 包含根目录下的文件 --> 66 <fileSet> 67 <directory>${basedir}</directory> 68 <includes> 69 <include>NOTICE</include> 70 <include>LICENSE</include> 71 <include>*.md</include> 72 </includes> 73 </fileSet> 74 </fileSets> 75 76</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 7<resource> 8 <directory>src/main/resources</directory> 9 <filtering>true</filtering> 10 <includes> 11 <include>application.yml</include> 12 <include>application-${profileActive}.yml</include> 13 <include>mapper/**/*.xml</include> 14 <include>static/**</include> 15 <include>templates/**</include> 16 <include>*.xml</include> 17 <include>*.properties</include> 18 </includes> 19</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 67├─logs 8│ springboot-assembly.log 910├─main 11│ ├─assembly 12│ │ assembly.xml 13│ │ 14│ ├─java 15│ │ └─io 16│ │ └─geekidea 17│ │ └─springboot 18│ │ └─assembly 19│ │ Application.java 20│ │ HelloController.java 21│ │ HelloService.java 22│ │ HelloServiceImpl.java 23│ │ 24│ └─resources 25│ │ application-dev.yml 26│ │ application-local.yml 27│ │ application-prod.yml 28│ │ application-test.yml 29│ │ application-uat.yml 30│ │ application.yml 31│ │ 32│ ├─mapper 33│ │ │ test.xml 34│ │ │ 35│ │ └─hello 36│ │ hello.xml 37│ │ 38│ ├─static 39│ │ index.html 40│ │ 41│ └─templates 42│ test.txt 4344└─test
项目打包
mvn clean package
使用maven assembly插件打包local环境后的压缩包,target目录下
spring-boot-assembly-local-1.0.RELEASE.tar.gz
linux解压tar.gz
tar -zxvf spring-boot-assembly-local-1.0.RELEASE.tar.gz
解压后的目录结构
1└─spring-boot-assembly 2LICENSE 3NOTICE 4README.md 56 ├─bin 7 │ restart.sh 8 │ shutdown.sh 9 │ startup.bat 10 │ startup.sh 1112 ├─boot 13 │ spring-boot-assembly.jar 1415 ├─config 16 │ │ application-local.yml 17 │ │ application.yml 18 │ │ 19 │ ├─mapper 20 │ │ │ test.xml 21 │ │ │ 22 │ │ └─hello 23 │ │ hello.xml 24 │ │ 25 │ ├─static 26 │ │ index.html 27 │ │ 28 │ └─templates 29 │ test.txt 3031 └─lib 32 classmate-1.4.0.jar 33 fastjson-1.2.54.jar 34 hibernate-validator-6.0.13.Final.jar 35 jackson-annotations-2.9.0.jar 36 jackson-core-2.9.7.jar 37 jackson-databind-2.9.7.jar 38 jackson-datatype-jdk8-2.9.7.jar 39 jackson-datatype-jsr310-2.9.7.jar 40 jackson-module-parameter-names-2.9.7.jar 41 javax.annotation-api-1.3.2.jar 42 jboss-logging-3.3.2.Final.jar 43 jul-to-slf4j-1.7.25.jar 44 log4j-api-2.11.1.jar 45 log4j-to-slf4j-2.11.1.jar 46 logback-classic-1.2.3.jar 47 logback-core-1.2.3.jar 48 slf4j-api-1.7.25.jar 49 snakeyaml-1.23.jar 50 spring-aop-5.1.2.RELEASE.jar 51 spring-beans-5.1.2.RELEASE.jar 52 spring-boot-2.1.0.RELEASE.jar 53 spring-boot-autoconfigure-2.1.0.RELEASE.jar 54 spring-boot-starter-2.1.0.RELEASE.jar 55 spring-boot-starter-json-2.1.0.RELEASE.jar 56 spring-boot-starter-logging-2.1.0.RELEASE.jar 57 spring-boot-starter-tomcat-2.1.0.RELEASE.jar 58 spring-boot-starter-web-2.1.0.RELEASE.jar 59 spring-context-5.1.2.RELEASE.jar 60 spring-core-5.1.2.RELEASE.jar 61 spring-expression-5.1.2.RELEASE.jar 62 spring-jcl-5.1.2.RELEASE.jar 63 spring-web-5.1.2.RELEASE.jar 64 spring-webmvc-5.1.2.RELEASE.jar 65 tomcat-embed-core-9.0.12.jar 66 tomcat-embed-el-9.0.12.jar 67 tomcat-embed-websocket-9.0.12.jar 68 validation-api-2.0.1.Final.jar

window启动,会打开浏览器,访问项目测试路径
bin/startup.bat
{"msg":"service hello:123","code":200}
linux启动,停止,重启
1sh bin/startup.sh 启动项目 2sh bin/shutdown.sh 停止服务 3sh bin/restart.sh 重启服务
startup.sh 脚本中的主要内容
  • 配置项目名称及项目启动jar名称,默认项目名称与启动jar名称一致

    APPLICATION="spring-boot-assembly" 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:解决重复异常不打印堆栈信息问题
1JAVA_OPT="-server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m" 2JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"

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

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

最终执行jar包的命令

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

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

日志记录

项目启动日志存储路径,一个项目只有一个启动日志文件
1logs/spring-boot-assembly_startup.log 2 3 4================================================ 2018-12-12 12:36:56 ================================================ 5application name: spring-boot-assembly 6application jar name: spring-boot-assembly.jar 7application bin path: /opt/spring-boot-assembly/bin 8application root path: /opt/spring-boot-assembly 9application log path: /opt/spring-boot-assembly/logs/spring-boot-assembly.log 10application JAVA_OPT : -server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m -XX:-OmitStackTraceInFastThrow 11application 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 & 12application pid: 11596
项目运行日志存储路径,最近一次启动项目的运行日志
1logs/spring-boot-assembly.log 2 3 4 . ____ _ __ _ _ 5 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 6( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 7 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 8 ' |____| .__|_| |_|_| |_\__, | / / / / 9 =========|_|==============|___/=/_/_/_/ 10 :: Spring Boot :: (v2.1.0.RELEASE) 11 122018-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) 132018-12-12 23:28:58.442 INFO 11596 --- [ main] o.s.boot.SpringApplication : The following profiles are active: local 142018-12-12 23:29:01.355 INFO 11596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8100 (http) 152018-12-12 23:29:01.437 INFO 11596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 162018-12-12 23:29:01.437 INFO 11596 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12 172018-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] 182018-12-12 23:29:01.646 INFO 11596 --- [ main] o.a.c.c.C.[.[localhost].[/example] : Initializing Spring embedded WebApplicationContext 192018-12-12 23:29:01.647 INFO 11596 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3028 ms 202018-12-12 23:29:01.708 INFO 11596 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 212018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 222018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 232018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'formContentFilter' to: [/*] 242018-12-12 23:29:01.713 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 252018-12-12 23:29:02.250 INFO 11596 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 262018-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' 272018-12-12 23:29:03.182 INFO 11596 --- [ main] o.s.boot.SpringApplication : Started application in 5.844 seconds (JVM running for 6.547) 28spring.profiles.active = local 29contextPath = /example 30server.port = 8100 31hello = Hello Local 32http://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
参考:
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )