我想使用通配符模式发现我的ClassLoader知道的所有xml文件。有没有办法做到这一点?

有帮助吗?

解决方案

这需要一点点诡计,但这里有一个相关的博客条目。首先找出罐子的URL,然后打开罐子并扫描其内容。我想你会通过查找`/META-INF/MANIFEST.MF'来发现所有罐子的URL。目录将是另一回事。

其他提示

Spring ApplicationContext 可以做到这一点:

 ApplicationContext context = new ClassPathXmlApplicationContext("applicationConext.xml");
 Resource[] xmlResources = context.getResources("classpath:/**/*.xml");

参见 ResourcePatternResolver #getResources ,或 ApplicationContext

 List<URL> resources = CPScanner.scanResources(new PackageNameFilter("net.sf.corn.cps.sample"), new ResourceNameFilter("A*.xml"));

将代码段放入pom.xml

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>

JAR文件只是另一个ZIP文件,对吧?

所以我想你可以使用 http://java.sun.com/javase/6/docs/api/java/util/zip/ZipInputStream.html

我在想:

<代码> ZipSearcher searcher = new ZipSearcher(new ZipInputStream(new FileInputStream(&quot; my.jar&quot;))); 列出xmlFilenames = searcher.search(new RegexFilenameFilter(&quot; .xml $&quot;));

干杯。基思。

嗯,它不是来自Java内部,而是

jar -tvf jarname | grep xml $

将显示jar中的所有XML。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top