如何使用 Spring Boot 在类路径上首先放置目录? [英] How to put a directory first on the classpath with Spring Boot?
问题描述
我正在使用 Maven 构建我的 Spring Boot 应用程序,因此我可以使用以下命令启动它:
I am building my Spring Boot application using Maven, so I can start it with:
java -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
我想首先在类路径上有一个目录,这样我就可以在文件系统上放置一些文件,而无需解压缩 jar 来更改它们.
I want to have a directory first on the classpath that would allow me to place some files on the filesystem without having to unzip the jar to change them.
我曾尝试使用 loader.path
,但它似乎不起作用.
I have tried using loader.path
, but it does not seem to work.
java -Dloader.path="config/*" -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod
config
目录是 jar 所在的子目录.我正在尝试加载一个密钥库文件,该文件在我的应用程序中作为 Resource
注入.src/main/resources 中有这样一个文件,但它只在我的 IDE 中有效,在打包为 jar 时无效.所以我想把一个文件放在类路径的第一个,以便在类路径中最先找到那个文件.
The config
dir is a subdirectory of where the jar is located. I am trying to load a keystore file which is injected as a Resource
in my application. There is such a file in the src/main/resources, but that only works in my IDE, not when packaged as a jar. So I want to put a file first on the classpath so that that one is found first on the classpath.
推荐答案
您可以使用 loader.path
但前提是 Main-Class
是 PropertiesLauncher
(所以这取决于您如何构建 JAR 文件).也许您需要在 Boot 插件中使用 Packaging=ZIP 重新构建 JAR(例如 文档在这里)?您不能将密钥库的路径设置为文件:"网址吗?
You can use loader.path
but only if the Main-Class
is PropertiesLauncher
(so it depends how you built the JAR file). Maybe you need to re-build the JAR with packaging=ZIP in the Boot plugin (e.g. docs here)? Can you not set the path to the keystore as a "file:" URL?
这篇关于如何使用 Spring Boot 在类路径上首先放置目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!