使用springBoot项目配置文件位置调整到打包外

项目目录

使用springBoot项目配置文件位置调整到打包外_第1张图片

问题痛点:

当我们打包一个项目的时候,springboot打包的jar都是把resouce下的配置文件打进去了,这样就没发修改配置文件

解决方案

  • 1.打包的时候指定打包路径
  • 2.将配置文件从resouce下面移出来

这两种方案其实都涉及到一个maven打包配置问题

首先来谈谈将配置文件从resouce下面移出来怎么解决

1在项目src同级别目录建

config目录

2.将resouce下的

application.yaml 文件移到config目录下方便打包后可以配置application文件中相关配置

pom.xml中的打包配置

 
        
            
                config
                
                    *.yaml
                    *.xml
                    *.conf
                
                true
            
            
                src/main/resources

                
                
                
                
                
                false
            
        
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                
                    
                    
                    exec
                    com.cloudwise.douc.zabbix.api.DoucZabbixApplication
                
            
            
                org.apache.maven.plugins
                maven-assembly-plugin
                
                    
                        src/main/assembly/assembly.xml
                    
                
                
                    
                        make-assembly
                        package
                        
                            single
                        
                    
                
            
        
    

assembly.xml配置


    bin
    
        tar.gz
    
    true
    
        
            ${project.basedir}
            ./
            
                README*
                LICENSE*
                NOTICE*
            
        
        
            config
            config
        
        
            bin
            bin
            777
        
        
            target
            lib
            
                *.jar
            
        
    
    
        
            
            true
            lib
            
            
        
    

sh启动类shell脚本

#!/bin/bash
PWDPATH=`dirname $0`
COMM_HOME=`cd $PWDPATH && cd .. && pwd`
cd $COMM_HOME
start () {
    JVM_OPTS="
     -server
     -Xms1g
     -Xmx1g
     -XX:+AlwaysPreTouch
     -XX:+UseG1GC
     -XX:MaxGCPauseMillis=2000
     -XX:GCTimeRatio=4
     -XX:InitiatingHeapOccupancyPercent=30
     -XX:G1HeapRegionSize=8M
     -XX:ConcGCThreads=2
     -XX:G1HeapWastePercent=10
     -XX:+UseTLAB
     -XX:+ScavengeBeforeFullGC
     -XX:+DisableExplicitGC
     -XX:+PrintGCDetails
     -XX:-UseGCOverheadLimit
     -XX:+PrintGCDateStamps
     -Xloggc:logs/gc.log
     -Dlog4j2.configurationFile=config/log4j2.xml
    "
    export CLASSPATH=$JAVA_HOME/jre/lib/*:$JAVA_HOME/lib/*:$COMM_HOME/lib/*
#    启动类路径
    export MAINCLASS="com.gug.api.AdimApplication"
    case $1 in
    -b )
        nohup java $JVM_OPTS -cp $CLASSPATH $MAINCLASS 1>/dev/null 2>&1 &
    ;;
    -d )
        java $JVM_OPTS -classpath $CLASSPATH $MAINCLASS
    ;;
    esac
    echo -e '\r'
}
case $1 in
restart )
    echo stop
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "The current process does not exist."
    else
       kill $PID
       echo "The process has been successfully stopped."
    fi
    echo start
    if [ ! -n "$2" ] ;then
	echo "After start, you must add parameters -d or -b. See help for details."
    else
        start $2 -b
    fi
;;
start )
    echo start
    if [ ! -n "$2" ] ;then
	echo "After start, you must add parameters -d or -b. See help for details."
    else
        start $2
    fi
;;
stop )
    echo stop
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "The current process does not exist."
    else
       kill $PID
       echo "The process has been successfully stopped."
    fi
;;
pid )
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "The current process does not exist."
    else
       echo "pid : "${PID}
    fi
;;
status )
    PID=`ps avx|grep $COMM_HOME|grep -v 'grep'|awk '{print $1}'`
    if  [ ! -n "$PID" ] ;then
       echo "dead"
    else
       echo "running"
    fi
;;
help )
    echo 'start    -d or -b     Start the service DEBUG mode or background mode.'
    echo 'stop                  Stop the service running in the background.'
    echo 'pid                   Gets the current process id information.'
    echo 'status                Gets the current service status information.'
;;
* )
    echo Command error, please enter help
;;
esac

总结:

当打包过程中出现各种问题后,及时的去查找问题,一般注意pom中的配置打包是否没有把某些包打进去还有就是启动sell脚本通过 ./Aplication.sh start -d 显示启动日志

到此这篇使用springBoot项目配置文件位置调整到打包外文章就介绍到这了,更多相关springBoot项目配置文件位置调整到打包外的内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(使用springBoot项目配置文件位置调整到打包外)