springboot打包上线

发布到线上的包结构

runtime是发布到线上的目录结构
image
image

1.项目pom.xml添加打包配置

<build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>sigma.resource.sync.SigmaApplication</mainClass>
                    <layout>JAR</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <useDefaultDelimiters>true</useDefaultDelimiters><!--  这是重点-->
                </configuration>
                <executions>
                    <execution>
                        <id>copy-fatjar</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/build</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}</directory>
                                    <include>${project.build.finalName}.${project.packaging}</include>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy-bin</id>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/build/bin</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>src/main/bin</directory>
                                    <include>start.sh</include>
                                    <include>stop.sh</include>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>copy resource</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes</outputDirectory>
                            <overwrite>true</overwrite>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main</directory>
                                    <includes>
                                        <include>*</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/resources/public</directory>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>

2.创建脚本目录

src/main/bin

3.启动及停止脚本

src/main/bin/start.sh

#!/bin/bash

#----------以下变量可以在脚本中直接使用----------------
# $def_app_id           应用id
# $def_app_name         应用名称
# $def_app_domain       应用域名
# $def_app_deploy_path  兼容老部署,废弃
# $def_path_app_log     应用日志路径 如:/export/Logs/
# $def_path_app_data    如:/export/Data
# $def_group_id         分组 id
# $def_instance_id      实例id
# $def_instance_name    实例名称 server1
# $def_instance_path    实例完成路径 /export/Instances/jone/server1/
# $def_host_ip
#--------------------------

#set -o errexit
readonly APP_NAME="sigma-sync"          #定义当前应用的名称
readonly JAR_VERSION="1.0-SNAPSHOT"         #打包的JAR版本
#获取当前应用的进程 id
function get_pid
{
  pgrep -f "$EXE_JAR"
}

BASEDIR=$(cd $(dirname $0) && pwd)/..            # 获取运行脚本的上级目录

readonly JAVA_HOME="/export/servers/jdk1.8.0_60" # java home
readonly JAVA="$JAVA_HOME/bin/java"
APP_LOG_DIR="/export/Logs/sigma-sync"
mkdir -p ${APP_LOG_DIR}
EXE_JAR="$APP_NAME-$JAR_VERSION.jar"
echo "$EXE_JAR"
# FIXME: make this configurable
OPTS_MEMORY="-Xms2G -Xmx2G -server -XX:MaxPermSize=256M -Xss256K"       #定义启动 jvm 的参数信息。
CLASSPATH="$BASEDIR/"

[[ -z $(get_pid) ]] || {
    echo "ERROR:  $APP_NAME already running" >&2
    exit 1
}

echo "Starting $APP_NAME ...."
[[ -x $JAVA ]] || {
    echo "ERROR: no executable java found at $JAVA" >&2
    exit 1
}
cd $BASEDIR
setsid "$JAVA" $OPTS_MEMORY -jar "$EXE_JAR" "$@" > /dev/null 2>&1 &

sleep 0.5
[[ -n $(get_pid) ]] || {
    echo "ERROR: $APP_NAME failed to start" >&2
    exit 1
}

echo "$APP_NAME is up runnig :)"

src/main/bin/stop.sh

#!/bin/bash

set -o errexit
set -o nounset

readonly APP_NAME="sigma-sync"          #定义当前应用的名称
readonly JAR_VERSION="1.0-SNAPSHOT"         #打包的JAR版本
EXE_JAR="$APP_NAME-$JAR_VERSION.jar"
#获取当前应用的进程 id
function get_running_pid
{
  pgrep -f "$EXE_JAR"
}

readonly SELF_DIR=$(cd $(dirname $0) && pwd)

function stop
{
    local -i timeout=20
    local -i interval=1
    local -r service_pid=$(get_running_pid) || true # ignore error
    [[ -n $service_pid ]] || {
        echo "WARNING: process not found, nothing to stop" >&2
        exit 0
    }
    kill $service_pid
    while (( timeout > 0 )) && get_running_pid > /dev/null; do
        echo -n "."ƒ
        sleep $interval
        timeout=$(( timeout - interval ))
    done
    if get_running_pid > /dev/null; then
        echo "WARNING: process still alive, sending SIGKILL ..." >&2
        kill -9 "$service_pid"
    fi
}
function main
{
    get_running_pid > /dev/null || {
        echo "WARNING: process not found, nothing to stop" >&2
        exit 0  # Ignore error
    }
    stop
}
main "$@"

4.构建

mvn clean -U install -P prod -Dmaven.test.skip=true
posted @ 2021-03-12 15:14  SpecialSpeculator  阅读(127)  评论(0编辑  收藏  举报