Resources and configuration loading in a maven project

I use Maven to install on the desktop. I read about the standard Maven layout directory , and now I have this project structure:

App |-- pom.xml `-- src |-- main |-- java | `-- java classes |-- resources | `-- images | `-- app images `--config `--config.xml 

I want to find a way to load my resources and configuration files. I read some articles and topics and found this (simplified example from my code):

 //class for loading config public class Preferences { public Preferences() { InputStream image = Preferences.class.getResourceAsStream("images/image.png"); InputStream config = Preferences.class.getResourceAsStream("config.xml"); } } 

But image variables and configurations contain zero. I tried to use different loading options (from the root folder, with this.getClass () instead of Preferences.class and others), but I always have null. I really do not understand this resource loading system, and I have not found any good documentation about this. It would be nice if someone gives a good explanation for this mechanism (or just give a link to the documents). So, the main question: how to load my resources and configuration files?

+6
source share
5 answers

I think I found a solution. As Juned Ahsan and mR_fr0g wrote, I need to use the ClassLoader class instead of this.getClass (). GetResource (). But it only works for the resource folder. But maven allows you to add other folders as resource folders. I just needed to add this section to pom.xml:

 <build> <resources> <resource> <directory>src/main/config</directory> </resource> </resources> </build> 

And Java working code:

 InputStream image = this.getClass().getClassLoader().getResourceAsStream("images/image.png"); InputStream config = ClassLoader.getSystemResourceAsStream("config.xml"); 
+4
source
 public Preferences() { InputStream image = this.getClass().getClassLoader().getResourceAsStream("image.png"); InputStream config = this.getClass().getClassLoader().getResourceAsStream("config.xml") } 
+3
source

How about using this rating:

 InputStream file = ClassLoader.getSystemResourceAsStream("res.txt"); 

or

 InputStream file = Thread.currentThread().getContextClassLoader().getResourceAsStream("MyProperty.properties"); 

As you now see it, this will look for the MyProperty.properties file at the top of your class path. Maybe in your src / main / resources directory or in another src folder - this will depend on how your application is created (jar / war).

If you are building a can, you must unpack it and look at your properties file at the top level of the can. If you are building a war, perhaps it should be in the WEB-INF / classes directory. Again, it depends on how it was built.

+2
source

You can use the getResourceAsStream() method of java.lang.Class , as you did, but before you go, you need to add / .


This question is complicated.

1. Two methods with the same name

First of all, there are two methods of the same name and the same signature in these two classes:

 java.lang.Class java.lang.ClassLoader 

They have the same name: getResource(String) (and getResourceAsStream(String) same).

2. They accept different format parameters

Then their parameter has a different format:

  • The java.lang.Class.getResouce<asStream>() method takes a path with and without a leading / , which leads to various resource search strategies. If the path does not have / , Java will look for the resource in the folder / folder where the .class file is located. If it has / , Java will start searching from the root of the classpath.
  • The java.lang.ClassLoader.getResource<asStream>() method accepts only the path without / , because it always looks for the class path. In a path based on classpath / not a valid character. *

    *: This answer says: this.getClass (). getClassLoader (). getResource ("...") and NullPointerException

How to add a folder to classpath? In Eclipse, we enable the project context menu: "Build Path" - "Configure Build Path ..." and add a folder to create the path.

3. When it comes to Maven

Finally, if the project is a Maven project, by default src/main/resources is in the classpath, so we can use

 Class.getResource("/path-to-your-res"); 

or,

 ClassLoader.getResource("path-to-your-res"); 

to load something under src/main/resources .

If we want to add another resource folder, as you already mentioned, this is done in pom.xml . And they are also added to the classpath made by Maven. No additional configuration is required.

4. Example

For example, if your config.ini is under src/main/resources/settings , myAvatar.gif in the src/main/images section, you can do:

In pom.xml :

 <build> <resources> <resource> <directory>src/main/images</directory> </resource> </resources> </build> 

In code:

 URL urlConfig = MyClass.class.getResource("/settings/config.ini"); //by default "src/main/resources/" is in classpath and no config needs to be changed. InputStream inputAvatar = MyClass.class.getResourceAsStream("/myAvatar.gif"); //with changes in pom.xml now "src/main/images" is counted as resource folder, and added to classpath. So we use it directly. 

We must use / above.

Or, with ClassLoader:

 URL urlConfig = MyClass.class.getClassLoader().getResource("settings/config.ini"); //no leading "/"!!! InputStream inputAvatar = MyClass.class.getClassLoader().getResourceAsStream("myAvatar.gif"); //no leading "/"!!! 
+2
source

Try to create a project earlier. If you just put the files in the resource folder, you need to build a project to copy the new resource files to the target folder

+1
source

Source: https://habr.com/ru/post/944277/


All Articles