springboot打zip包运行linux环境中

1.添加打包配置文件
1.1 assembly.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/bin</directory>
            <outputDirectory>/bin</outputDirectory>
            <fileMode>0777</fileMode>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/conf</directory>
            <outputDirectory>/conf</outputDirectory>            
            <fileMode>0644</fileMode>
        </fileSet>
    </fileSets>
</assembly>

1.2 serverStatus.sh

#!/bin/sh
if [ ! -n "$JAVA_HOME" ]; then
   export JAVA_HOME="/export/server/jdk1.8.0_141"
fi
APP_MAIN=${application.main.class}
PID=0

getPID(){
   javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAIN`
   if [ -n "$javaps" ]; then
       PID=`echo $javaps | awk '{print $1}'`
   else
       PID=0
   fi
}

getServerStatus(){
   getPID
   echo "================================================================================================================"
   if [ $PID -ne 0 ]; then
       echo "$APP_MAIN is running(PID=$PID)"
       echo "================================================================================================================"
   else
       echo "$APP_MAIN is not running"
       echo "================================================================================================================"
   fi
}
getServerStatus

1.3 shutdown.sh

#!/bin/sh
if [ ! -n "$JAVA_HOME" ]; then
    export JAVA_HOME="/export/server/jdk1.8.0_141"
fi
APP_MAIN=${application.main.class}
PID=0

getPID(){
    javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAIN`
    if [ -n "$javaps" ]; then
        PID=`echo $javaps | awk '{print $1}'`
    else
        PID=0
    fi
}

shutdown(){
    getPID
    echo "================================================================================================================"
    if [ $PID -ne 0 ]; then
        echo -n "Stopping $APP_MAIN(PID=$PID)..."
        kill -9 $PID
        if [ $? -eq 0 ]; then
            echo "[Success]"
            echo "================================================================================================================"
        else
            echo "[Failed]"
            echo "================================================================================================================"
        fi
        getPID
        if [ $PID -ne 0 ]; then
            shutdown
        fi
    else
        echo "$APP_MAIN is not running"
        echo "================================================================================================================"
    fi
}

shutdown

1.4 startup.sh

#!/bin/sh
#-------------------------------------------------------------------------------------------------------------
#该脚本的使用方式为-->[sh startup.sh]
#该脚本可在服务器上的任意目录下执行,不会影响到日志的输出位置等
#-------------------------------------------------------------------------------------------------------------
if [ ! -n "$JAVA_HOME" ]; then
    export JAVA_HOME="/export/server/jdk1.8.0_141"
fi

#-------------------------------------------------------------------------------------------------------------
#       系统运行参数
#-------------------------------------------------------------------------------------------------------------
DIR=$(cd "$(dirname "$0")"; pwd)
APP_HOME=${DIR}/..
CLASSPATH=$APP_HOME/conf
APP_LOG=${APP_HOME}/logs
APP_CONFIG=${APP_HOME}/conf/application.yml
APP_MAIN=${application.main.class}

JAVA_OPTS="$JAVA_OPTS -server -Xms512m -Xmx512m -Xmn128m -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:MaxGCPauseMillis=850 -XX:+PrintGCDetails -Xloggc:$APP_LOG/gc.log -Dfile.encoding=UTF-8"
JAVA_OPTS="$JAVA_OPTS -DlogPath=$APP_LOG"
JAVA_OPTS="$JAVA_OPTS -Dconf.config=file:${APP_CONFIG}"

echo "JAVA_HOME="$JAVA_HOME
echo "CLASSPATH="$CLASSPATH
echo "JAVA_OPTS="$JAVA_OPTS

