Sunday 16 April 2023

Micronaut: Provide default values to a property value placeholder

We can provide default values to a property value place holder by defining a value after the : character.

 

Example

appDescription: ${appName:"my chat server"} is used to send and receive messages from other users. Right now this app is at ${appVersion}

 

The above example defaults to "my chat server"} if no value is found for the property appName.

 

What if the default value has a : ?

You can use backticks to specify the default value that has a : in it.

 

Example
appUrls: "dev: ${devUrl:`https://dev-default-my-chat-server.com`}, 
          qa : ${qaUrl: `https://qa-default-my-chat-server.com`},
          prod : ${prodUrl: `https://prod-default-my-chat-server.com`}"

 

The above example looks for devUrl , qaUrl and prodUrl properties and defaults to https://dev-default-my-chat-server.com, https://qa-default-my-chat-server.com and https://prod-default-my-chat-server.com if the properties are not found. These default values are escaped with backticks since it has a : character.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-default-value-to-property-placeholder’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<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>
	<groupId>com.sample.app</groupId>
	<artifactId>micronaut-default-value-to-property-placeholder</artifactId>
	<version>1</version>

	<properties>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
		<micronaut.version>3.7.3</micronaut.version>
		<slf4j.version>2.0.3</slf4j.version>
		<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>

		<maven.compiler.target>15</maven.compiler.target>
		<maven.compiler.source>15</maven.compiler.source>
	</properties>

	<dependencies>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-inject-java</artifactId>
			<version>${micronaut.version}</version>
		</dependency>
		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-runtime</artifactId>
			<version>${micronaut.version}</version>
		</dependency>

		<dependency>
			<groupId>io.micronaut</groupId>
			<artifactId>micronaut-validation</artifactId>
			<version>${micronaut.version}</version>
		</dependency>


		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven-compiler-plugin.version}</version>
				<configuration>
					<annotationProcessorPaths>
						<path>
							<groupId>io.micronaut</groupId>
							<artifactId>micronaut-validation</artifactId>
							<version>${micronaut.version}</version>
						</path>
					</annotationProcessorPaths>
				</configuration>
			</plugin>

			<plugin>
				<artifactId>maven-assembly-plugin</artifactId>
				<configuration>
					<archive>
						<manifest>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
				</configuration>

				<executions>
					<execution>
						<id>make-assembly</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>
</project>

 

Step 3: Create application.yml file under src/main/resources folder.

 

application.yml
appVersion: 1.23.456
appDescription: ${appName:"my chat server"} is used to send and receive messages from other users. Right now this app is at ${appVersion} 

devUrl: https://dev.my-chat-server.com
appUrls: "dev: ${devUrl:`https://dev-default-my-chat-server.com`}, 
          qa : ${qaUrl: `https://qa-default-my-chat-server.com`},
          prod : ${prodUrl: `https://prod-default-my-chat-server.com`}"

 

Step 4: Define main application class.

 

App.java

 

package com.sample.app;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.Environment;

public class App {

	public static void main(String[] args) {

		try (ApplicationContext applicationContext = ApplicationContext.run()) {

			Environment environment = applicationContext.getEnvironment();

			String appName = environment.getProperty("appName", String.class, "No appName found");
			String appVersion = environment.getProperty("appVersion", String.class, "No appVersion found");
			String appDescription = environment.getProperty("appDescription", String.class, "No description found");
			String appUrls = environment.getProperty("appUrls", String.class, "No appUrls found");

			System.out.println("appName : " + appName);
			System.out.println("appVersion : " + appVersion);
			System.out.println("appDescription : " + appDescription);
			System.out.println("appUrls : " + appUrls);
		}

	}
}

Total project structure looks like below.



Build the project using mvn package command.

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

Upon command successful execution, you can see the jar file ‘micronaut-default-value-to-property-placeholder-1-jar-with-dependencies.jar’ in project target folder.

$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-default-value-to-property-placeholder-1-jar-with-dependencies.jar
micronaut-default-value-to-property-placeholder-1.jar
test-classes

Execute below command to run the application.

$java -jar ./target/micronaut-default-value-to-property-placeholder-1-jar-with-dependencies.jar
appName : No appName found
appVersion : 1.23.456
appDescription : "my chat server" is used to send and receive messages from other users. Right now this app is at 1.23.456
appUrls : dev: https://dev.my-chat-server.com, qa : //qa-default-my-chat-server.com`, prod : //prod-default-my-chat-server.com`

You can download this application from this link.


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment