使用-jar选项时,我设法在外部路径中加载了application.properties文件。
关键是PropertiesLauncher。
要使用PropertiesLauncher,必须按以下方式更改pom.xml文件:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- added --> <layout>ZIP</layout> <!-- to use PropertiesLaunchar --> </configuration> </plugin> </plugins></build>
构建jar文件之后,通过检查jar中meta-INF / MENIFEST.MF中的Main-
Class属性,可以看到使用了PropertiesLauncher。
现在,我可以按以下方式运行jar(在Windows中):
java -Dloader.path=file:///C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar
请注意,应用程序jar文件包含在loader.path中。
现在,已加载C: My External Dir config中的application.properties文件。
另外,Jar也可以访问该目录中的任何文件(例如,静态html文件),因为它位于加载程序路径中。
至于UPDATE 2中提到的非jar(扩展)版本,可能存在类路径顺序问题。



