使用springBoot項目配置文件位置調(diào)整到打包外
項目目錄

問題痛點:
當(dāng)我們打包一個項目的時候,springboot打包的jar都是把resouce下的配置文件打進去了,這樣就沒發(fā)修改配置文件
解決方案
- 1.打包的時候指定打包路徑
- 2.將配置文件從resouce下面移出來
這兩種方案其實都涉及到一個maven打包配置問題
首先來談?wù)剬⑴渲梦募膔esouce下面移出來怎么解決
1在項目src同級別目錄建
config目錄
2.將resouce下的
application.yaml 文件移到config目錄下方便打包后可以配置application文件中相關(guān)配置
pom.xml中的打包配置
<build>
<resources>
<resource>
<directory>config</directory>
<includes>
<include>*.yaml</include>
<include>*.xml</include>
<include>*.conf</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<!--過濾掉的 -->
<!-- <excludes>-->
<!-- <exclude>**/*.properties</exclude>-->
<!-- <exclude>**/*.xml</exclude>-->
<!-- <exclude>**/*.yml</exclude>-->
<!-- </excludes>-->
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions></executions>
<configuration>
<!-- 生成可執(zhí)行的jar的名字:xxx-exec.jar -->
<!-- 不固定,寫成abcd都可以 -->
<classifier>exec</classifier>
<mainClass>com.cloudwise.douc.zabbix.api.DoucZabbixApplication</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<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>
</plugins>
</build>
assembly.xml配置
<assembly
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>bin</id>
<formats>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory>./</outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<fileSet>
<directory>config</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
<fileSet>
<directory>bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>777</fileMode>
</fileSet>
<fileSet>
<directory>target</directory>
<outputDirectory>lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<!--是否把本項目添加到依賴文件夾下-->
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<!--將scope為runtime的依賴包打包-->
<!--<scope>runtime</scope>-->
</dependencySet>
</dependencySets>
</assembly>
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
總結(jié):
當(dāng)打包過程中出現(xiàn)各種問題后,及時的去查找問題,一般注意pom中的配置打包是否沒有把某些包打進去還有就是啟動sell腳本通過 ./Aplication.sh start -d 顯示啟動日志
到此這篇使用springBoot項目配置文件位置調(diào)整到打包外文章就介紹到這了,更多相關(guān)springBoot項目配置文件位置調(diào)整到打包外的內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot接收excel數(shù)據(jù)文件去重方式
文章主要介紹了如何在Spring?Boot中實現(xiàn)文件上傳并入庫的功能,包括讀取Excel文件、生成Entity對象、使用MergeInto語句進行數(shù)據(jù)庫操作以及注意事項2024-12-12
Spring中@PropertySource的使用方法和運行原理詳解
這篇文章主要介紹了Spring中@PropertySource的使用方法和運行原理詳解,PropertySource注解可以方便和靈活的向Spring的環(huán)境容器(org.springframework.core.env.Environment?Environment)中注入一些屬性,這些屬性可以在Bean中使用,需要的朋友可以參考下2023-11-11
SpringBoot中配置多數(shù)據(jù)源的方法詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot中配置多數(shù)據(jù)源的方法的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
將InputStream轉(zhuǎn)化為base64的實例
這篇文章主要介紹了將InputStream轉(zhuǎn)化為base64的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
PowerJob的AliOssService工作流程源碼解讀
這篇文章主要介紹了PowerJob的AliOssServiceg工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
spring cloud gateway網(wǎng)關(guān)路由分配代碼實例解析
這篇文章主要介紹了spring cloud gateway網(wǎng)關(guān)路由分配代碼實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01

