SpringBoot專案使用maven-assembly-plugin根據不同環境打包成tar.gz或者zip
spring-boot-assembly
- 在spring boot專案中使用maven profiles和maven assembly外掛根據不同環境打包成tar.gz或者zip
- 將spring boot專案中的配置檔案提取到外部config目錄中
- 將spring boot專案中的啟動jar包移動到boot目錄中
- 將spring boot專案中的第三方依賴jar包移動到外部lib目錄中
- bin目錄中是啟動,停止,重啟服務命令
- 打包後的目錄結構類似於tomcat/maven目錄結構
GITHUB專案主頁
https://github.com/geekidea/spring-boot-assembly
主要外掛
- maven-assembly-plugin
- maven-jar-plugin
- spring-boot-maven-plugin
- maven-dependency-plugin
- maven-resources-plugin
1.maven-assembly-plugin 配置assembly.xml檔案路徑
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
2.assembly.xml打包配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 可自定義,這裡指定的是專案環境 -->
<!-- spring-boot-assembly-local-1.0.RELEASE.tar.gz -->
<id>${profileActive}-${project.version}</id>
<!-- 打包的型別,如果有N個,將會打N個型別的包 -->
<formats>
<format>tar.gz</format>
<!--<format>zip</format>-->
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--
0755->即使用者具有讀/寫/執行許可權,組使用者和其它使用者具有讀寫許可權;
0644->即使用者具有讀寫許可權,組使用者和其它使用者具有隻讀許可權;
-->
<!-- 將src/bin目錄下的所有檔案輸出到打包後的bin目錄中 -->
<fileSet>
<directory>${basedir}/src/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
<!-- 指定輸出target/classes中的配置檔案到config目錄中 -->
<fileSet>
<directory>${basedir}/target/classes</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>mapper/**/*.xml</include>
<include>static/**</include>
<include>templates/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- 將第三方依賴打包到lib目錄中 -->
<fileSet>
<directory>${basedir}/target/lib</directory>
<outputDirectory>lib</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<!-- 將專案啟動jar打包到boot目錄中 -->
<fileSet>
<directory>${basedir}/target</directory>
<outputDirectory>boot</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
<!-- 包含根目錄下的檔案 -->
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>NOTICE</include>
<include>LICENSE</include>
<include>*.md</include>
</includes>
</fileSet>
</fileSets>
</assembly>
3.spring-boot-maven-plugin 排除啟動jar包中依賴的jar包
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<!-- 專案啟動jar包中排除依賴包 -->
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
</configuration>
</plugin>
4.maven-jar-plugin 自定義maven jar打包內容
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<!-- 專案啟動類 -->
<mainClass>Application</mainClass>
<!-- 依賴的jar的目錄字首 -->
<classpathPrefix>../lib</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<includes>
<!-- 只打包指定目錄的檔案 -->
<include>io/geekidea/springboot/**</include>
</includes>
</configuration>
</plugin>
5.maven-dependency-plugin 複製專案的依賴包到指定目錄
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<includeScope>compile</includeScope>
</configuration>
</execution>
</executions>
</plugin>
6.maven-resources-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>mapper/**/*.xml</include>
<include>static/**</include>
<include>templates/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
7.maven profiles配置
<!--MAVEN打包選擇執行環境-->
<!-- 1:local(預設) 本地 2:dev:開發環境 3:test 4:uat 使用者驗收測試 5.pro:生產環境-->
<profiles>
<profile>
<id>local</id>
<properties>
<profileActive>local</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profileActive>test</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>uat</id>
<properties>
<profileActive>uat</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
8.阿里雲倉庫配置
<repositories>
<!--阿里雲倉庫-->
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
_
專案原始碼結構
├─bin
│ restart.sh
│ shutdown.sh
│ startup.bat
│ startup.sh
│
├─logs
│ springboot-assembly.log
│
├─main
│ ├─assembly
│ │ assembly.xml
│ │
│ ├─java
│ │ └─io
│ │ └─geekidea
│ │ └─springboot
│ │ └─assembly
│ │ Application.java
│ │ HelloController.java
│ │ HelloService.java
│ │ HelloServiceImpl.java
│ │
│ └─resources
│ │ application-dev.yml
│ │ application-local.yml
│ │ application-prod.yml
│ │ application-test.yml
│ │ application-uat.yml
│ │ application.yml
│ │
│ ├─mapper
│ │ │ test.xml
│ │ │
│ │ └─hello
│ │ hello.xml
│ │
│ ├─static
│ │ index.html
│ │
│ └─templates
│ test.txt
│
└─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
解壓後的目錄結構
└─spring-boot-assembly
│ LICENSE
│ NOTICE
│ README.md
│
├─bin
│ restart.sh
│ shutdown.sh
│ startup.bat
│ startup.sh
│
├─boot
│ spring-boot-assembly.jar
│
├─config
│ │ application-local.yml
│ │ application.yml
│ │
│ ├─mapper
│ │ │ test.xml
│ │ │
│ │ └─hello
│ │ hello.xml
│ │
│ ├─static
│ │ index.html
│ │
│ └─templates
│ test.txt
│
└─lib
classmate-1.4.0.jar
fastjson-1.2.54.jar
hibernate-validator-6.0.13.Final.jar
jackson-annotations-2.9.0.jar
jackson-core-2.9.7.jar
jackson-databind-2.9.7.jar
jackson-datatype-jdk8-2.9.7.jar
jackson-datatype-jsr310-2.9.7.jar
jackson-module-parameter-names-2.9.7.jar
javax.annotation-api-1.3.2.jar
jboss-logging-3.3.2.Final.jar
jul-to-slf4j-1.7.25.jar
log4j-api-2.11.1.jar
log4j-to-slf4j-2.11.1.jar
logback-classic-1.2.3.jar
logback-core-1.2.3.jar
slf4j-api-1.7.25.jar
snakeyaml-1.23.jar
spring-aop-5.1.2.RELEASE.jar
spring-beans-5.1.2.RELEASE.jar
spring-boot-2.1.0.RELEASE.jar
spring-boot-autoconfigure-2.1.0.RELEASE.jar
spring-boot-starter-2.1.0.RELEASE.jar
spring-boot-starter-json-2.1.0.RELEASE.jar
spring-boot-starter-logging-2.1.0.RELEASE.jar
spring-boot-starter-tomcat-2.1.0.RELEASE.jar
spring-boot-starter-web-2.1.0.RELEASE.jar
spring-context-5.1.2.RELEASE.jar
spring-core-5.1.2.RELEASE.jar
spring-expression-5.1.2.RELEASE.jar
spring-jcl-5.1.2.RELEASE.jar
spring-web-5.1.2.RELEASE.jar
spring-webmvc-5.1.2.RELEASE.jar
tomcat-embed-core-9.0.12.jar
tomcat-embed-el-9.0.12.jar
tomcat-embed-websocket-9.0.12.jar
validation-api-2.0.1.Final.jar
_
window啟動,會開啟瀏覽器,訪問專案測試路徑
bin/startup.bat
- 訪問地址: http://localhost:8100/example/hello?name=123
- 響應結果:
{"msg":"service hello:123","code":200}
linux啟動,停止,重啟
sh bin/startup.sh 啟動專案
sh bin/shutdown.sh 停止服務
sh 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:解決重複異常不列印堆疊資訊問題
JAVA_OPT="-server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m"
JAVA_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
PID=$(ps -ef | grep "${APPLICATION_JAR}" | grep -v grep | awk `{ print $2 }`)
kill -9 ${PID}
日誌記錄
專案啟動日誌儲存路徑,一個專案只有一個啟動日誌檔案
logs/spring-boot-assembly_startup.log
================================================ 2018-12-12 12:36:56 ================================================
application name: spring-boot-assembly
application jar name: spring-boot-assembly.jar
application bin path: /opt/spring-boot-assembly/bin
application root path: /opt/spring-boot-assembly
application log path: /opt/spring-boot-assembly/logs/spring-boot-assembly.log
application JAVA_OPT : -server -Xms256m -Xmx256m -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m -XX:-OmitStackTraceInFastThrow
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 &
application pid: 11596
專案執行日誌儲存路徑,最近一次啟動專案的執行日誌
logs/spring-boot-assembly.log
. ____ _ __ _ _
/\ / ___`_ __ _ _(_)_ __ __ _
( ( )\___ | `_ | `_| | `_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
` |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.0.RELEASE)
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)
2018-12-12 23:28:58.442 INFO 11596 --- [ main] o.s.boot.SpringApplication : The following profiles are active: local
2018-12-12 23:29:01.355 INFO 11596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8100 (http)
2018-12-12 23:29:01.437 INFO 11596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-12-12 23:29:01.437 INFO 11596 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.12
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]
2018-12-12 23:29:01.646 INFO 11596 --- [ main] o.a.c.c.C.[.[localhost].[/example] : Initializing Spring embedded WebApplicationContext
2018-12-12 23:29:01.647 INFO 11596 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3028 ms
2018-12-12 23:29:01.708 INFO 11596 --- [ main] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: `characterEncodingFilter` to: [/*]
2018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: `hiddenHttpMethodFilter` to: [/*]
2018-12-12 23:29:01.712 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: `formContentFilter` to: [/*]
2018-12-12 23:29:01.713 INFO 11596 --- [ main] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: `requestContextFilter` to: [/*]
2018-12-12 23:29:02.250 INFO 11596 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService `applicationTaskExecutor`
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`
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)
spring.profiles.active = local
contextPath = /example
server.port = 8100
hello = Hello Local
http://localhost:8100/example/hello?name=123
專案歷史執行日誌儲存路徑,每啟動一次專案,會將之前的執行日誌移動到back目錄
`-- logs
|-- back
| |-- spring-boot-assembly_back_2018-12-12-23-30-10.log
| `-- spring-boot-assembly_back_2018-12-12-23-36-56.log
|-- spring-boot-assembly.log
`-- spring-boot-assembly_startup.log
參考:
- https://docs.spring.io/spring-boot/docs/current/maven-plugin/
- http://maven.apache.org/components/plugins/maven-assembly-plugin/usage.html
- http://maven.apache.org/plugins/maven-jar-plugin/
- http://maven.apache.org/components/plugins/maven-dependency-plugin/
- https://maven.apache.org/plugins/maven-resources-plugin/
- http://maven.apache.org/guides/introduction/introduction-to-profiles.html
相關文章
- vue-cli 根據不同的環境打包Vue
- Xcode 使用 Configuration 來配置不同的專案環境XCode
- nodemon+cross-env+config實現支援熱更新的能根據不同環境載入不同配置的nodejs環境ROSNodeJS
- 根據使用者的不同登入不同的頁面
- zip是什麼格式 怎麼把檔案打包成zip格式
- [譯] 輕鬆管理 Swift 專案中的不同環境Swift
- 使用Log4Net根據log level的不同將log輸出到不同的檔案中
- Django 專案如何通過載入不同 env 檔案來區分不同環境Django
- 「開發環境」讓 Valet 支援多個本地專案使用不同 PHP 版本開發環境PHP
- springboot專案打war包流程Spring Boot
- 專案開發中的pro、pre、test、dev環境及SpringBoot多環境配置devSpring Boot
- PHP開發環境 03 - 使用KFKDock搭建PHP專案環境PHP開發環境
- 使用HBuilder將web專案打包成appUIWebAPP
- map自定義排序,根據鍵或者值排隊排序
- springboot打包不同環境配置與shell指令碼部署Spring Boot指令碼
- springboot 專案引入tk或者jpa 訪問報錯Spring Boot
- 網頁根據螢幕寬度請求不同的CSS檔案網頁CSS
- Vue搭建專案環境Vue
- Windows環境部署專案Windows
- Gradle根據引數配置不同的依賴Gradle
- vue-cli 搭建的專案處理不同環境下請求不同域名的問題Vue
- AndroidStudio專案打包成jarAndroidJAR
- linux或者CentOS環境下安裝.NET Core環境LinuxCentOS
- py目錄下獲取指定字尾檔案打包成zip檔案
- 根據專案用例圖用例點估算專案工時的方法
- Qt設定根據編譯器不同連結不同的lib庫QT編譯
- Spring Series---Springboot指定不同的啟動環境方法總結Spring Boot
- 使用 Docker 開發 PHP 專案(五):生產環境DockerPHP
- Windows 環境中使用 Git 上傳專案到 GitHubWindowsGithub
- WIN7環境使用nodejs快速搭建web專案Win7NodeJSWeb
- Windows 下如何使用 Linux 環境開發 PHP 專案!WindowsLinuxPHP
- SpringBoot使用JPA根據實體類自動生成相應表-mysqlSpring BootMySql
- nginx 判斷移動端或者 PC 端 進入不同專案Nginx
- vue專案打包配置多個測試環境與生產環境,用npm命令打出不同的資源包。VueNPM
- 如何根據生產環境mysql版本選擇合適的percona xtrabackup版本MySql
- WPF TextBlock根據值顯示不同的內容或格式BloC
- Springboot專案中使用KafkaSpring BootKafka
- 使用IDEA建立springboot專案IdeaSpring Boot