2020-08-14

springboot打包依赖包和配置文件分离

前言:springboot项目打包使用spring-boot-maven-plugin插件,默认会将依赖包和配置文件统统打进可执行jar文件中,使得jar文件太臃肿,则正式环境修改配置文件较麻烦,因此需要将配置文件和依赖jar包分离。如下:



    ${project.name}
    src/main/java
    
    
        
            src/main/resources
            
                **/*.properties
                **/*.xml
                **/*.yml
            
            false
            
            ${project.build.directory}/${project.name}/config
        
        
            src/main/bin
            ${project.build.directory}/${project.name}/bin
        
        
            
            
                
            
        
    
    
        
        
            org.apache.maven.plugins
            maven-jar-plugin
            
                
                    
                    false
                    
                        
                        true
                        
                        lib/
                        com.epri.dcloud.data.LoopcloseriskanalyzeApplication
                    
                    
                    
                        config/
                    
                
                
                    **/*.properties
                    **/*.sh
                    **/*.xml
                    **/*.yml
                
                ${project.build.directory}/${project.name}
            
        
        
        
            org.apache.maven.plugins
            maven-dependency-plugin
            
                
                    copy
                    package
                    
                        copy-dependencies
                    
                    
                        
                            ${project.build.directory}/${project.name}/lib
                        
                        false
                        false
                        runtime
                    
                
            
        
        
        
            org.apache.maven.plugins
            maven-surefire-plugin
            
                true
            
        
    

最后目录:

2020-08-14_第1张图片

bin目录下启动脚本

 内容:

#JDK所在路径
#JAVA_HOME="/home/dcenter/jdk1.8.0_91/bin/java"
#Java程序所在的目录(classes的上一级目录)
APP_HOME=$(dirname $(cd "$(dirname "$0")"; pwd))
cd ${APP_HOME}
#日志写入位置
LOG_HOME="${APP_HOME}/log"
#应用名
APP_NAME="loopcloser_risk"
#版本
APP_VERSION="1.0.0"
#拼凑完整的路径
#APP="${APP_HOME}/${APP_NAME}-${APP_VERSION}.jar"
APP="${APP_HOME}/${APP_NAME}.jar"
echo ${APP}
#mkdir -p ${LOG_HOME}
#cd ${LOG_HOME}
#java虚拟机启动参数
#JAVA_OPTS=" -Dfile.encoding=UTF8 -Duser.timezone=GMT+08 -ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m"
#JAVA_OPTS=" -ms512m -mx512m -Xmn256m -Djava.awt.headless=true -XX:MaxPermSize=128m"
JAR=$APP
prog=$APP_NAME
psid=0

checkpid() {
   javaps=`jps -l | grep $APP`
 
   if [ -n "$javaps" ]; then
      psid=`echo $javaps | awk '{print $1}'`
   else
      psid=0
   fi
}
start() {
   checkpid
 
   if [ $psid -ne 0 ]; then
      echo "================================"
      echo "warn: $APP_NAME already started! (pid=$psid)"
      echo "================================"
   else
      echo -n "Starting $APP_NAME ..."
      nohup java  -jar $APP >/dev/null  2>&1 &
      checkpid
      if [ $psid -ne 0 ]; then
         echo "(pid=$psid) [OK]"
      else
         echo "[Failed]"
      fi
   fi
}

stop() {
   

    checkpid
 
   if [ $psid -ne 0 ]; then
      echo -n "Stopping $APP_NAME ...(pid=$psid) "
       kill -9 $psid
      if [ $? -eq 0 ]; then
         echo "[OK]"
      else
         echo "[Failed]"
      fi
 
      checkpid
      if [ $psid -ne 0 ]; then
         stop
      fi
   else
      echo "================================"
      echo "warn: $APP_NAME is not running"
      echo "================================"
   fi
}

restart() {
   checkpid
 
   if [ $psid -ne 0 ];  then
      echo "$APP_NAME is running! (pid=$psid)"
   else
      echo "$APP_NAME is not running"
   fi
}

status() {
   pid=$(ps -ef | grep $JAR | grep -v 'grep ' | awk '{print $2}')

   if [  -e $pid ];then
      echo -e $"$prog hasn't run\t\t[OK]"
   else
      echo -e $"$prog is running\t\t[OK]"
   fi
}


case "$1" in
   start)
          start
          ;;
   stop)
          stop
          ;;
   status)
          status
          ;;
        restart)
                restart
                ;;

   *)
   echo -e "Usage: $0 {start|stop|status|restart}"
       exit 2
esac

启动:

bash ./bin/loopclose.sh start

 

你可能感兴趣的:(java)