本篇和你們分享的是springboot打包並結合shell腳本命令部署,重點在分享一個shell程序啓動工具,但願能便利工做;java
一般一套程序分爲了不少個部署環境:開發,測試,uat,線上 等,咱們要想對這些環境區分配置文件,能夠經過兩種方式:node
這裏咱們要講的是第二種,首先在mvn中配置以下內容:linux
1 <profiles> 2 <profile> 3 <id>node</id> 4 <properties> 5 <!--傳遞給腳本的參數值--> 6 <activeProfile>node</activeProfile> 7 <package-name>${scripts_packageName}</package-name> 8 <boot-main>${scripts_bootMain}</boot-main> 9 </properties> 10 <activation> 11 <activeByDefault>true</activeByDefault> 12 </activation> 13 </profile> 14 <profile> 15 <id>node1</id> 16 <properties> 17 <activeProfile>node1</activeProfile> 18 <package-name>${scripts_packageName}</package-name> 19 <boot-main>${scripts_bootMain}</boot-main> 20 </properties> 21 </profile> 22 <profile> 23 <id>node2</id> 24 <properties> 25 <activeProfile>node2</activeProfile> 26 <package-name>${scripts_packageName}</package-name> 27 <boot-main>${scripts_bootMain}</boot-main> 28 </properties> 29 </profile> 30 </profiles>
節點粗解:git
對於springboot程序打包,能夠分爲jar和war,這裏是jar包;有場景是咋們配置文件或者第三方等依賴包不想放到工程jar中,而且把這些文件壓縮成一個zip包,方便上傳到linux;此時經過maven-assembly-plugin和maven-jar-plugin就能夠作到,mvn的配置如:github
1 <plugin> 2 <groupId>org.apache.maven.plugins</groupId> 3 <artifactId>maven-jar-plugin</artifactId> 4 <version>2.6</version> 5 <configuration> 6 <archive> 7 <addMavenDescriptor>false</addMavenDescriptor> 8 <manifest> 9 <addClasspath>true</addClasspath> 10 <classpathPrefix>lib/</classpathPrefix> 11 <mainClass>${scripts_bootMain}</mainClass> 12 </manifest> 13 </archive> 14 <!--打包排除項--> 15 <excludes> 16 <exclude>**/*.yml</exclude> 17 <exclude>**/*.properties</exclude> 18 <exclude>**/*.xml</exclude> 19 <exclude>**/*.sh</exclude> 20 </excludes> 21 </configuration> 22 <executions> 23 <execution> 24 <id>make-a-jar</id> 25 <phase>compile</phase> 26 <goals> 27 <goal>jar</goal> 28 </goals> 29 </execution> 30 </executions> 31 </plugin> 32 33 <plugin> 34 <groupId>org.apache.maven.plugins</groupId> 35 <artifactId>maven-assembly-plugin</artifactId> 36 <version>2.4</version> 37 <!-- The configuration of the plugin --> 38 <configuration> 39 <!-- Specifies the configuration file of the assembly plugin --> 40 <descriptors> 41 <descriptor>${project.basedir}/src/main/assembly/assembly.xml</descriptor> 42 </descriptors> 43 </configuration> 44 <executions> 45 <execution> 46 <id>make-assembly</id> 47 <phase>package</phase> 48 <goals> 49 <goal>single</goal> 50 </goals> 51 </execution> 52 </executions> 53 </plugin>
值得注意的地方以下幾點:spring
有了上面mvn配置,咱們還須要assembly.xml的配置,這裏提取告終合shell腳本發佈程序的配置:shell
1 <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd 3 http://maven.apache.org/ASSEMBLY/2.0.0 "> 4 <id>${activeProfile}</id> 5 <!--打包成一個用於發佈的zip文件--> 6 <formats> 7 <format>zip</format> 8 </formats> 9 <!--true:zip中生成一級目錄(此處屏蔽,配合腳本須要profiles後綴)--> 10 <includeBaseDirectory>false</includeBaseDirectory> 11 <dependencySets> 12 <dependencySet> 13 <!--打包進zip文件的lib目錄--> 14 <useProjectArtifact>false</useProjectArtifact> 15 <outputDirectory>${package-name}-${activeProfile}/lib</outputDirectory> 16 <unpack>false</unpack> 17 </dependencySet> 18 </dependencySets> 19 20 <fileSets> 21 <!-- 配置文件打包進zip文件的conf目錄 --> 22 <fileSet> 23 <directory>${project.basedir}/src/main/profiles/${activeProfile}</directory> 24 <outputDirectory>${package-name}-${activeProfile}/conf</outputDirectory> 25 <includes> 26 <include>**/*</include> 27 <!--<include>*.xml</include>--> 28 <!--<include>*.properties</include>--> 29 <!--<include>*.yml</include>--> 30 </includes> 31 </fileSet> 32 33 <!--啓動腳本打包進zip文件--> 34 <fileSet> 35 <directory>${project.basedir}/src/main/scripts</directory> 36 <outputDirectory></outputDirectory> 37 <includes> 38 <include>**/*</include> 39 </includes> 40 <!-- 文件文件權限爲777 --> 41 <fileMode>777</fileMode> 42 <!-- 目錄權限爲777 --> 43 <directoryMode>777</directoryMode> 44 <!--腳本中參數變量爲pom中的值 關鍵--> 45 <filtered>true</filtered> 46 </fileSet> 47 48 <!-- 項目編譯出來的jar打包進zip文件 --> 49 <fileSet> 50 <directory>${project.build.directory}</directory> 51 <outputDirectory>${package-name}-${activeProfile}/</outputDirectory> 52 <includes> 53 <include>*.jar</include> 54 </includes> 55 </fileSet> 56 </fileSets> 57 </assembly>
重點節點介紹:apache
完成上面配置後,此時咱們能夠經過idea上勾選切換不一樣環境來打zip包,如圖:vim
上面步驟完成了zip格式的發佈包,咱們再分享下啓動程序的shell腳本,該腳本具備的功能如:windows
目前該shell中封裝了兩種啓動jar命令的方式:
如圖命令格式:
來看所有的shell代碼:
1 #!/usr/bin/env bash 2 #可變參數變量 3 languageType="javac" #支持 java,javac,netcore 發佈 4 #參數值由pom文件傳遞 5 baseZipName="${package-name}-${activeProfile}" #壓縮包名稱 publish-test.zip的publish 6 packageName="${package-name}" #命令啓動包名 xx.jar的xx 7 mainclass="${boot-main}" #java -cp啓動時,指定main入口類;命令:java -cp conf;lib\*.jar;${packageName}.jar ${mainclass} 8 9 #例子 10 # baseZipName="publish-test" #壓縮包名稱 publish-test.zip的publish 11 # packageName="publish" #命令啓動包名 publish.jar的xx 12 13 #固定變量 14 basePath=$(cd `dirname $0`/; pwd) 15 baseZipPath="${basePath}/${baseZipName}.zip" #壓縮包路徑 16 baseDirPath="${basePath}" #解壓部署磁盤路徑 17 pid= #進程pid 18 19 #解壓 20 function shenniu_unzip() 21 { 22 echo "解壓---------------------------------------------" 23 echo "壓縮包路徑:${baseZipPath}" 24 if [ ! `find ${baseZipPath}` ] 25 then 26 echo "不存在壓縮包:${baseZipPath}" 27 else 28 echo "解壓磁盤路徑:${baseDirPath}/${baseZipName}" 29 echo "開始解壓..." 30 31 #解壓命令 32 unzip -od ${baseDirPath}/${baseZipName} ${baseZipPath} 33 34 #設置執行權限 35 chmod +x ${baseDirPath}/${baseZipName}/${packageName} 36 37 echo "解壓完成。" 38 fi 39 } 40 41 #檢測pid 42 function getPid() 43 { 44 echo "檢測狀態---------------------------------------------" 45 pid=`ps -ef | grep -n ${packageName} | grep -v grep | awk '{print $2}'` 46 if [ ${pid} ] 47 then 48 echo "運行pid:${pid}" 49 else 50 echo "未運行" 51 fi 52 } 53 54 #啓動程序 55 function start() 56 { 57 #啓動前,先中止以前的 58 stop 59 if [ ${pid} ] 60 then 61 echo "中止程序失敗,沒法啓動" 62 else 63 echo "啓動程序---------------------------------------------" 64 65 #選擇語言類型 66 read -p "輸入程序類型(java,javac,netcore),下一步按回車鍵(默認:${languageType}):" read_languageType 67 if [ ${read_languageType} ] 68 then 69 languageType=${read_languageType} 70 fi 71 echo "選擇程序類型:${languageType}" 72 73 #進入運行包目錄 74 cd ${baseDirPath}/${baseZipName} 75 76 #分類啓動 77 if [ "${languageType}" == "javac" ] 78 then 79 if [ ${mainclass} ] 80 then 81 nohup java -cp conf:lib\*.jar:${packageName}.jar ${mainclass} >${baseDirPath}/${packageName}.out 2>&1 & 82 #nohup java -cp conf:lib\*.jar:${packageName}.jar ${mainclass} >/dev/null 2>&1 & 83 fi 84 elif [ "${languageType}" == "java" ] 85 then 86 nohup java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar >/dev/null 2>&1 & 87 # java -jar ${baseDirPath}/${baseZipName}/${packageName}.jar 88 elif [ "${languageType}" == "netcore" ] 89 then 90 #nohup dotnet run ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 & 91 nohup ${baseDirPath}/${baseZipName}/${packageName} >/dev/null 2>&1 & 92 fi 93 94 #查詢是否有啓動進程 95 getPid 96 if [ ${pid} ] 97 then 98 echo "已啓動" 99 #nohup日誌 100 tail -n 50 -f ${baseDirPath}/${packageName}.out 101 else 102 echo "啓動失敗" 103 fi 104 fi 105 } 106 107 #中止程序 108 function stop() 109 { 110 getPid 111 if [ ${pid} ] 112 then 113 echo "中止程序---------------------------------------------" 114 kill -9 ${pid} 115 116 getPid 117 if [ ${pid} ] 118 then 119 #stop 120 echo "中止失敗" 121 else 122 echo "中止成功" 123 fi 124 fi 125 } 126 127 #啓動時帶參數,根據參數執行 128 if [ ${#} -ge 1 ] 129 then 130 case ${1} in 131 "start") 132 start 133 ;; 134 "restart") 135 start 136 ;; 137 "stop") 138 stop 139 ;; 140 "unzip") 141 #執行解壓 142 shenniu_unzip 143 #執行啓動 144 start 145 ;; 146 *) 147 echo "${1}無任何操做" 148 ;; 149 esac 150 else 151 echo " 152 command以下命令: 153 unzip:解壓並啓動 154 start:啓動 155 stop:中止進程 156 restart:重啓 157 158 示例命令如:./shenniu_publish start 159 " 160 fi
正如上面小節說的,shell中的參數 package-name,activeProfile,boot-main 都是由mvn中profiles的properties中提供,是可變的參數,腳本代碼自己不須要人工去修改,只須要變的是mvn的參數便可;其實在咱們生成zip包的時候,shell中的參數就被替換了,能夠看zip中shell文件內容如:
把生成的zip上傳到linux上,經過命令解壓:
1 unzip -od eureka-server-0.0.1-node eureka-server-0.0.1-node.zip
其實shell腳本中包含有解壓命令,可是我在打包時放在了zip中,因此只能經過手動解壓了,固然能夠調整;此時進入加壓目錄如此:
注:這裏第一次執行./shenniu_publish.sh腳本時候,提示了錯誤信息;是因爲我是在windows上編輯的這個腳本,其空格等和linux上不同,因此運行會有問題,要解決可使用vim命令在linux把該文件轉成linux格式,以下命令:
1 vim shenniu_publish.sh 2 set ff=unix 3 :wq
執行完後,再來運行腳本./shenniu_publish.sh,此時有以下提示:
此刻咱們文件是解壓狀態,所以只須要start命令啓動程序便可:
到這裏shenniu_publish.sh腳本使用就完成了,只要腳本沒有提示錯誤,基本都能啓動jar服務;其餘restart和stop命令也如此執行就行:
能夠去研究下shell代碼,但願該腳本能給你帶來效率和好的學習思路,下面是測試用例git地址,腳本在eureka-server項目中:https://github.com/shenniubuxing3/springcloud-Finchley.SR2