1. 程式人生 > >spring boot 打包和部署

spring boot 打包和部署

這兩天專案剛剛寫完準備測試,專案是用Springboot搭建的,一個project和三個module,分別是API(用來其他系統的呼叫,包括前端)、service(內含service層、dao層和mapper以及mybatis的xml檔案)和job(任務排程的module),其中API依賴service和job。在父類和API中新增如下的啟動項,而在父類中不用處理,因為這是在API中進行的打包操作:

  <build>
    <plugins>
      <!--提供mvn命令直接執行springboot-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <excludes>
          <exclude>application.properties</exclude>
          <exclude>application-dev.properties</exclude>
          <exclude>application-online.properties</exclude>
          <exclude>application-test.properties</exclude>
          <exclude>logback-spring.xml</exclude>
        </excludes>
      </resource>
      <resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
        <includes>
          <include>application.properties</include>
          <include>application-${profileActive}.properties</include>
          <include>logback-spring.xml</include>
        </includes>
      </resource>
    </resources>
  </build>

執行在idea的控制檯執行mvn clean package Ptest這個mvn命令,打包test環境的配置檔案,將專案打成jar包。

jar包打包完成後將jar包上傳到Linux的系統壞境,寫一個啟動指令碼,執行下邊的指令碼就可以將專案啟動:

#!/bin/bash
nohup java -jar yourapp.jar &
echo Asset is already started!

&符號的意思是程式在後臺執行,nohup是不掛斷的執行命令,這樣就可以保證程式執行的時候始終保持。需要注意的是,將指令碼檔案在文字中編輯後上傳到Linux中後,執行時會出現以下這兩個錯誤:

stop.sh: line 11: syntax error: unexpected end of file
: command not found

這是由於格式不合符要求導致的,因此要設定格式,設定格式的命令如下:

vi start.sh
:set fileformat=unix
:wq

下邊是stop.sh指令碼:

#!/bin/bash
PID=$(ps -ef | grep yourappp.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Asset is already stopped!
else
    echo kill $PID
    kill $PID
	echo Asset stopped!
fi