目录结构:
config
application.properties
redis.yml
xxxx.jar
项目中使用getResouce方式获取redis.yml。发现读取到的是jar包内的配置。
URL resource = RedissonConfig.class.getClassLoader().getResource("redis.yml");
尝试方案:
1、修改代码,有效
URL resource = RedissonConfig.class.getClassLoader().getResource("file:config/redis.yml");
2、不修改代码,启动脚本中增加 -Dloader.path=${BASE_DIR}/config,无效 。
排查最后发现是springboot打包的方式不对。
最开始的pom.xml
<build>
<finalName>xxxxxx</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
,修改后的配置,增加了layout
<build>
<finalName>xxxxx</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
第一种方式生成的jar包里面MANIFEST.MF的Main-Class是JarLauncher
第二种方式生成的jar包里面MANIFEST.MF的Main-Class是PropertiesLauncher
使用-Dloader.path需要在打包的时候增加<layout>ZIP</layout>,不指定的话-Dloader.path不生效
参考:SpringBoot 打包插件spring-boot-maven-plugin打包机制及内部结构分析_build-jdk-spec_Archie_java的博客-CSDN博客