Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in DevOps and Agile by (29.3k points)

Is it possible to have multiple application properties file? (EDIT: note that this question evolved to the one on the title.)

I tried to have 2 files.

The first one is on the root folder in the application Jar.

The second one is on the directory which is specified in the classpath.

2 files are both named 'application.properties'.

Is it possible to 'merge' the contents of both files? (and the second one's property values override the first ones) Or, if I have one file then the other file is ignored?

1 Answer

0 votes
by (50.2k points)

Yes,  it is possible to 'merge' the contents. 

For this, you need to load an application.properties file in the external path while using -jar option.

The key was PropertiesLauncher.

To use PropertiesLauncher, the pom.xml file must be changed like this:

<build>

    <plugins>

        <plugin>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-maven-plugin</artifactId>

            <configuration>  <!-- added -->

                <layout>ZIP</layout> <!-- to use PropertiesLaunchar -->

            </configuration>

        </plugin>

    </plugins>

</build>

After the jar file had been built, you could see that the PropertiesLauncher is used by inspecting Main-Class property in META-INF/MANIFEST.MF in the jar.

Now, you can run the jar as follows:

java -Dloader.path=file:///C:/My/External/Dir,MyApp-0.0.1-SNAPSHOT.jar -jar MyApp-0.0.1-SNAPSHOT.jar

Note that the application jar file is included in loader.path.

Now an application.properties file in C:\My\External\Dir\config is loaded.

As a bonus, any file (for example, static html file) in that directory can also be accessed by the jar since it's in the loader path.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...