[英]What is the classpath of message.properties if in dependency jar
[英]Extract properties file from JAR dependency and use it in own classpath
我想在我的Maven项目中使用一个小程序作为依赖项。 该程序由属性文件配置,可以在执行前对其进行编辑。 到目前为止,我已经在本地存储库中将依赖项添加为JAR。
现在,我想在我自己的上级类路径中访问该依赖项的属性文件,即在myprogram/src/main/resources/config/myprogram.properties
而不是在myprogram/local-repository/com/example/mydependency/mydependency.jar/mydependency.properties
。
我试图修改定义属性文件路径的代码部分:
public example() {
Properties prop = new Properties();
InputStream input = example.class.getResourceAsStream("/config/myprogram.properties");
prop.load(input);
...
}
我还删除了依赖项的原始属性文件,然后将其添加到本地存储库中。
整个程序正在运行,但没有按预期进行。 奇怪的是,既没有使用/src/main/resources/config
新属性文件,也没有使用mydependency.jar
的旧属性文件。 似乎某种默认属性文件已放入我的最终胖JAR文件中。 但是,即使在尝试调试时,也无法在任何地方找到它的源。 该默认属性文件似乎似乎无处不在。
现在,如何正确地将依赖项的属性文件移动到我自己的类路径中?
缺省属性文件从何处出现?
这是Maven还是源代码本身的问题?
提前致谢!
您可以执行以下操作:
public static String getValueFromDependencyProps(ClassLoader cl, String propertiesFile, String key) throws IOException {
Properties prop = new Properties();
prop.load(cl.getResourceAsStream(propertiesFile));
String value = prop.getProperty(key);
if (value == null)
throw new NullPointerException();
else
return value;
}
接着:
String value = getValueFromDependencyProps(YourDependencyClass.class.getClassLoader(), "your.properties", key);
经过测试和工作。 这样,我就可以从外部Maven依赖项访问任何属性文件(使用其类之一YourDependencyClass
来访问它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.