이 제한 사항을 해결하기위한 코드를 작성했습니다.
클래스 경로에서 모든 항목을 읽고 폴더 또는 JAR 파일인지 확인한 다음 "META_INF/props"에서 항목을 찾습니다. 항목이 속성 인 경우로드합니다.
아래 코드는 세련된 것은 아니지만 일반적인 아이디어입니다.
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();
}
아니요. 반환 할 ['ClassLoader # findResources'] (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#findResources (java.lang.String))을 사용할 수 있습니다. 클래스 경로 내에서 같은 이름의 모든 자원을 열거하지만 여전히 정규화 된 경로와 이름을 기대합니다. – MadProgrammer