【发布时间】:2010-09-13 02:11:16
【问题描述】:
我想发现我的 ClassLoader 使用通配符模式知道的所有 xml 文件。有没有办法做到这一点?
【问题讨论】:
我想发现我的 ClassLoader 使用通配符模式知道的所有 xml 文件。有没有办法做到这一点?
【问题讨论】:
Spring ApplicationContext 可以轻松做到这一点:
ApplicationContext context = new ClassPathXmlApplicationContext("applicationConext.xml");
Resource[] xmlResources = context.getResources("classpath:/**/*.xml");
请参阅ResourcePatternResolver#getResources 或ApplicationContext。
【讨论】:
classpath*: 才能在 jar 中查找资源。这至少有一个war文件和一个包含xml文件的jar库。
List<URL> resources = CPScanner.scanResources(new PackageNameFilter("net.sf.corn.cps.sample"), new ResourceNameFilter("A*.xml"));
把 sn-p 放到你的 pom.xml 中
<dependency>
<groupId>net.sf.corn</groupId>
<artifactId>corn-cps</artifactId>
<version>1.0.1</version>
</dependency>
【讨论】:
这需要一点技巧,但这里有一个相关的blog entry。您首先找出罐子的 URL,然后打开罐子并扫描其内容。我想你会通过查找“/META-INF/MANIFEST.MF”来发现所有 jar 的 URL。目录将是另一回事。
【讨论】:
JAR 文件只是另一个 ZIP 文件,对吗?
所以我想你可以使用 http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html 迭代 jar 文件
我在想这样的事情:
ZipSearcher searcher = new ZipSearcher(new ZipInputStream(new FileInputStream("my.jar")));
List xmlFilenames = searcher.search(new RegexFilenameFilter(".xml$"));
干杯。基思。
【讨论】:
嗯,它不是来自 Java,而是
jar -tvf jarname | grep xml$
将显示 jar 中的所有 XML。
【讨论】: