简体   繁体   English

Maven-从JAR加载资源?

[英]Maven - Load resources from JAR?

I have a feeling I'm missing something critical when it comes to bundling resources in JAR files using Maven. 当使用Maven绑定JAR文件中的资源时,我感觉缺少一些关键的东西。 My directory structure is the standard Maven one (ie \\src\\main\\java, \\src\\main\\resources). 我的目录结构是标准的Maven目录(即\\ src \\ main \\ java,\\ src \\ main \\ resources)。

When using the IDE I reference the resource files as \\main\\resources\\images\\blah... and it works fine, but when Maven bundles the JAR file it puts the paths relative to the root ("\\images\\blah"), so none of the paths are correct. 使用IDE时,我将资源文件引用为\\ main \\ resources \\ images \\ blah ...,并且工作正常,但是当Maven捆绑JAR文件时,它将相对于根的路径(“ \\ images \\ blah”)放置,因此所有路径都不正确。

My question is two-fold really, the first being am I missing something obvious? 我的问题确实是双重的,首先是我缺少明显的东西吗? The second being what's the best way to handle this issue? 第二个是处理此问题的最佳方法是什么? I know I could just replace all the paths when I go to bundle everything for deployment, or surround all the paths in try/catch blocks, or even change the maven output directory, but I feel like these are in-elegant and I'm missing something. 我知道当我打包所有内容以进行部署时,可以将所有路径替换,或者将所有路径包围在try / catch块中,甚至更改maven输出目录,但是我觉得它们太过优雅了,遗漏了什么。 Thanks in advance. 提前致谢。

EDIT: I reference the resource files like this: getClass().getResource("/main/resources/images/lightboardImage.jpg")); 编辑:我像这样引用资源文件: getClass().getResource("/main/resources/images/lightboardImage.jpg"));

EDIT2: This is obviously now an IDE configuration issue, probably stemming from the fact that I converted an old project into one using Maven. EDIT2:现在这显然是一个IDE配置问题,可能是由于我使用Maven将一个旧项目转换为一个项目这一事实。 I don't really understand how it is misconfigured though. 我不太了解它是如何配置错误的。 My folder structure is: 我的文件夹结构是:

src/main/java src / main / java
src/main/resources src / main / resources

My classpath has one entry for "src" <classpathentry kind="src" path="src"/> 我的类路径中有一个用于“ src”的条目<classpathentry kind="src" path="src"/>

If I right click on the /main/resources/images/ folder and Use as Source Folder it turns the classpathentry into this <classpathentry excluding="main/resources/images/" kind="src" path="src"/> and creates a new one like this <classpathentry kind="src" path="src/main/resources/images"/> . 如果我右键单击/main/resources/images/文件夹并用作源文件夹,它将把classpathentry变成此<classpathentry excluding="main/resources/images/" kind="src" path="src"/>并创建一个新的像这样的<classpathentry kind="src" path="src/main/resources/images"/> It also doesn't work. 它也不起作用。

EDIT3: Okay this is really dumb but I just realized what the issue is. EDIT3:好的,这确实很愚蠢,但是我才意识到问题出在哪里。 I'm not actually using Maven for local development builds. 我实际上并没有使用Maven进行本地开发。 It's using Eclipse's built in Ant configuration or whatever. 它使用的是Eclipse的内置Ant配置。 Maven is used for the automated deployment builds, hence the issue. Maven用于自动部署构建,因此出现了问题。 Sorry to waste everyone's time. 很抱歉浪费大家的时间。

This should work from IDE and from JAR. 这应该在IDE和JAR中都有效。

getClass().getResource("images/lightboardImage.jpg");

Files from src/main/resources are copied to target/classes . 来自src/main/resources被复制到target/classes

During runtime, all resources should be visible from classpath as images/lightboardImage.jpg . 在运行时,所有资源应从classpath中以images/lightboardImage.jpg可见。 This works from IDE for me. 这对我来说是IDE的作品。 If not, check your IDE configuration as said @juanluisrp. 如果没有,请按照@juanluisrp所述检查IDE配置。

Java executed from my IDE looks like this: 从我的IDE执行的Java如下所示:

java  -classpath (...) /home/.../target/classes (...)

So, the runtime classpath is configured to target/classes directory. 因此,运行时类路径被配置为target/classes目录。

During the lifecycle of Maven, resources are copied to target/classes . 在Maven的生命周期中,资源被复制到target/classes When the JAR is assembled target/classes content goes to the JAR root. 组装JAR时, target/classes内容将转到JAR根目录。 So, if you want to load a resource that is in the JAR you can use 因此,如果您要加载JAR中的资源,则可以使用

getClass().getResource("/images/lightboardImage.jpg"));

From the IDE it should work with the same path unless you don't have classpath properly configured. 在IDE中,除非没有正确配置类路径,否则它应该使用相同的路径。 If you are using Eclipse check that src/main/resources is marked as source folder. 如果使用的是Eclipse,请检查src/main/resources是否标记为源文件夹。

I think the problem here is that you need to set your IDE to point to src/main/resources as part of the classpath. 我认为这里的问题是,您需要将IDE设置为指向src/main/resources作为类路径的一部分。 Then you can use getClass().getResource("images\\lightboardImage") for both running under maven and from your IDE. 然后,您可以使用getClass().getResource("images\\lightboardImage")在maven下和从IDE中运行。

For example, in Eclipse you would do this by right-clicking on src/main/resources in the Project Explorer and clicking "Build Path" -> "Use as Source Folder" 例如,在Eclipse中,您可以通过右键单击Project Explorer中的src/main/resources并单击"Build Path" -> "Use as Source Folder"

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

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