Make IntelliJ aware of a properties file

11,642

Solution 1

Just look at this picture

This can be set in project structure. Just look at this capture.

Solution 2

This is how I do it:

  • From the project explorer, i right click in src/main/resources, I hover on "Mark Directory As..." and I click on "Resources Root"

enter image description here

  • In the Spring configuration I just specify the name of the properties file directly.

Here an example when I am doing this in one of my projects: https://github.com/SFRJ/springpractice/blob/master/src/main/java/part3/PropertiesLoaderConfiguration.java

By the way, if you are using multiple property sources you can also do something like this:

@PropertySources({
        @PropertySource("a.properties"),
        @PropertySource("b.properties")
    })

Solution 3

Create a folder named res in your project and keep the necessary property files in that folder. Go to the Project_Structures -> Modules and select the tab Resources. In your code, you can access the resource file using the path: res/your_file_name.

Before marking the folder as a resource folder enter image description here

After marking the folder as a resource folder enter image description here

Share:
11,642
Joe Lee-Moyet
Author by

Joe Lee-Moyet

Updated on June 06, 2022

Comments

  • Joe Lee-Moyet
    Joe Lee-Moyet almost 2 years

    Is there some way to tell IntelliJ that a particular .properties file will be loaded into a project's environment? We use @PropertySource annotations to load properties files, and in several places load override values from a file determined by an already-configured property, like so:

    @Configuration
    @PropertySource("classpath:com/example/package/db.properties")
    @PropertySource("classpath:com/example/package/db.${db.instance}.properties")
    public class DatabaseConfig {
        private @Value("${db.someProperty}") String someDBProperty;
        // ...
    }
    

    The problem is that within one of these indirectly referenced files, e.g. db.test.properties, IntelliJ doesn't know if/how the properties file is referenced, so it can't tie any of the entries to their usages. (Bizarrely, some of the properties listed in these files are not greyed out to indicate 'Unused property', though even these give no results for a 'Find usages' search). There is no issue with directly named files, e.g. db.properties above.

    Is there some way to tell IntelliJ about these files short of creating additional dummy @Configuration files that reference them?

  • Joe Lee-Moyet
    Joe Lee-Moyet almost 9 years
    Thanks for your answer sfrj, I'm not sure this answers the question though - I want to reference different property sources depending on a property that's already been loaded. E.g. in the example I gave where I want to load the configuration for a specific database instance, where the DB instance to use has been configured elsewhere (in my case it is looked up at application startup time dependent on the running host).
  • Joe Lee-Moyet
    Joe Lee-Moyet almost 9 years
    Also the wrapper PropertySources annotation is not needed from Java 8 which supports repeatable annotations.
  • Mark Ainsworth
    Mark Ainsworth over 8 years
    My problem was different (simpler). I wanted to open a properties file using Properties properties = null; InputStream is = getClass().getClassLoader().getResourceAsStream("mysql.prope‌​rties") properties.load(is); but is was always null. sfrj's answer solved my problem