美文网首页
2018-09-15

2018-09-15

作者: 毛子果 | 来源:发表于2018-09-15 15:20 被阅读0次

springboot中自动化打包部署


目录

一、maven插件
二、目的
--1、打包成zip格式,目录结构
--2、将包上传到服务器,并且自动部署
三、pom.xml的配置
--1、打包时添加一些常用信息到META-INF/MANIFEST.MF
--2、assembly配置
--3、shell脚本
--4、自动化部署配置
四、执行命令,触发自动打包部署


一、maven插件

plugin function
maven-jar-plugin maven 默认打包插件,用来创建 project jar,负责将应用程序打包成可执行的jar文件。可在此处设置主类,manifest,排除对应的配置文件等
maven-assembly-plugin 支持定制化打包方式,负责将整个项目按照自定义的目录结构打成最终的压缩包,方便实际部署。可在此处设置打包拷贝路径,配置,以及打包好的jar文件等
wagon-maven-plugin 将包上传到服务器,并支持定制shell命令执行

二、目的

1、打包成zip格式,目录结构:

原码的目录结构

原目录结构.PNG
目标的目录结构
打包后的目录结构.PNG
2、将包上传到服务器,并且自动部署

三、pom.xml的配置

1、打包时添加一些常用信息到META-INF/MANIFEST.MF(添加Class-Path信息,设置主类MainClass)

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <excludes>
            <exclude>**/*.properties</exclude>
            <exclude>**/assembly.xml</exclude>
        </excludes>
        <archive>
            <index>1</index>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.zhengjia.Application</mainClass>
            </manifest>
            <manifestEntries>
                <mode>development</mode>
                <Class-Path>conf/</Class-Path>
                <version>${project.version}</version>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
2、assembly配置

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.5</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <descriptors>
            <descriptor>src/main/resources/assembly.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>copy-scripts</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

assembly.xml

<assembly>
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>

    <dependencySets>
        <dependencySet>
            <useProjectArtifact>true</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <fileSet>
            <directory>src/main/bin/</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>startServer.sh</include>
            </includes>
            <fileMode>0755</fileMode>
            <lineEnding>unix</lineEnding>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/</directory>
            <outputDirectory>conf/</outputDirectory>
            <includes>
                <include>application.properties</include>
                <include>application-dev.properties</include>
                <include>application-pro.properties</include>
            </includes>
            <fileMode>0755</fileMode>
        </fileSet>
    </fileSets>
</assembly>
3、shell脚本
#!/usr/bin/env bash
#@Copyright (C) Locket, Inc.

SERVER_NAME=zhengjia-api
#脚本文件名称
FINDNAME=$0
#检查脚本是否存在
#FINDNAME=`ls -ld $FINDNAME | awk '{print $NF}'` #查找文件,管道后取最后一个字段作为文件名
while [ -h $FINDNAME ] ; do FINDNAME=`ls -ld $FINDNAME | awk '{print $NF}'` ; done
APP_HOME=`echo $FINDNAME | sed -e 's@/[^/]*$@@'`
unset FINDNAME

if [ "$APP_HOME" = '.' ]; then
   APP_HOME=$(echo `pwd` | sed 's/\/bin//')
else
   APP_HOME=$(echo $APP_HOME | sed 's/\/bin//')
fi

echo "APP_HOME: $APP_HOME"

# Create pid file
# -d 检测文件是否是目录,如果是,则返回 true。
if [ ! -d "$APP_HOME"/pids ]; then
    mkdir "$APP_HOME"/pids
fi

HEAP_MEMORY=512m
MAX_HEAP_MEMORY=2048m
PERM_MEMORY=256m
MAX_PERM_MEMORY=512m

PIDFILE="$APP_HOME"/pids/$SERVER_NAME.pid
#echo "PIDFILE = $PIDFILE"

start() {
    if test -e "$PIDFILE"
    then
        if test -d "/proc"/$(cat "$PIDFILE")
        then
            echo "$SERVER_NAME service still running, please stop it first"
            exit 1
        fi
    else
        touch "$PIDFILE"
    fi

    if test -e "$APP_HOME"/$SERVER_NAME.jar
    then
        echo  "starting $SERVER_NAME ... "
        JAVA_OPTS="-server -XX:+HeapDumpOnOutOfMemoryError "

        shift
        ARGS=($*)
        for ((i=0; i<${#ARGS[@]}; i++)); do
            case "${ARGS[$i]}" in
            -D*)    JAVA_OPTS="${JAVA_OPTS} ${ARGS[$i]}" ;;
            -Heap*) HEAP_MEMORY="${ARGS[$i+1]}" ;;
            -Perm*) PERM_MEMORY="${ARGS[$i+1]}" ;;
            esac
        done
        JAVA_OPTS="${JAVA_OPTS} -Xms${HEAP_MEMORY} -Xmx${MAX_HEAP_MEMORY} -XX:PermSize=${PERM_MEMORY} -XX:MaxPermSize=${MAX_PERM_MEMORY} -Dapp.name=$SERVER_NAME"
        echo "start jvm args ${JAVA_OPTS}"
        nohup java $JAVA_OPTS -Dfile.encoding=UTF-8 -Duser.dir="$APP_HOME" -jar "."/$SERVER_NAME.jar >/dev/null 2>&1 &
        echo $! > "$PIDFILE"
        echo "started $SERVER_NAME OK"
    else
        echo "could not find $SERVER_NAME"
        exit 1
    fi
}

