【发布时间】:2014-04-18 00:47:44
【问题描述】:
注意:根据以下公认的解决方案,这似乎仅与 Spring 的 DefaultResourceLoader 不使用类加载器为资源创建 URL 实例这一事实有关,因此忽略了自定义类加载器
https://jira.spring.io/browse/SPR-8176
我有一个标准的 Maven 项目,它看起来像这样
$ tree
.
├── pom.xml
└── src
├── main
│ ├── java
│ │
│ └── resources
│ └── application.properties
└── test
└── java
我很快就会在资源目录下的目录树中拥有更多资源文件,我想循环访问它们。在其他几篇文章之后,我使用了 Spring Framework 中的 PathMatchingResourcePatternResolver。我写的抓取所有文件名的函数如下
public static List<File> getAllResourceFiles() {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources;
try {
resources = resolver.getResources("classpath*:.*");
System.out.println(JsonUtils.objectToJson(resources));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
其中objectToJson 是一个简单地使用Jackson 序列化对象的函数。现在,当我编译并运行这个应用程序时,我没有得到任何结果。特别是,我没有看到 application.properties。我该如何完成这项工作?
$ java -jar target/MyApp.one-jar.jar
[]
更新:正如下面评论中所指出的,这是使用 Simon Tuffs 的一个 jar maven 插件。但是,从类路径读取文件的常用方法是有效的。即
MyClass.class.getResourceAsStream("/application.properties");
更新 2:我尝试了以下方法:
resources = resolver.getResources("classpath:**/*.*");
并得到这个堆栈跟踪
java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:178)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.isJarResource(PathMatchingResourcePatternResolver.java:414)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:343)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:282)
at com.example.utils.ResourceUtils.getAllResourceFiles(ResourceUtils.java:29)
at com.example.Starter.main(Starter.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
Starter.java中的代码是
public static void main(String[] args) {
System.out.println(Environment.getProperty("com.example.applicationLogPath"));
System.out.println(JsonUtils.objectToJson(ResourceUtils.getAllResourceFiles()));
}
第一行成功执行并打印 applicationLogPath 属性的字符串值。第二个导致 FNF 堆栈跟踪。获取属性的代码使用标准方法获取 jar 中文件的输入流。
Environment.class.getResourceAsStream("/application.properties");
【问题讨论】:
-
那个文件夹结构是打包在 MyApp.jar 里面的?或者更好地说,相对于 MyApp.jar 位置 application.properties 在哪里?
-
这是一个很好的问题,我正在使用 maven jar 插件和 one jar 插件。我查看了 jar 内部
jar tvf target/MyApp.one-jar.jar,当我提取 jar 的内容时看到了main/MyApp.jar,并查看了这个 jar$ jar tvf MyApp.jar我在顶层看到了application.properties。 -
也就是说,我可以提取特定资源,如
InputStream inputStream = MyClass.class.getResourceAsStream("/application.properties");
标签: java spring maven resources classpath