资源文件夹在以JAR文件形式运行时找不到,而外部文件夹可以找到。

huangapple 未分类评论60阅读模式
标题翻译

Resource folder not found when running as a jar while external folder can be found

问题

我在Spring Boot 1.5.1 RELEASE中遇到了这个问题。
在我的属性中,我有一个属性config-dir。使用资源处理程序,我可以通过以下方式添加我的配置文件夹:

public PortalAppConfig(@Value("${config-dir}") String configDir) {
    this.configDir = configDir;
}

public void addResourceHandlers(final ResourceHandlerRegistry registry) {
    registry
        .addResourceHandler("/assets/config/**")
        .addResourceLocations(String.format("file:%s", configDir));
}

当我将config-dir声明为外部文件夹,例如config-dir: ${user.home}/externalConfigFolder/时,一切正常工作。
然而,我不知道如何指向JAR文件中的目录,以便可以使用JAR文件中的目录。我尝试过classpath:static/assets/configstatic/assets/config以及许多其他变体。
以下图片显示了资源和目标的目录树:

资源文件夹在以JAR文件形式运行时找不到,而外部文件夹可以找到。
资源文件夹在以JAR文件形式运行时找不到,而外部文件夹可以找到。

英文翻译

I have this problem in spring boot 1.5.1RELEASE
In my properties i have got a property config-dir. Using resource handler I can add my config folder by:

public PortalAppConfig(@Value("${config-dir}") String configDir) {
   this.configDir = configDir;
 }
 
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
   registry
       .addResourceHandler("/assets/config/**")
       .addResourceLocations(String.format("file:%s", configDir));
 }

When I declare config-dir as an external folder, like config-dir: ${user.home}/externalConfigFolder/ everything works fine.
However I have no idea how to point path to that directory, so It could use directory from JAR file. I tried
classpath:static/assets/config, static/assets/config and many other variations.
Those pictures shows directory tree for both resources and target

I know that the question sound similar to https://stackoverflow.com/questions/25869428/classpath-resource-not-found-when-running-as-jar
but in my case I do not want to 'use' a file inside a jar, but point to the whole directory (using path as a String) and add it to resourceHandler.
I am building project using maven. Java 8.

资源文件夹在以JAR文件形式运行时找不到,而外部文件夹可以找到。
资源文件夹在以JAR文件形式运行时找不到,而外部文件夹可以找到。

huangapple
  • 本文由 发表于 2020年3月16日 16:32:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/60702593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定