stop() {

    if test -e "$PIDFILE"
    then
        echo "stopping server"
        if kill -TERM `cat "$PIDFILE"`
        then
            sleep 2
            loop_check_process_status 3
        elif
            kill -KILL `cat "$PIDFILE"`
        then
            sleep 2
            loop_check_process_status 3
        elif
            kill -9 `cat "$PIDFILE"`
        then
            echo "server stop OK"
            /bin/rm "$PIDFILE"
        fi
    else
        echo "no server running"
        exit 1
    fi
}

restart() {
    echo "restarting server"
    stop
    start "$1"
}

info() {
    echo "System Information:"
    echo "****************************"
    echo `head -n 1 /etc/issue`
    echo `uname -a`
    echo
    echo "JAVA_HOME=$JAVA_HOME"
    echo `$JAVA_HOME/bin/java -version`
    echo
    echo "APP_HOME=$APP_HOME"
}


loop_check_process_status() {
    COUNT=$1
    KILLED=false
    for (( i = 0; i < $COUNT; i++));do
        if test -d "$APP_HOME"/$(cat "$PIDFILE")
        then
            sleep 10
        else
            KILLED=true
            break
        fi
    done
    if $KILLED; then
        echo "server stop OK"
        /bin/rm "$PIDFILE"
    else
        echo "server stop failed"
    fi
}
case $1 in
start)
    start "$2"
    ;;

stop)
    stop
    ;;

restart)
    restart "$2"
    ;;
info)
    info
    ;;
*)
    echo "Usage: $0 {start|stop|restart}" >&2
    ;;

esac

exit 0

使用./startServer.sh (start|stop|restart)

4、自动化部署配置

pom.xml

<dependency>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>wagon-maven-plugin</artifactId>
    <version>1.0</version>
</dependency>



<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>wagon-maven-plugin</artifactId>
    <version>1.0</version>
    <configuration>
        <serverId>webserver</serverId>
        <fromFile>${project.build.directory}/${project.artifactId}-${project.version}.zip</fromFile>
        <url>scp://ip:port/bigscreen-api/zhengjia-api/</url>
        <commands>
            <command>cd /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}/;ls -al;./startServer.sh stop</command>
            <command>rm -rf /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}</command>
            <command>unzip /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version}.zip -d /bigscreen-api/zhengjia-api/</command>
            <command>cd /bigscreen-api/zhengjia-api/${project.artifactId}-${project.version};source /etc/profile;./startServer.sh start;ps -ef|grep ${project.artifactId}</command>
        </commands>
        <displayCommandOutputs>true</displayCommandOutputs>
    </configuration>
</plugin>




<extensions>
    <extension>
        <groupId>org.apache.maven.wagon</groupId>
        <artifactId>wagon-ssh</artifactId>
        <version>2.10</version>
    </extension>
</extensions>

maven中的setting

<server>
    <id>webserver</id>
    <username>root</username>
    <password>*********</password>
</server>

四、执行命令,触发自动打包部署

mvn clean package wagon:upload-single wagon:sshexec

相关文章

  • 面试:JavaScript进阶篇

    JavaScript前端2018-09-15 18:13:32 作者:yuxiaolian 链接:https://...

  • 学习强国|大国名医:张仲景

    张仲景 “学习强国”学习平台2018-09-15 张仲景(约公元150~154年——约公元215~219年),名机...

  • 2018-09-18

    去逛街 庞芸熙 修改一 2018-09-15 19:27 · 字数 356 · 阅读 20 · 日记本 星期天,...

  • 做精致女人&享性福人生

    时间:2018-09-15 女人,本该精致如花 女人,本该幸福快乐 女人,本该了解自己 女人,你应该爱你自己。 今...

  • 附录C:SQL 语言的分类

    时间:2018-09-15 作者:魏文应 一、分 类 SQL 语言,对数据进行增、删、改、查操作。这样,也就意味...

  • 应该改变的是企业家的”心“

    2018-09-15 (稻盛哲学学习会)打卡第117天 姓名:祝新华 部门:业务部 组别:待定 【知~学习】...

  • 电影记录

    碟中谍6:全面瓦解 2018-09-15 我不是妖神 208-07-17 超人总动员2 2018-06-23 头...

  • 逻辑思维(五)

    2018-09-15 D48【读书感悟】3173-安安 第3章:利用框架来节约时间和精力 1、框架(framewo...

  • 张苗的周检视

    2018-09-15 星期六 本周温馨时刻及点滴成果 一、健康:本周坚持了40公里的运动,感觉非常不错。下...

  • 喜欢的文章21

    中年如秋 春暖花开 2018-09-15 16:48:35 当秋叶落在时光雕刻的脉络里,季节里已满是秋的韵味了。 ...

网友评论

      本文标题:2018-09-15

      本文链接:https://www.haomeiwen.com/subject/lqnzgftx.html