How to populate application properties dynamically in Spring Boot?

Assumption: Maven is used to build the spring boot project

Let’s say you want to populate application.properties dynamically while building the project in Spring Boot.

You can fetch those properties from various sources like:

  • pom.xml
  • an external file
  • System properties
  • settings xml of Maven

To accomplish this , define the properties in application.properties like this :

application.name = @project.name@

To dynamically fetch any value enclose the property with @ both at the beginning and the end.

In the above example the value for project.name is populated dynamically.

Let’s look at the different ways of populating the properties:

Fetching from pom xml

The word ‘project’ is an alias for pom.xml . The above key project.name fetches the value of <name> tag from pom.xml.

Here is a sample pom xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.0.RELEASE</version>
		<relativePath /> 
	</parent>
	<groupId>com.springboot</groupId>
	<artifactId>dynamicproperties</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Maven Dynamic Properties</name>
	<description>Demo project for Spring Boot Dynamic Properties</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>


	</build>

</project>

The value from <name> tag – Maven Dynamic properties will be substituted in place @project.name@ in the properties file.

For this to work , you need to include the spring-boot-starter-parent dependency in pom.xml :

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.0.RELEASE</version>
		<relativePath /> 
	</parent>

If you are not using the starter parent dependency then do the following:

1)Include the below tag under <build> tag in pom.xml:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
    </resource>
</resources>


2)Include the below tag under <plugins> tag:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <delimiters>
            <delimiter>@</delimiter>
        </delimiters>
        <useDefaultDelimiters>false</useDefaultDelimiters>
    </configuration>
</plugin>

These are required by Maven . Since spring-boot-starter-parent defines these , it is not required if that dependency is included.

When the project is built using mvn clean install , the application.properties automatically fetches the value from pom.xml.

Fetching from External File

To fetch from an external , say a file named filter.properties, include that file in pom.xml using <filters> tag:

<filters>

	<filter>
	 G:/filter.properties
	</filter>
</filters>

Here is the filter.properties I used with a single property named ‘purpose’:

purpose = Showing Demo on how to build property file dynamically

And in application.properties define using the same @ symbol like :

application.purpose = @purpose@

Again when the project is built, the above value @purpose@ is replaced by the value “Showing Demo on how to build property file dynamically”

Fetching from System Properties:

To fetch from System.properties no configuration is needed in pom.xml except for including spring-boot-starter-parent dependency or including the maven <resources> and <plugin> tags as mentioned in the beginning of the post.

Let’s say you want to populate a key in application.properties with java version .

You can do this using @java.version@ placeholder like this :

	application.java.version = @java.version@

Again when the application is built, the placeholder will be replaced by the java version in your classpath.

Here is the complete application.properties file:


#from pom.xml
application.name = @project.name@

#from external file
application.purpose = @purpose@

#from system properties
application.java.version = @java.version@

The pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.0.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springboot</groupId>
	<artifactId>dynamicproperties</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Maven Dynamic Properties</name>
	<description>Demo project for Spring Boot Dynamic Properties</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>

<filters>

	<filter>
	 G:/filter.properties
	</filter>
</filters>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>


	</build>

</project>

Here is the application.properties generated in target folder after building the project:


#from pom.xml
application.name = Maven Dynamic Properties

#from external file
application.purpose = Showing Demo on how to build property file dynamically

#from system properties
application.java.version = 1.8.0_181

Further to this properties can be fetched from settings.xml of Maven using the prefix “settings”. Example:

application.repository = @settings.tag_name_in_settings_xml_file

tag_name_in_settings_xml_file = any of the tags in settings.xml of Maven.

This will be replaced by the actual value on building the project .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s