java项目构建部署启动脚本

mvn打包pom配置

pom.xml
<build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*include>
                includes>
                <filtering>falsefiltering>
            resource>
        resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>2.7.16version>
                plugin>
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-assembly-pluginartifactId>
                    <version>3.1.0version>
                plugin>
            plugins>
        pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-assembly-pluginartifactId>
                <configuration>
                    <finalName>packageNamefinalName>
                    <appendAssemblyId>falseappendAssemblyId>
                    <descriptors>
                        <descriptor>assembly/assembly.xmldescriptor>
                    descriptors>
                configuration>
                <executions>
                    <execution>
                        <id>make-assemblyid>
                        <phase>packagephase>
                        <goals>
                            <goal>singlegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
assembly.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/3.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/3.1.0 http://maven.apache.org/xsd/assembly-3.1.0.xsd">
    <id>releaseid>
    <formats>
        <format>tar.gzformat>
    formats>
    <fileSets>
        <fileSet>
            <directory>target/目录路径directory>
            <outputDirectory>输出目录路径outputDirectory>
            
            <fileMode>0644fileMode>
        fileSet>
    fileSets>
    <files>
        <file>
            <source>${project.basedir}/target/项目启动.jarsource>
            <destName>输出文件路径destName>
        file>
    files>
assembly>
构建部署脚本
NOW_HOME=$PWD
mvn clean install -Dmaven.test.skip=true;
mvn package -Dmaven.test.skip=true;

NOW_DATA=`date "+%Y-%m-%d+%H:%M:%S"`
# 项目名称;打包文件夹名称;解压缩根目录名称;项目运行目录名称
PROJECT_NAME=
# 已经打包的工程文件;包含配置文件;启动jar等
TARGET_TAR_NAME=`$PROJECT_NAME-$NOW_DATA.tar.gz`
# 需要部署到的服务器ssh登陆用户和地址
TARGET_SERVER=[email protected]
# 编译后的assembly打包好的压缩包路径
COMPLIE_TAR_PATH=$NOW_HOME/编译后的assembly打包好的压缩包路径
# 部署服务器上备份历史运行包的路径
TARGET_BACKUP_DIR=/app/bak
# 解压缩根目录名称
DECP_DIR_NAME=$PROJECT_NAME
# 部署服务器解压项目包后的目录路径
DECP_DIR_PATH=$TARGET_BACKUP_DIR$DECP_DIR_NAME
# 部署服务运行项目的目录
RUN_PROJECT_DIR=/app/$PROJECT_NAME

