Maven profile environment configuration

In the development process, there are at least three environments, namely development environment, test environment and production environment, corresponding to three configuration files (here *.yml is taken as an example, *.properties is the same), respectively:
application-dev .yml development
application-uat.yml test
application-prod.yml production

There is a small point to pay attention to : the configuration file name is the underscore "-" instead of the underscore "_", here I have stepped on the pit, and The problem is difficult to locate.

The corresponding environment can be selected flexibly through two-step configuration in Maven pom.xml.

  1. profile configuration
<profiles>

		<profile>
			<id>dev</id>
			 <activation>
                 <activeByDefault>true</activeByDefault>
             </activation>
			<properties>
				<build.profile.env>dev</build.profile.env>
			</properties>
		</profile>

		<profile>
			<id>uat</id>
			<properties>
				<build.profile.env>uat</build.profile.env>
			</properties>
		</profile>

		<profile>
			<id>prod</id>
			<properties>
				<build.profile.env>prod</build.profile.env>
			</properties>
		</profile>

	</profiles>

Among them <activation> <activeByDefault>true</activeByDefault> </activation>is to control the default environment. I generally comment and prefer to select manually.
After the first step configuration is completed, reimport in the maven project window, you can see the profile options in the window, as shown in the following figure:
profile options
2.plugin After configuring the
profile, do not forget to configure the maven-antrun-plugin, The function of this plug-in is to copy the configuration information in the application-*. yml configuration file to application.yml, because only application.yml is recognized at runtime, but application- *.yml is not recognized , so one step of integration is required here. Copy the user's configuration to the application.yml specified in the framework. The plug-in configuration is as follows:

<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<phase>compile</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<target>
								<move file="${project.build.directory}/classes/application-${build.profile.env}.yml"
									  tofile="${project.build.directory}/classes/application.yml"/>
							</target>
						</configuration>
					</execution>
				</executions>
			</plugin>

At this point, the entire configuration is complete. After you package or compile through maven, you can go to target/classes to see what has changed. If uat is selected during the packaging process, you will find that there is no application-uat.yml file under the target/classes file, other dev and prod still exist, instead of the application.yml file, open this file to find the application-uat. The yml content is the same, in fact, this step is the result of step 2 above.

Guess you like

Origin blog.csdn.net/hongyinanhai00/article/details/106312874