从外部模块访问资源文件 [英] Accessing resource files from external modules

查看:97
本文介绍了从外部模块访问资源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到非模块化java为止,你只需将文件放在 src / main / java / resources 中,确保它在classpath中,然后用

  file = getClass()。getClassLoader()。getResourceAsStream(myfilename);从$ class 

p>现在有了模块,情节会变粗。



我的项目设置如下:

  module playground.api {
需要java.base;
需要java.logging;
需要framework.core;
}

配置文件放在 src / main / resources中/config.yml



项目运行

  java -p target / classes:target / dependency -m framework.core / com.framework.Main 

由于主类不在我自己的项目中,但在外部框架模块中,它无法看到 config.yml 。现在的问题是,有没有办法以某种方式将我的配置文件放入模块或打开它?我是否必须更改框架上游加载文件的方式?



我尝试在module-info中使用exports或opens,但它想要一个包名,而不是文件夹名。



如何以最佳实用方式实现这一点,以便它在Java 8中工作并尽可能少地进行更改?

解决方案

在使用 java 命令启动应用程序,如下所示: -

  java -p target / classes:target / dependency -m framework.core / com.framework.Main 




  • 您使用选项 -p aternate为 - module-path 指定模块路径会查找模块的 target / classes target / dependency


  • 另外,使用 -m 替代 - 模块使用名称 framework.core 指定要解析的初始模块,并构造模块图,并使用主类明确地将其列为 com.framework.Main




现在,问题这里似乎模块 framework.core 需要或读取 playground。 api 模块,因为模块图不包含由实际资源组成的所需模块 config.yml



@Alan建议,在启动期间列出模块分辨率输出的一种好方法是使用 - show-module-resolution 选项。 / p>





我只是天真地尝试打开src / main / resources,不编译ofc


由于资源在您的模块位于根级别,因此, 未封装 ,不需要打开或导出到任何其他模块。



在您的情况下,您只需要确保模块 playground.api 在模块图中结束然后应用程序可以访问该资源。要指定除初始模块之外要解析的根模块,您可以使用 - add-modules 选项。






因此,为您工作的整体解决方案以及一些调试应为:

  java --module-path target / classes:target / dependency 
--module framework.core / com.framework.Main
--add-modules playground.api
- -show-module-resolution


So far until non-modularized java, you would simply put a file in src/main/java/resources make sure it is in classpath and then load it with

file = getClass().getClassLoader().getResourceAsStream("myfilename"); 

from pretty much anywhere in the classpath.

Now with modules, the plot thickens.

My project setup is the following:

module playground.api {
    requires java.base;
    requires java.logging;
    requires framework.core;
}

Config file is placed inside src/main/resources/config.yml.

Project is run with

java -p target/classes:target/dependency -m framework.core/com.framework.Main

Since the main class does not reside in my own project, but in an external framework module, it can't see config.yml. Now the question is, is there a way to somehow put my config file into the module or open it up? Do I have to change the way file is loaded by framework upstream?

I tried using "exports" or "opens" in module-info but it wants to have a package name, not a folder name.

How to achieve this in best practical way so it would work as in Java 8 and with as little changes as possible?

解决方案

While you are using the java command to launch an application as follows:-

java -p target/classes:target/dependency -m framework.core/com.framework.Main 

  • you are specifying the modulepath using the option -p aternate for --module-path which would look up into target/classes and target/dependency for your modules.

  • Alongside, using -m alternate for --module specifies the initial module to resolve with the name framework.core and constructs the module graph with the main class to execute explicitly listed as com.framework.Main.

Now, the problem here seems to be that the module framework.core doesn't requires or read playground.api module because of which the module graph doesn't include the desired module consisting of the actual resource config.yml.

As suggested by @Alan, a good way to list out the module resolution output during startup is to make use of the --show-module-resolution option.


I just naively tried to opens src/main/resources, doesn't compile ofc

Since the resource in your module is at the root level, it is, therefore, not encapsulated and does not need to be opened or exported to any other module.

In your case, you just need to make sure that the module playground.api ends up in the module graph and then the resource would be accessible to the application. To specify root modules to resolve in addition to the initial module, you can make use of the --add-modules option.


Hence the overall solution to work for you along with some debugging shall be :

java --module-path target/classes:target/dependency 
     --module framework.core/com.framework.Main
     --add-modules playground.api
     --show-module-resolution

这篇关于从外部模块访问资源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