Java 在 IntelliJ 中为 Maven 项目添加资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18717038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding resources in IntelliJ for Maven project
提问by vikas
I have a project structure like this:
我有一个这样的项目结构:
src
|-main
|-java
|-com.abc.xyz
|-Login.java
I have to add a resource file to this and read the resource with
我必须为此添加一个资源文件并读取资源
InputStream is = getClass().getResourceAsStream("launchers.properties");
This is giving null.
这是给空的。
In Intellij I am not able to add a new package under src/main
for resources
folder so
that the project structure looks like this. How can I load the launchers.properties
resource file into the project?
在 Intellij 中,我无法在src/main
forresources
文件夹下添加新包,因此项目结构如下所示。如何将launchers.properties
资源文件加载到项目中?
src
|-main
|-java
|-com.abc.xyz
|-Login.java
|-resources
|-com.abc.xyz
|-Login
|-launcher.properties
I tried the solution suggested by @maba but still not working
我尝试了@maba 建议的解决方案,但仍然无法正常工作
回答by maba
The launcher.properties
should not be under a folder called Login
. It should be placed directly in the src/main/resources/com/abc/xyz
folder.
本launcher.properties
不应该是一个叫做文件夹下Login
。它应该直接放在src/main/resources/com/abc/xyz
文件夹中。
It is really as simple as I said but if the resources folder is not marked as a sources folder then this may be the problem.
这真的像我说的一样简单,但如果资源文件夹没有标记为源文件夹,那么这可能是问题所在。
This is the initial class and setup:
这是初始类和设置:
Now create the resources folder:
现在创建资源文件夹:
This newly created folder should be automatically marked as a sources folder and if it is blue color marked then it is. Otherwise you'll have to mark it manually:
这个新创建的文件夹应该被自动标记为源文件夹,如果它被标记为蓝色,那么它就是。否则,您必须手动标记它:
Now you'll be able to add packages to it:
现在您将能够向其中添加包:
And now you can add the file to it:
现在您可以将文件添加到其中:
And rerunning the application will not give you any null
value back:
重新运行应用程序不会给你任何null
价值:
And the package view will surely show the launchers.properties
file as well:
并且包视图肯定也会显示该launchers.properties
文件:
回答by boky
As @maba pointed out, your properties file should be in the same package as your class for your code to work.
正如@maba 指出的那样,您的属性文件应该与您的类位于同一个包中,以便您的代码正常工作。
So, you should have two files:
所以,你应该有两个文件:
- src/main/java/com/abc/xyz/Login.java
- src/main/resources/com/abc/xyz/launcher.properties
- src/main/java/com/abc/xyz/Login.java
- src/main/resources/com/abc/xyz/launcher.properties
If IntelliJ is showing the resource or not is beside the question. What you need to do is check if the results are included in your target artefact.
IntelliJ 是否显示资源不是问题。您需要做的是检查结果是否包含在您的目标工件中。
Do a build all
in IntelliJ, open up the resulting WAR/JAR/EAR with your favorite ZIP viewer and browse into the "com/abc/xyz" folder. You should see both files there.
build all
在 IntelliJ 中做一个,用你最喜欢的 ZIP 查看器打开生成的 WAR/JAR/EAR 并浏览到“com/abc/xyz”文件夹。您应该在那里看到两个文件。
- If they are, you are doing something wrong in your code. Check for typos, especially dots and spaces at the end or beginning (e.g. "launcher.properties[space]"), copy/paste the file name to make sure
- If they are not there, your IntelliJ setup is wrong. Resources do not get included in your target build. Check online for tutorialshow to do this with IntelliJ idea.
- 如果是,那么您在代码中做错了。检查拼写错误,尤其是结尾或开头的点和空格(例如“launcher.properties[space]”),复制/粘贴文件名以确保
- 如果它们不存在,则您的 IntelliJ 设置是错误的。资源不会包含在您的目标构建中。检查网上的教程如何使用IntelliJ IDEA的做到这一点。
回答by WesternGun
No..... the structure is wrong.... you should not create the same package under resources
, that is ugly and not proper: resources
is for resources, and should not contain source packages.
不..... 结构错误.... 您不应该在 下创建相同的包resources
,这很丑陋且不正确:resources
用于资源,不应包含源包。
When using ClassLoader.getResources(asStream)(path)
method, the method just starts searching from the root of the classpath and the path name cannot start with /
as leading characters. What you have to do, is to mark the resources
as resources folder
in IntelliJ. Then the files under resources
will be listed under classpath and you can easily use them like you have done.
使用ClassLoader.getResources(asStream)(path)
方法时,方法只是从类路径的根开始搜索,路径名不能/
以前导字符开头。你需要做的是在 IntelliJ 中标记resources
as resources folder
。然后下面的文件resources
将列在类路径下,您可以像以前一样轻松地使用它们。
(I see in previous answers this option is not available yet in 2013, you only have mark as source folder
, just like in Eclipse till now we have "add source folder", but now in 2018 it is available in Intellij: you can mark a folder as source, resources, test source, test resources, and all of them will be add to the root of classpath. )
(我在之前的答案中看到此选项在 2013 年尚不可用,您只有mark as source folder
,就像在 Eclipse 中直到现在我们有“添加源文件夹”一样,但现在在 2018 年它在 Intellij 中可用:您可以将文件夹标记为源、资源、测试源、测试资源,它们都会被添加到类路径的根目录中。)
回答by vivanov
I had the same problem and noticed that the resource file, for example: my.properties
is not copied to the corresponding module folder in the target
directory after build occurres. In order to solve that, I had to instruct Maven to copy the resources from the module directory to the target
directory during the build process. In the .pom
file I added <resource>
element like that:
我遇到了同样的问题,并注意到资源文件,例如:在构建发生后my.properties
没有复制到目录中的相应模块文件夹中target
。为了解决这个问题,我不得不target
在构建过程中指示 Maven 将资源从模块目录复制到目录中。在.pom
文件中,我添加了这样的<resource>
元素:
<project ...>
...
<build>
...
<resource>
<directory>src/main/java/com/abc/xyz</directory>
<targetPath>com/abc/xyz</targetPath>
</resource>
</build>
...
</project>
Note that the <directory>
element is relative to the location of the .pom
file , i.e. the root directory of the project, and the <targetPath>
element indicates the package name separated by slashes.
注意<directory>
元素是相对于.pom
文件所在的位置,即项目的根目录,<targetPath>
元素是用斜杠分隔的包名。
回答by Sudabe-Neirizi
from menu Run/edit configuration in VM option you should add -Dspring.config.location=path-file
从菜单运行/编辑配置在 VM 选项中,您应该添加 -Dspring.config.location=path-file
I've tried it in IntelliJ, and it works!
我已经在 IntelliJ 中尝试过,它有效!
回答by dasunse
Follow these two steps
按照这两个步骤
1) Create a directory
1) 创建目录
Right Click ==> New ==> Directory
右键单击 ==> 新建 ==> 目录
2) Mark Directory as Resources Root
2) 将目录标记为资源根
Right Click on the Direcory ==> Mark Directory as ==> Resources Root
右键单击目录 ==> 将目录标记为 ==> 资源根目录
回答by acbiler
Only solution worked for me:
唯一的解决方案对我有用:
File -> Project Structure -> Modules -> Dependencies Tab -> + Sign -> JARs or directories -> select resources directory -> Classes
文件 -> 项目结构 -> 模块 -> 依赖项选项卡 -> + 符号 -> JAR 或目录 -> 选择资源目录 -> 类