概述
1.运行脚本
在项目的主工程src/main/script目录下放置如下运行脚本
setenv.sh 设置JDK环境变量脚本
#JDK目录
export JAVA_HOME=/home/develop/jdk1.8
boot.sh 运行脚本,注意更改里面的项目名SERVER_NAME
#!/bin/bash
# 项目名
SERVER_NAME='***-server'
#sh执行文件目录
cd `dirname $0`
BIN_DIR=`pwd`
#配置文件目录
cd ..
DEPLOY_DIR=`pwd`
#工作目录
CONF_DIR=$DEPLOY_DIR/config
#服务启动方法
start(){
echo 'start '${SERVER_NAME}
# jar名称
JAR_NAME=${SERVER_NAME}'.jar'
# 获取应用的端口号
SERVER_PORT=`sed -nr '/server:(n) port: [0-9]+/ s/.*port: +([0-9]+).*/1/p' config/application.yml`
#获取进程ID
PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" |awk '{print $2}'`
#防止重复启动
if [ -n "$PIDS" ]; then
echo "ERROR: The $SERVER_NAME already started!"
echo "PID: $PIDS"
exit 1
fi
#检查端口是否被占用
if [ -n "$SERVER_PORT" ]; then
SERVER_PORT_COUNT=`netstat -tln | grep $SERVER_PORT | wc -l`
if [ $SERVER_PORT_COUNT -gt 0 ]; then
echo "ERROR: The $SERVER_NAME port $SERVER_PORT already used!"
exit 1
fi
echo 1
fi
# 设置JAVA_HOME
if [ -r "$DEPLOY_DIR/bin/setenv.sh" ]; then
. "$DEPLOY_DIR/bin/setenv.sh"
fi
if [ ! -n "$JAVA_HOME" ]; then
echo "JAVA_HOME not found"
exit 0
fi
echo Using JAVA_HOME: $JAVA_HOME
JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
JAVA_DEBUG_OPTS=""
if [ "$1" = "debug" ]; then
JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
fi
JAVA_JMX_OPTS=""
if [ "$1" = "jmx" ]; then
JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
fi
JAVA_MEM_OPTS=""
BITS=`java -version 2>&1 | grep -i 64-bit`
if [ -n "$BITS" ]; then
JAVA_MEM_OPTS=" -server -Xmx512m -Xms512m -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
else
JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
fi
CONFIG_FILES=" -Dlogging.config=$CONF_DIR/logback-spring.xml -Dspring.config.location=$CONF_DIR/application.yml "
echo -e "Starting the $SERVER_NAME ..."
if [ "$1" = "console" -o "$2" = "console" ]; then
echo "$JAVA_HOME/bin/java ${JAVA_OPTS} $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -DDEPLOY_DIR=$DEPLOY_DIR -jar $DEPLOY_DIR/boot/$JAR_NAME"
$JAVA_HOME/bin/java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -DDEPLOY_DIR=$DEPLOY_DIR -jar $DEPLOY_DIR/boot/$JAR_NAME
else
echo "nohup $JAVA_HOME/bin/java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -DDEPLOY_DIR=$DEPLOY_DIR -jar $DEPLOY_DIR/boot/$JAR_NAME >>../logs/$SERVER_NAME.logs 2>&1 &"
nohup $JAVA_HOME/bin/java $JAVA_OPTS $JAVA_MEM_OPTS $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS $CONFIG_FILES -DDEPLOY_DIR=$DEPLOY_DIR -jar $DEPLOY_DIR/boot/$JAR_NAME >>../logs/$SERVER_NAME.log 2>&1 &
fi
PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
if [ ${PIDS} ]; then
echo "start OK"
echo "PID: $PIDS"
else
echo "start FAIL"
fi
}
stop(){
PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
if [ ${PIDS} ]; then
if [ "$1" = "force" ]; then
#强制停止
echo 'Stop '$SERVER_NAME' Process...'
kill -9 $PIDS
else
#优雅的停止
echo 'Stop '$SERVER_NAME' Process...'
kill -9 $PIDS
fi
else
echo $SERVER_NAME' is NOT running.'
exit 0
fi
flag=true
while ((${flag}))
do
sleep 2
PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
if [ ! -n ${PIDS} ]; then
flag=false
fi
done
echo 'Stop '$SERVER_NAME' SUCC'
}
status(){
PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
#查看进程状态
if [ -n "$PIDS" ]; then
echo "The $SERVER_NAME is running...!"
echo "PID: $PIDS"
exit 0
else
echo "The $SERVER_NAME is stopped"
exit 0
fi
}
restart(){
PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | awk '{print $2}'`
if [ ${PIDS} ]; then
stop
fi
start
}
case $1 in
start)
start $2 $3
;;
stop)
stop $2
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: boot.sh start|stop|status|restart"
;;
esac
2.配置文件
该打包方式需要依赖于assembly.xml配置文件
将assembly.xml文件放置于项目的主工程src/main/assembly目录下
<?xml version="1.0" encoding="UTF-8"?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- 可自定义,这里指定的是项目环境 -->
<!-- spring-boot-assembly-local-1.0.RELEASE.tar.gz -->
<id>${profileActive}-${project.version}</id>
<!-- 打包的类型,如果有N个,将会打N个类型的包 -->
<formats>
<!-- <format>tar.gz</format> -->
<format>zip</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!-- 0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限; 0644->即用户具有读写权限,组用户和其它用户具有只读权限; -->
<!-- 将src/main/scripts目录下的所有文件输出到打包后的bin目录中 -->
<fileSet>
<!-- 指定目标文件的目录 -->
<directory>${basedir}/src/main/scripts</directory>
<!-- 指定目标文件的(打包)输出目录 -->
<outputDirectory>bin</outputDirectory>
<!-- 文件赋予可运行、读、写权限 -->
<fileMode>0755</fileMode>
<includes>
<!-- 指定加入打包文件 -->
<include>**.sh</include>
<include>**.bat</include>
</includes>
</fileSet>
<!-- 指定输出target/classes中的配置文件到config目录中 -->
<fileSet>
<directory>${basedir}/target/classes</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>application.yml</include>
<include>logback-spring.xml</include>
</includes>
</fileSet>
<!-- 将第三方依赖打包到lib目录中 -->
<fileSet>
<directory>${basedir}/target/lib</directory>
<outputDirectory>lib</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<!-- 将项目启动jar打包到boot目录中 -->
<fileSet>
<directory>${basedir}/target</directory>
<outputDirectory>boot</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
<!-- 包含根目录下的文件 -->
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>NOTICE</include>
<include>LICENSE</include>
<include>*.md</include>
</includes>
</fileSet>
</fileSets>
</assembly>
项目主工程 pom.xml
<build>
<!-- 打包后的启动jar名称 -->
<finalName>center-server</finalName>
<plugins>
<!-- 配置assembly.xml文件路径 -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<!-- 对应assembly.xml文件存放路径 -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 排除启动jar包中依赖的jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
<includes>
<!-- 项目启动jar包中排除依赖包 -->
<include>
<groupId>non-exists</groupId>
<artifactId>non-exists</artifactId>
</include>
</includes>
</configuration>
</plugin>
<!-- 打jar包配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- 项目启动类 -->
<mainClass>xxx.xxx.xxx.Application</mainClass>
<!-- 依赖的jar的目录前缀 -->
<classpathPrefix>../lib</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
<Class-Path>../config</Class-Path>
</manifestEntries>
</archive>
<includes>
<!-- 只打包指定目录的文件 -->
<include>xxx/xxx/hh/**</include>
<include>templates/**</include>
</includes>
</configuration>
</plugin>
<!-- 复制项目的依赖包到指定目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<!-- 配置maven编译版本,可选 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
配置完成后可直接使用maven命令 mvn clean package或通过idea的maven插件进行打包,打包完成后会在对应工程的target目录下生成 xxx.zip 文件,里面就包括
config 配置文件
boot jar包
bin 脚本文件
lib 依赖jar包
3.项目部署
将生成的 *.zip文件上传到服务器上,通过unzip *.zip 解压。
如果提示unzip命令找不到,可通过 yum -y install unzip 进行下载
解压完毕后,直接运行bin目录下的boot.sh脚本进行项目的启动,具体命令如下:
sh boot.sh start #启动项目
sh boot.sh stop #启动项目
sh boot.sh status #启动项目
sh boot.sh restart #重启项目
最后
以上就是暴躁钥匙为你收集整理的Springboot maven 打jar包,分离资源、配置、依赖,及部署脚本的全部内容,希望文章能够帮你解决Springboot maven 打jar包,分离资源、配置、依赖,及部署脚本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复