# 在目标端创建出历史编译工程备份文件夹
ssh $TARGET_SERVER "mkdir -p $TARGET_BACKUP_DIR";
# 把编译好的压缩包发送到目标端
scp -r $COMPLIE_TAR_PATH $TARGET_SERVER:$TARGET_BACKUP_DIR/$TARGET_TAR_NAME
# 在目标端把编译包解压到备份文件夹
ssh $TARGET_SERVER "tar -zxvf $TARGET_BACKUP_DIR/$TARGET_TAR_NAME -C $TARGET_BACKUP_DIR";
# 先把停止项目脚本复制到运行目录
ssh $TARGET_SERVER "cp -rf $DECP_DIR_PATH/stop.sh $RUN_PROJECT_DIR/";
# 执行停止脚本;停掉之前正在运行的工程
ssh $TARGET_SERVER "cd $RUN_PROJECT_DIR; sh stop.sh";
# 然后重命名旧的的工程目录
ssh $TARGET_SERVER "mv $RUN_PROJECT_DIR $RUN_PROJECT_DIR-OLD";
# 把新工程目录移动重命名为运行工程目录
ssh $TARGET_SERVER "mv $DECP_DIR_PATH $RUN_PROJECT_DIR";
# 启动新工程
ssh $TARGET_SERVER "cd $RUN_PROJECT_DIR; sh start.sh";
# 删除旧工程目录
ssh $TARGET_SERVER "rm -rf $RUN_PROJECT_DIR-OLD";
启动脚本 start.sh
#!/bin/bash
sleep 1
function start() {
    cd "$1"
	DIR_BASE="$1"
	BootStrap="$1/$2"
	JVM_SIZE=''
	YOUNG_SIZE=''
	NEW_SIZE=''
	DIR_LIB=$DIR_BASE/lib
	JAVA_OPTS='-Dfile.encoding=UTF-8 -Dsun.rmi.dgc.client.gcInterval=600000  -Dsun.rmi.dgc.server.gcInterval=600000 -Dnet.sf.ehcache.enableShutdownHook=true'

    #if test -e $DIR_LIB;then
    #    JAVA_OPTS="$JAVA_OPTS -cp $DIR_LIB"
    #fi

	if [ "$JVM_SIZE" = '' ];then
	  JVM_SIZE=$(free -m | awk '$2 > 0 && NR==2 {print (($2/128) - ($2/128%1)) * 64 }') > /dev/null
	fi
	if [ $JVM_SIZE -gt 4096 ];then
	  JVM_SIZE=4096
	fi

	YOUNG_SIZE=$(($JVM_SIZE / 4))
	NEW_SIZE=$(($YOUNG_SIZE / 2))

	JVM_OPTS=" \
-server \
-Xms${JVM_SIZE}M \
-Xmx${JVM_SIZE}M \
-Xmn${YOUNG_SIZE}M \
-XX:NewSize=${NEW_SIZE}M \
-Xss1M \
-XX:+PrintHeapAtGC \
-XX:SurvivorRatio=8 \
-XX:+PrintGCDetails \
-XX:+PrintFlagsFinal \
-XX:MetaspaceSize=64m \
-XX:+PrintGCTimeStamps \
-XX:+PrintGCDateStamps \
-XX:+DisableExplicitGC \
-XX:+UseConcMarkSweepGC \
-XX:PrintFLSStatistics=1 \
-XX:MaxMetaspaceSize=512m \
-XX:+PrintCommandLineFlags \
-XX:+UnlockDiagnosticVMOptions \
-XX:+PrintTenuringDistribution \
-XX:-HeapDumpOnOutOfMemoryError \
-XX:+PrintGCApplicationStoppedTime \
-XX:+PrintGCApplicationConcurrentTime"

	touch nohup.out && sudo chmod 755 *
	REMOTE_DEBBUG_CMD='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=14567'
	echo "nohup java $REMOTE_DEBBUG_CMD $JVM_OPTS $JAVA_OPTS -jar $BootStrap > $DIR_BASE/nohup.out 2>&1 &"
	nohup java $REMOTE_DEBBUG_CMD $JVM_OPTS $JAVA_OPTS -jar $BootStrap > $DIR_BASE/nohup.out 2>&1 &
}

CUR_DIR=$(dirname $0)
cd $CUR_DIR

JAR_NUM=$(ls *jar | awk -F " " '{print $1}' | wc -l)
if [ $JAR_NUM != 1 ]; then
    echo "check jar file name failed, there is more or less than one jar file."
    exit 1
fi

BOOT_STRAP=$(ls *jar | awk -F " " '{print $1}')
RUN_PID=$(ps -wef | grep java | grep -i $BOOT_STRAP | grep -v grep | awk -F " " '{printf $2" "}')

if test -z "$RUN_PID";then
    start "$CUR_DIR" "$BOOT_STRAP"
else
    echo "The Service already starting."
    echo "$RUN_PID"
fi
停止脚本 stop.sh
#!/bin/bash
CUR_DIR=$(dirname $0)
cd $CUR_DIR

JAR_NUM=$(ls *jar | awk -F " " '{print $1}' | wc -l)
if [ $JAR_NUM != 1 ]; then
    echo "check jar file name failed, there is more or less than one jar file."
    exit 1
fi

BOOT_STRAP=$(ls *jar | awk -F " " '{print $1}')
RUN_PID=$(ps -wef | grep java | grep -i $BOOT_STRAP | grep -v grep | awk -F " " '{printf $2" "}')

if test -z "$RUN_PID"
then
    echo "The Service already stopped."
else
    echo "$RUN_PID"
    for pid in $RUN_PID
    do
        if ! test -z $pid
        then
            kill -9 "$pid"
        fi
    done
    echo "Stopped Service Success."
fi
exit 0

你可能感兴趣的:(java,开发语言)