1. Create a configuration file

Configuration file structure:Here we create three configuration files, with application.yml as the main configuration file to configure all common configurations. -dev and -local configure different configurations in the two environments, such as the database address.



Add the spring.profiles. Active configuration to dynamically load active profiles in application.yml:

spring:
  profiles:
    active: @spring.active@
Copy the code

Add PROFILES to POM files

<profiles>
	<profile>
		<id>local</id>
		<properties>
			<spring.active>local</spring.active>
		</properties>
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
	</profile>
	<profile>
		<id>dev</id>
		<properties>
			<spring.active>dev</spring.active>
		</properties>
	</profile>
</profiles>
Copy the code

The above configuration declaration has two configuration files, dev and local. The default is local (set by true).

This gives the configured project an additional configuration item in Maven:

Three, specific application

1. Use the MVN command to package the project

MVN clean Package # Clean and package command, default is to use the local configuration file. MVN clean package -p dev # Clean and specify config file package command, use dev config file.Copy the code

2. Manually package and switch profiles by checking the Profiles option

Check dev in Maven Profiles and then package to use the dev profile.Copy the code

3. The following uses IDEA as an example when SpringBoot is started locally

If the configuration file to be activated is selected in Step 2, the main method in the application will load the selected configuration file. You can also configure the specified configuration file in IDEA. After the specified configuration file is selected, the function becomes invalid. The method is as follows: