发布jar到指定目录
添加maven插件
需要使用maven-antrun-plugin插件把jar包发布到release/lib目录下
<build> <finalName>build-app</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--把包发布到应用的build目录下面和项目名相应的目录下面--> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <copy todir="release/lib/" overwrite="true"> <fileset dir="${project.build.directory}" erroronmissingdir="false"> <include name="*.jar"/> </fileset> </copy> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build>
执行打包命令
在应用根路径下面,执行以下命令
mvn clean package
可以看到jar包已经发布到了release的lib目录下
启动脚本
linux启动脚本
脚本内容
只需要修改SERVER_NAME属性即可使用脚本
server.sh脚本内容如下
#:!/bin/bash BASE_DIR=$(cd $(dirname $0);cd ..; pwd) echo "Welcome enter $BASE_DIR" cd $BASE_DIR #启动的jar存放路径(需要换成你jar包的位置) SERVER_NAME=build-app.jar SERVER_PATH="lib/${SERVER_NAME}" #程序的外部配置文件路径,这里使用了相对路径 CONFIG=conf/application.yml #日志文件的路径 LOG_CONFIG=conf/logback-spring.xml #nohup命令的日志输出路径, CONSOLE_LOG_OUT=logs/server-console.log #设置最小堆内存 最大堆内存 年轻代堆内存 初始化元空间内存 最大元空间内存 (元空间是jdk1.8才有的,jdk1.8之前叫永久代,jvm知识请阅读方志明老师写的深入理解Java虚拟机) JAVA_OPT='-Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m' #设置内存溢出打印堆日志 JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/heapdump.hprof" #设置GC输出日志 ,测试环境可以打印,生产环境需要注释掉 JAVA_OPT="${JAVA_OPT} -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:${BASE_DIR}/logs/gc.log" #设置启动程序需要指定的spring配置信息 JAVA_OPT="${JAVA_OPT} -jar ${SERVER_PATH} " JAVA_OPT="${JAVA_OPT} --spring.config.location=${CONFIG}" JAVA_OPT="${JAVA_OPT} --logging.config=${LOG_CONFIG}" if [ ! -d "logs" ] ; then echo 'mkdir logs dir' mkdir -p "logs" fi function start() { PID=$(ps -ef | grep $SERVER_NAME | grep -v grep | awk '{ print $2 }') if [ -z "$PID" ] then echo will start ... else echo "Start fail, app runing. at $CURRENT_DIR, pid=$PID" exit 1 fi echo '-----print JAVA_OPT------' echo $JAVA_OPT #把启动命令里的 >$CONSOLE_LOG_OUT 2>&1 & 换成 >dev/null & 可以让nohup不输出启动日志,改成>dev/null的话记得把 tail -f nohup java $JAVA_OPT >$CONSOLE_LOG_OUT 2>&1 & #查看实时的启动日志 tail -f $CONSOLE_LOG_OUT } function stop() { PID=$(ps -ef | grep $SERVER_NAME | grep -v grep | awk '{ print $2 }') if [ -z "$PID" ] then echo Application is already stopped else echo kill $PID kill $PID fi } #根据启动脚本的第1个参数决定调用哪个方法 # 启动 start,停止 stop, 重启 restart case $1 in start) shift 1 start $@ ;; stop) shift 1 stop ;; restart) shift 1 stop sleep 3 start $@ ;; esac
启动
#修改server.sh脚本,把SERVER_NAME改成你的jar包的名称 vi bin/server.sh #给刚上传到服务器的脚本文件分配执行的权限 chmod 777 bin/server.sh #启动程序 bin/server.sh start
停止
bin/server.sh stop
重启
重启命令相当于 stop和start 的组合命令
bin/server.sh restart
window脚本
启动脚本
bin/startup.cmd
只需要修改SERVER_NAME属性,然后双击此脚本即可启动程序
@echo off @rem ----------------------------------------------------------------------------- @rem Start script for the app Server @rem ----------------------------------------------------------------------------- setlocal enabledelayedexpansion set errorlevel= title build-app server set CONFIG=conf/application.yml set LOG_CONFIG=conf/logback-spring.xml rem 此处需要修改成你的jar包 set SERVER_NAME=build-app.jar set SERVER_PATH=lib/%SERVER_NAME% set JAVA_OPT=-Xms512m -Xmx512m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m set JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=logs/heapdump.hprof set JAVA_OPT=%JAVA_OPT% -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps -Xloggc:logs/gc.log set JAVA_OPT=%JAVA_OPT% -jar %SERVER_PATH% set JAVA_OPT=%JAVA_OPT% --spring.config.location=%CONFIG% set JAVA_OPT=%JAVA_OPT% --logging.config=%BASE_DIR%%LOG_CONFIG% cd /d "%~dp0" cd .. set BASE_DIR=%~dp0 rem Check if Java is correctly installed and set java -version 1>nul 2>nul if !errorlevel! neq 0 ( @echo Please install Java 1.8 or higher and make sure the Java is set correctly. @echo You can execute command [ java -version ] to check if Java is correctly installed and set. pause goto:eof ) @echo Please do not close the current window. @echo -----print JAVA_OPT------ @echo %JAVA_OPT% java %JAVA_OPT% @echo Stopped %SERVER_JAR% pause goto:eof
双击release/bin/startup.cmd启动程序
停止脚本
shutdown.cmd
需要把脚本中的build-app换成你jar包的名称,然后双击此脚本既可以停止应用
@echo off if not exist "%JAVA_HOME%injps.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! & EXIT /B 1 setlocal set "PATH=%JAVA_HOME%in;%PATH%" echo killing build-app server for /f "tokens=1" %%i in ('jps -m ^| find "build-app"') do ( taskkill /F /PID %%i ) echo Done!