Scala jar读取外部属性文件 [英] Scala jar read external properties file

查看:50
本文介绍了Scala jar读取外部属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些代码并将其导出为jar文件.在这个jar中,有一个名为automation.properties的文件,默认情况下我正在使用该文件加载

I have written some code and exported it as a jar file. In this jar there is a file named automation.properties with defaults that I'm loading using

val automationPropertiesFileURL = getClass.getResource("/automation.properties")  
  if (automationPropertiesFileURL != null) {
    val source = Source.fromURL(automationPropertiesFileURL)
    config = new Properties()
    config.load(source.bufferedReader())
  }

但是当这个jar文件作为gradle依赖项添加到C:\User\abc\.gradle中并且我想从当前项目中读取automation.properties时,如何覆盖位置并从我的项目中而不是从项目中读取文件. jar文件本身?

But when this jar file gets added as a gradle dependency in C:\User\abc\.gradle and I want to read automation.properties from my current project, how can I override the location and read the file from my project and not from the jar file itself?

推荐答案

类加载器将从其首先找到的位置加载文件.

The class loader will load the file from the location it finds first.

对于您而言,该文件存在于两个位置:

In your case, the file exists in two places:

  • 在当前项目本身内部
  • jar依赖项内部
  • Inside the current project itself
  • Inside a jar dependency

类加载器会找到哪个文件, 取决于当前项目"的顺序以及对 classpath jar依赖关系. 那就是你需要复习的 这是加载正确文件的关键.

Which file will be found by the class loader, depends on the ordering of the "current project" and the jar dependency on the classpath. That's what you need to review, that's the key to loading the right file.

您当前的代码是正确的, 这是类路径配置的问题.

Your current code is correct as it is, this is a matter of classpath configuration.

这篇关于Scala jar读取外部属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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