Monday, 20 March 2023

Micronaut: Specify the environment in command line

You can specify the environment at command line using ‘-Dmicronaut.environments’ option.

 

Example


java -Dmicronaut.environments=env1,env2 -jar myapp.jar

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-specify-envt-cmd-line’.

 

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-specify-envt-cmd-line</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: Define application property files in src/main/resources folder.

 

application.yml

logger:
  levels:
    io.netty: ERROR
    io.micronaut: ERROR
    
a1: default-a1
a2: default-a2
a3: default-a3

application-dev.yml

logger:
  levels:
    io.netty: ERROR
    io.micronaut: ERROR
    
a1: dev-a1
a2: dev-a2

application-qa.yml

logger:
  levels:
    io.netty: ERROR
    io.micronaut: ERROR
    
a1: qa-a1
a2: qa-a2

application-prod.yml

logger:
  levels:
    io.netty: ERROR
    io.micronaut: ERROR
    
a1: prod-a1
a2: prod-a2

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 a1 = environment.getProperty("a1", String.class, "a1");
			String a2 = environment.getProperty("a2", String.class, "a2");
			String a3 = environment.getProperty("a3", String.class, "a3");

			System.out.println("a1 : " + a1);
			System.out.println("a2 : " + a2);
			System.out.println("a3 : " + a3);
		}

	}
}

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-specify-envt-cmd-line-1-jar-with-dependencies.jar’ in project target folder.


$ ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-specify-envt-cmd-line-1-jar-with-dependencies.jar
micronaut-specify-envt-cmd-line-1.jar
test-classes

Execute below command to run the app in dev environment

java -Dmicronaut.environments=dev -jar ./target/micronaut-specify-envt-cmd-line-1-jar-with-dependencies.jar

$ java -Dmicronaut.environments=dev -jar ./target/micronaut-specify-envt-cmd-line-1-jar-with-dependencies.jar
[main] INFO io.micronaut.context.env.DefaultEnvironment - Established active environments: [dev]
a1 : dev-a1
a2 : dev-a2
a3 : default-a3

From the above output, you can confirm that, if any property is not present in the environment specific property file, it will read from default property file (application.yml).

 

What if I do not specify the profile?

Micronaut take the properties from application.yml file.

$ java -jar ./target/micronaut-specify-envt-cmd-line-1-jar-with-dependencies.jar
a1 : default-a1
a2 : default-a2
a3 : default-a3

You can download this application from this link.


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment