Specify spring.profiles.active via context-param of web.xml in Spring Boot

For the time being, create an appropriate `pom.xml`.

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 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>kagamihoge</groupId>
	<artifactId>asdasdf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>asdasdf</name>
	<description>Add project description here</description>

	<properties>
		<maven.war.filteringDeploymentDescriptors>true</maven.war.filteringDeploymentDescriptors>
	</properties>

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

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

	<profiles>
		<profile>
			<id>hoge</id>
			<properties>
				<spring_profile_value>hoge_profile</spring_profile_value>
			</properties>
		</profile>
	</profiles>
</project>

Of the above, by setting `<maven.war.filteringDeploymentDescriptors> ... </ maven.war.filteringDeploymentDescriptors>`, `maven-war-plugin``` will be web.xml It will replace the `` placeholder.

src/main/webapp/WEB-INF/web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">

	<context-param>
		<param-name>spring.profiles.active</param-name>
		<param-value>@spring_profile_value@</param-value>
	</context-param>

</web-app>

Specify spring.profiles.active specified in maven profile in context-param of web.xml. In spring-boot, the maven placeholder reference is `@ ... @` instead of `` $ {...} `.

After that, if you do mvn package -P hoge, the web.xml in the generated war will look like this.

web.xml


<context-param>
	<param-name>spring.profiles.active</param-name>
	<param-value>hoge_profile</param-value>
</context-param>

However, it is also written in How to set spring active profiles with maven profiles that I referred to. But it's not very beautiful to switch active profiles this way. This is because different wars are created for each environment, so there is less trouble if there is only one final binary.

However, even so, there are various cases in the real world, so it cannot be said that the specification via web.xml is bad.

Reference URL

Recommended Posts

Specify spring.profiles.active via context-param of web.xml in Spring Boot
Specify the encoding of static resources in Spring Boot
I checked asynchronous execution of queries in Spring Boot 1.5.9
How to use CommandLineRunner in Spring Batch of Spring Boot
Spring Boot 2 multi-project in Gradle
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Accelerate testing of Validators that require DI in Spring Boot
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Write test code in Spring Boot
Going out of message (Spring boot)
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
[Spring Boot] Role of each class
Implement Spring Boot application in Gradle
Thymeleaf usage notes in Spring Boot
Unknown error in line 1 of pom.xml when using Spring Boot in Eclipse
Procedure to make the value of the property file visible in Spring Boot
Launch (old) Spring Boot project in IntelliJ
Output Spring Boot log in json format
Local file download memorandum in Spring Boot
Create Java Spring Boot project in IntelliJ
Loosen Thymeleaf syntax checking in Spring Boot
[Practice! ] Display Hello World in Spring Boot
WebMvcConfigurer Memorandum of Understanding for Spring Boot 2.0 (Spring 5)
Use DynamoDB query method in Spring Boot
DI SessionScope Bean in Spring Boot 2 Filter
Change session timeout time in Spring Boot
Organize the differences in behavior of @NotBlank, @NotEmpty, @NotNull with Spring Boot + Thymeleaf
Get the path defined in Controller class of Spring boot as a list
How to set environment variables in the properties file of Spring boot application
SameSite cookie in Spring Boot (Spring Web MVC + Tomcat)
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Summary of what I learned about Spring Boot
[FCM] Implementation of message transmission using FCM + Spring boot
Output request and response log in Spring Boot
The story of raising Spring Boot 1.5 series to 2.1 series
Let's check the feel of Spring Boot + Swagger 2.0
Various correspondence table of Spring Framework and Spring Boot
When @Transactional of Spring Boot does not work
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
How to add a classpath in Spring Boot
[Java / Spring Boot] Spring security ④ --Implementation of login process
Java tips-Create a Spring Boot project in Gradle
Annotations used in Spring Boot task management tool
Summary of what I learned in Spring Batch
View the Gradle task in the Spring Boot project
[Verification] Comparison of Spring Boot vs Micronaut boot speed
Check the behavior of getOne, findById, and query methods in Spring Boot + Spring Data JPA