简体   繁体   English

使用 PathMatchingResourcePatternResolver 获取 WAR 文件中的 src/main/resources 时遇到问题

[英]Trouble using PathMatchingResourcePatternResolver to get src/main/resources in WAR file

I'm using Spring's PathMatchingResourcePatternResolver to attempt to get a list of.jmx files (JMeter tests) that are currently in my src/main/resources directory.我正在使用 Spring 的 PathMatchingResourcePatternResolver 尝试获取当前位于我的 src/main/resources 目录中的 .jmx 文件(JMeter 测试)列表。 This will be hosted as a WAR file on AWS.这将作为 WAR 文件托管在 AWS 上。

When I'm working locally, I have this code block giving me a list of the.jmx files in the resources/ directory, but I know can't use my local file path:当我在本地工作时,我有这个代码块给了我资源/目录中的 .jmx 文件列表,但我知道不能使用我的本地文件路径:

PathMatchingResourcePatternResolver resolver =
                   new PathMatchingResourcePatternResolver(MyClass.class.getClassLoader());
Resource[] resources = resolver.getResources("file:/C:/Users/user.name/repos/my-repo/src/main/resources/dailyTests/*.jmx")

I've been trying to use classpath*: and this actually almost works:我一直在尝试使用 classpath*: 这实际上几乎可以工作:

Resource[] resources = resolver.getResources("classpath*:*.jmx");

This will get me all of my.jmx files, with URLs that look like this:这将为我提供所有 my.jmx 文件,其 URL 如下所示:

file:/C:/Users/user.name/repos/my-repo/target/classes/my_jmeter_test.jmx

But I need to be able to handle sub directories of the resources directory as well.但我还需要能够处理资源目录的子目录。 When I attempt to be more specific, I believe I'm entering incorrect patterns.当我尝试更具体时,我相信我输入了不正确的模式。 From what I understand, when using classpath* you still need to use a root directory after classpath*:, but I'm not sure what that should be.据我了解,在使用 classpath* 时,您仍然需要在 classpath*: 之后使用根目录,但我不确定应该是什么。

So far I've tried到目前为止我已经尝试过

Resource[] resources = resolver.getResources("classpath*:Users/*.jmx");
Resource[] resources = resolver.getResources("classpath*:classes/*.jmx");
Resource[] resources = resolver.getResources("classpath*:WEB-INF/*.jmx");
Resource[] resources = resolver.getResources("classpath*:com/*.jmx");

And many other guesses, all of which returned 0 resources.和许多其他的猜测,所有这些都返回了 0 个资源。 It seems when I just get one step more specific, I'm getting 0 resources.似乎当我更具体一步时,我得到了 0 个资源。

I suppose I might be misunderstanding how I should navigate a WAR file, or possible just misunderstanding what classpath* gets me.我想我可能误解了我应该如何浏览 WAR 文件,或者可能只是误解了 classpath* 给我带来了什么。 Am I completely off here?我完全离开这里了吗?

Turns out it was my lack of understanding about where src/main/resources ends up in a WAR file when using Maven.事实证明,当使用 Maven 时,我对 src/main/resources 在 WAR 文件中的最终位置缺乏了解。 Any files in src/main/resources will get copied into the root directory of the WAR file. src/main/resources 中的所有文件都将被复制到 WAR 文件的根目录中。 (noted in the selected answer here ). (在此处选择的答案中注明)。

When using Spring's PathMatchingResourcePatternResolver, classpath* gets the root of the WAR, so that's why I was getting my.jmx files with this:当使用 Spring 的 PathMatchingResourcePatternResolver 时, classpath*获取 WAR 的根目录,这就是为什么我要使用以下内容获取 my.jmx 文件的原因:

 Resource[] resources = resolver.getResources("classpath*:*.jmx");

From there, if my project requires more files in a nested directory such as src/main/resources/specialTest.从那里开始,如果我的项目需要嵌套目录中的更多文件,例如 src/main/resources/specialTest。 I can use:我可以用:

Resource[] resources = resolver.getResources("classpath*:specialTest/*.jmx");

One thing I'm unsure on, According to the docs:根据文档,我不确定的一件事:

WARNING: Note that "classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system.警告:请注意,当与 Ant 样式模式结合使用时,“classpath*:”只能在模式开始前至少在一个根目录下可靠地工作,除非实际的目标文件驻留在文件系统中。 This means that a pattern like "classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories.这意味着像“classpath*:*.xml”这样的模式不会从 jar 文件的根目录中检索文件,而只会从扩展目录的根目录中检索文件。

I thought this would mean that I wouldn't be able to get files from the root of the WAR file, but that's exactly what I'm doing.我认为这意味着我将无法从 WAR 文件的根目录获取文件,但这正是我正在做的事情。 Currently it seems that when I use "classpath*:*.jmx" I ONLY get.jmx files from the root directory, and if I want anything deeper, I have to add a root directory to start the pattern.目前看来,当我使用“classpath*:*.jmx”时,我只从根目录获取 .jmx 文件,如果我想要更深入的内容,我必须添加一个根目录来启动模式。 I'll try to update if I fully figure out if that's correct or not.如果我完全确定这是否正确,我会尝试更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM