Saturday 14 September 2024

How to build spring boot application for Production , UAT Testing and Dev Environment using Eclipse.

In this blog I will show you that how we can build Spring Boot Application for different Environments like Production , UAT Testing and Dev. Here i am creating a Spring Boot Application and also you can download this application at the end of this post.

create a Controller class
------------------------------ AppController.class -----------------------------

@RestController
@RequestMapping("/api")
public class AppController {

@GetMapping("/status")
public String runningStatus() {
String deployName = VariableConfig.CURRENT_ENVIRONMENT;
System.out.println("/api/=> runningStatus()..."+VariableConfig.CURRENT_ENVIRONMENT);
return "<h3 style="color: red;">"+deployName+"</h3><h2 style="color: green;">Application is Running....</h2>";
}

}


create a config class for set currrent Environment variable

--------------------------------VariableConfig.class---------------------

@Component
public class VariableConfig {

public VariableConfig() {

}

public static String CURRENT_ENVIRONMENT;

@Value("${environment.current}")
public void setCurrentEnvironment(String currentEnvironment) {
CURRENT_ENVIRONMENT = currentEnvironment;
}
}


create four properties files for local, prod and stage Environments.

application.properties
application-local.properties
application-prod.properties
application-stage.properties

----------------application.properties-------------------

spring.profiles.active=@activatedProperties@

----------------application-local.properties--------------------

profile.environment.current=local
server.servlet.context-path=/profile
server.port=8080
environment.current=Local Environment


----------------application-prod.properties----------------------

profile.environment.current=prod
server.servlet.context-path=/profile
server.port=9090
environment.current=Prod Environment

----------------application-stage.properties---------------------

profile.environment.current=stage
server.servlet.context-path=/profile
server.port=2020
environment.current=Stage Environment


----------------------pom.xml--------------------------


<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" 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.7.6</version>
<relativepath> <!--lookup parent from repository-->
</relativepath></parent>
<groupid>com.ramsis</groupid>
<artifactid>profile</artifactid>
<version>0.0.1-SNAPSHOT</version>
<name>profile</name>
<description>Demo project for Spring Boot</description>
<properties>
<java .version="">11</java>
</properties>
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>

<!--https://mvnrepository.com/artifact/commons-codec/commons-codec-->
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-text</artifactid>
<version>1.3</version>
</dependency>


<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-maven-plugin</artifactid>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>local</id>
<activation>
<activebydefault>true</activebydefault>
</activation>
<properties>
<activatedproperties>local</activatedproperties>
</properties>
</profile>

<!--Stage Environment-->
<profile>
<id>stage</id>
<properties>
<activatedproperties>stage</activatedproperties>
</properties>
</profile>

<!--Production Environment-->
<profile>
<id>prod</id>
<properties>
<activatedproperties>prod</activatedproperties>
</properties>
</profile>

</profiles>

</project>

------------------------------------------------------

Download Code