#-------------------------------------------------------------------------------------------------------------
#   程序开始
#-------------------------------------------------------------------------------------------------------------
for appJar in "$APP_HOME"/lib/*.jar;
do
   CLASSPATH="$CLASSPATH":"$appJar"
done
PID=0

getPID(){
    javaps=`$JAVA_HOME/bin/jps -l | grep $APP_MAIN`
    if [ -n "$javaps" ]; then
        PID=`echo $javaps | awk '{print $1}'`
    else
        PID=0
    fi
}

startup(){
    getPID
    echo "================================================================================================================"
    if [ $PID -ne 0 ]; then
        echo "$APP_MAIN already started(PID=$PID)"
        echo "================================================================================================================"
    else
        echo -n "Starting $APP_MAIN"
         if [ ! -d "$APP_LOG" ]; then
            mkdir "$APP_LOG"
         fi
        nohup $JAVA_HOME/bin/java $JAVA_OPTS -classpath $CLASSPATH $APP_MAIN &
        for i in $(seq 5)
        do
        sleep 0.8
        echo -e  ".\c"
        done
        getPID
        if [ $PID -ne 0 ]; then
            echo "(PID=$PID)...[Success]"
            echo "================================================================================================================"
        else
            echo "[Failed]"
            echo "================================================================================================================"
        fi
    fi
}

startup

1.5 run.bat

title face-server
@echo off 
rem ##############设置延迟环境变量扩充,即感叹号间的值不会因跳出循环而为空值。################
setlocal enabledelayedexpansion 

rem ###############java命令######################
set JAVA=%JAVA_HOME%\bin\java.exe 

rem ###############jvm参数######################
set OPTS=-Xms512M -Xmx512M -XX:+AggressiveOpts -XX:+UseParallelGC -XX:NewSize=64M 

rem ###############agent启动类参数######################
set serverMain=cn.micropattern.face.Application

echo JAVA: %JAVA% 
echo CLASSPATH: %CP% 
echo OPTS: %OPTS%
java %OPTS%  -cp "../lib/*;../conf" %serverMain% 
PAUSE
image.png

1.6 pom.xml依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test</groupId>
    <artifactId>springboot-zip</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <application.main.class>com.test.Application</application.main.class>
        <spring.boot.version>1.5.9.RELEASE</spring.boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>${spring.boot.version}</version>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                    <include>*.yml</include>
                    <include>*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>scripts/*</exclude>
                    <exclude>*.properties</exclude>
                    <exclude>*.yml</exclude>
                    <exclude>*.xml</exclude>
                </excludes>
            </resource>

            <!-- 收集运行脚本 -->
            <resource>
                <directory>src/main/resources/scripts</directory>
                <targetPath>${project.build.directory}/bin</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>*.sh</include>
                    <include>*.bat</include>
                </includes>
            </resource>
            <!-- 收集配置文件 -->
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}/conf</targetPath>
                <filtering>true</filtering>
                <includes>
                    <include>*.properties</include>
                    <include>*.yml</include>
                    <include>*.xml</include>
                </includes>
            </resource>

            <!-- 收集手动导入的jar包 -->
            <resource>
                <directory>lib</directory>
                <targetPath>${project.build.directory}/lib</targetPath>
            </resource>
        </resources>
        <plugins>
            <!-- 1.用于编译的plugin -->
            <!-- <plugin> -->
            <!-- <groupId>org.apache.maven.plugins</groupId> -->
            <!-- <artifactId>maven-compiler-plugin</artifactId> -->
            <!-- <version>3.1</version> -->
            <!-- <configuration> -->
            <!-- <fork>true</fork> -->
            <!-- <source>1.8</source> -->
            <!-- <target>1.8</target> -->
            <!-- <encoding>UTF-8</encoding> -->
            <!-- </configuration> -->
            <!-- </plugin> -->

            <!-- 2.用于生成jar包的plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                    <excludes>
                        <exclude>*.xml</exclude>
                        <exclude>*.yml</exclude>
                        <exclude>*.properties</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!-- 3.用于拷贝maven依赖(lib)的plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- 4.用于拷贝resource文件夹 的plugin -->

            <!-- 5.配置生成源代码jar的plugin[这里的源代码是指java文件,不是class文件] -->
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <attach>true</attach>
                    <!-- 配置源代码jar文件的存放路径 -->
                    <outputDirectory>${project.build.directory}</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- 打包zip -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>dfrp-portal</id>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>${application.main.class}</mainClass>
                                </manifest>
                            </archive>
                            <descriptors>
                                <descriptor>src/main/resources/scripts/assembly.xml</descriptor>
                            </descriptors>
                            <!-- 将依赖jar包都包含在目标jar中 -->
                            <!-- <descriptorRefs> -->
                            <!-- <descriptorRef>jar-with-dependencies</descriptorRef> -->
                            <!-- </descriptorRefs> -->
                            <finalName>${project.name}-${version}</finalName>
                        </configuration>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

2.打包 install -Dmaven.test.skip=true


image.png

执行完命令后zip包在target文件路径下生成

3.linux启动服务

image.png

上传并解压zip包

image.png

————————————————
版权声明:本文为CSDN博主「奔奔老王」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lao_wang_tou/article/details/109483465

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,992评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,212评论 3 388
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,535评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,197评论 1 287
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,310评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,383评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,409评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,191评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,621评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,910评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,084评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,763评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,403评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,083评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,318评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,946评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,967评论 2 351

推荐阅读更多精彩内容