springboot項目打包發布流程


1、概述

記錄jar包linux和windows發布全過程,源碼在gitee中托管。

本文使用assembly.xml進行打包,使用sh命令在linux中發布,使用bat命令在windows發布。

下面介紹整個過程

項目地址:https://gitee.com/mfengyu415/SpringBoot-Learn.git 的springboot-12-assembly這個module

2、創建項目

1、創建一個普通的springboot項目

2、添加HellowController

@RestController
public class HelloController {
    @RequestMapping("hello")
    public  String hello(){
      return  "hello";
    }
}

3、測試運行

3、maven-assembly-plugin配置

1、添加pom依賴

  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!-- 打tar.gz包 -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-targz</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/../release/tar.gz</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>${basedir}/assembly.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>

2、配置assembly.xml

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

    <id>release</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/main/script</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.sh</include>
                <include>*.bat</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/target</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>springboot-12-assembly*.jar</include>
            </includes>
        </fileSet>
        <!--devops僅用於開發和test環境, stage環境和線上環境會由部署系統自動生成devops並覆蓋-->
        <fileSet>
            <directory>${project.basedir}/src/main/resources/devops</directory>
            <outputDirectory>devops</outputDirectory>
            <includes>
                <include>*.properties</include>
            </includes>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <outputDirectory></outputDirectory>
            <source>${project.basedir}/src/main/resources/application.properties</source>
            <destName>application.properties</destName>
        </file>
    </files>

</assembly>

3、添加腳本

main下創建script文件夾,放sh和bat腳本

4、Linux的sh腳本

sh腳本使用notepad轉換為unix腳本,否則無法執行

1、啟動腳本(start.sh)

#!/bin/sh
source /etc/profile
base_dir=$(cd "$(dirname "$0")"; pwd)
jar_file=`ls $base_dir -t| grep "^springboot-12-assembly.*\.jar$"`
server_name="springboot-12-assembly"
launcher_daemon_out="server.out"

if [ ! -f "$jar_file" ]
then
    echo "can not found jar file , failed to start server! "
    exit 1
fi

pid=`ps -ef | grep "serverName=$server_name" | grep -v "grep" | awk '{print $2}'`

if [ "$pid" = "" ];then
     nohup java -DserverName=$server_name -Dbasedir=$base_dir -Djava.security.egd=file:/dev/./urandom -Dloader.path=. $jvm_args -jar $jar_file >"$launcher_daemon_out" 2>&1 < /dev/null  &
else
    echo "$server_name is running"
fi

2、停止腳本(stop.sh)

#!/bin/sh
server_name="springboot-12-assembly"
pid=`ps -ef | grep "serverName=$server_name" | grep -v "grep" | awk '{print $2}'`
if [ "$pid" = "" ]
then
    echo "$server_name is not running"
else
    kill -9 $pid
    echo "kill pid:$pid"
    echo "$server_name stop success"
fi

5、windows的bat腳本

1、啟動腳本(start.bat)

@echo off
%1 mshta vbscript:CreateObject("WScript.Shell").Run("%~s0 ::",0,FALSE)(window.close)&&exit
java -jar -Dfile.encoding=utf-8 springboot-12-assembly-0.0.1-SNAPSHOT.jar  >StartupLog.log  2>&1 &
exit

2、停止腳本(stop.bat)

echo  -------WechatApp stop--------
set port=8081
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (taskkill /f /pid %%m)
echo  -------WechatApp end------------------
@pause

6、打包

方法一、idea中直接運行maven的package

方法二、命令

mvn clean package -Dmaven.test.skip=true -Pstage  -s D:\mygit\SpringBoot-Learn\springboot-07-jpa\assembly.xml 

7、windows下部署

直接運行start.start后台運行輸入日志查看StartupLog.log

運行stop.bat命令結束運行

8、Linux下部署

運行start.sh后台運行,server.out查看運行日志
sh start.sh

運行stop.sh結束運行
sh stop.sh

9、linunx下開放端口

  1. 重新打開防火牆:
    systemctl start firewalld
  2. 添加8888端口的訪問權限,這里會永久生效
    firewall-cmd --zone=public --add-port=8888/tcp --permanent
  3. 重新載入防火牆,添加的端口才能生效
    firewall-cmd --reload
  4. 循環2-3步,添加想要的端口,然后關閉防火牆
    systemctl stop firewalld
  5. 查詢防火牆開放端口
    sudo iptables-save

10、測試

windows中直接瀏覽器打開

http://127.0.0.1:8082/hello 返回hello

linux下

curl http://127.0.0.1:8082/hello 返回hello

11、源碼地址

項目地址:https://gitee.com/mfengyu415/SpringBoot-Learn.git 的springboot-12-assembly這個module


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM