[英]Loading resources from multiple jars with wild card
I have a Java application with multiple modules, each module has a jar
file. 我有一个带有多个模块的Java应用程序,每个模块都有一个jar
文件。 each jar file follows same folder structure called META-INF/props
Is there a way in java to load all the property files which are in `META-INF/props of multiple jars using a wild card? 每个jar文件都遵循称为META-INF/props
相同文件夹结构,java中是否可以使用通配符加载多个jar的`META-INF / props中的所有属性文件?
Something like 就像是
ClassLoader.getSystemResourceAsStream("META-INF/props/*.properties");
I know that this method does not accept wild cards and does not return array of streams , but is it possible to do something like this? 我知道此方法不接受通配符,并且不返回流数组,但是可以这样做吗?
No, there is no standard/reliable way to do this. 不,没有标准/可靠的方法可以做到这一点。 Some libraries take advantage of common patterns of ClassLoader.getResources implementations (specifically, that they usually always return "file:" or "jar:file:" URLs) in order to support wildcards in resource lookups. 某些库利用ClassLoader.getResources实现的常见模式(特别是它们通常总是返回“ file:”或“ jar:file:” URL)来支持资源查找中的通配符。 For example, the Wildcards in application context constructor resource paths explains how Spring does this, and it lists several caveats ("Implications on portability", "Classpath*: portability", "notes relating to wildcards"). 例如, 应用程序上下文构造函数资源路径中的通配符解释了Spring如何做到这一点,并列出了一些警告(“对可移植性的影响”,“ Classpath *:可移植性”,“与通配符有关的注释”)。
I wrote some code to get around this this limitation. 我写了一些代码来解决这个限制。
I read all entries from class path and determine if it is a folder or a JAR file and then look for the entries in "META_INF/props". 我从类路径中读取所有条目,并确定它是文件夹还是JAR文件,然后在“ META_INF / props”中查找条目。 If the entry is a property, I load it. 如果条目是属性,则将其加载。
Below is the code, it is not refined but it gives general idea. 下面是代码,虽然没有精炼,但给出了总体思路。
try{
URL[] urls = ((URLClassLoader) LoaderTest.class.getClassLoader()).getURLs();
HashMap<String, Properties> mapProperties = new HashMap<String, Properties>();
for(URL url: urls){
File file = new File(url.getFile());
if(file.isDirectory()){
System.out.println("Directory: " +file.getName());
File propFolder = new File(file.getAbsolutePath() + "/META-INF/props");
if (propFolder.exists() && propFolder.isDirectory()){
for(File f: propFolder.listFiles()){
if(f.getName().endsWith("properties") || f.getName().endsWith("props")){
Properties props = new Properties();
props.load(new FileReader(f));
String appName = props.getProperty("load.global.props.appName");
if(appName != null){
if( mapProperties.get(appName) == null) {
mapProperties.put(appName, props);
} else {
mapProperties.get(appName).putAll(props);
}
}
}
}
}
} else if (file.getName().endsWith("jar")){
System.out.println("Jar File: " + file.getName());
JarFile jarFile = null;
try{
jarFile = new JarFile(file);
Enumeration<JarEntry> entries = jarFile.entries();
while ( entries.hasMoreElements() ){
JarEntry entry = entries.nextElement();
if ( entry.getName().startsWith("META-INF/props") &&
(entry.getName().endsWith("properties") ||
entry.getName().endsWith("props"))){
System.out.println("Prop File: " + entry.getName());
Properties props = new Properties();
props.load(jarFile.getInputStream(entry));
String appName = props.getProperty("load.global.props.appName");
if(appName != null){
if( mapProperties.get(appName) == null) {
mapProperties.put(appName, props);
} else {
mapProperties.get(appName).putAll(props);
}
}
}
}
} finally {
if ( jarFile != null ) jarFile.close();
}
}
}
} catch(Exception e){
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.