[Spring Boot series] Integrated maven and spring boot profile features

Source: Internet
Author: User

Due to the needs of the project, the profile feature was added to the SPIRNG boot project today. Online Search a lap, also did not find a satisfactory reference, in fact, configuration is not difficult, is not a one stop (one-stop) to explain the place, so have the idea of writing this blog. Because of my limited level, any errors and omissions in this article welcome feedback. I hope this article can help you.

The goals that this article achieves:

1 uses the MAVEN profile feature

2 uses the profile feature of spring boot

3 sets of 1 and 2 functions

4 Run the MVN project in Eclipse, using profile for the development environment.

5 You can specify the appropriate profile when you package from the command line by MVN.


What is profile, solve what problem. Give an example. Typically, when developing projects, there are multiple environments, such as the development environment, the test environment, the production environment, and their configuration files are generally different. When we want to publish programs to each environment, we need to manually process these profiles, which is obviously cumbersome and error-prone. With profile, all the problems are simple. Just use the following command when MAVEN packs.

            MVN Clean package-dmaven.test.skip=true-p PROD

To explain,-p prod is to tell Maven to use the name PROD profile to package, that is, all the configuration files using the production environment (PROD is defined here, customized as a production environment).


The realization thought brief:

MAVEN supports the profile feature, when packaged with Maven profile, you can package the specified directory and the specified files, and modify the variables in the file. Spring boot also supports the profile feature, as long as you specify SPRING.PROFILES.ACTIVE=XXX in the application.properties file, where xxx is a variable, and when MAVEN is packaged, Modify this variable.


Specific implementation:



Section A, introducing the configuration of the Coolpro project.

This project only needs to modify the Pom.xml file, you need to define the specific Maven profile. Once defined, when we use mvn clean package-p dev, maven knows Profileactive=dev this attribute is in effect. Where profileactive can be defined by itself, is a maven custom attribute.

Pom.xml files are as follows:



Section B, Introduction to the configuration of COOLPRO-API project

This project is a Web project, primarily to configure the corresponding spring boot runtime environment with the specified profile. For example, if profile is dev and the listening port for the configuration Web server is 8010;profile to test, then the port is 8020;profile to prod and the port is 8030.

There are 4 documents in the project:

Application.properties, a file that contains a generic configuration. The file has a spring.profiles.active= @profileActive @ attribute. The Spring boot profile is configured with the Spring.profiles.active property, where the profileactive is consistent with the Profileactive attribute name configured in the Coolpro project above. This way, when you run the MVN command, MAVEN will help us replace the @profileactive@ with the specified profile.

Application-dev.properties, when Mvn-p Dev, you need to package this file.

Application-test.properties, when Mvn-p test, you need to package this file.

Application-prod.properties, when mvn-p prod, you need to package this file.

Pom.xml configuration:


Application.properties file:


See screenshots for 3 other documents:


Section C, introducing the configuration of Coolpro-core project


The configuration is complete. Look at the effect.

1 run the project directly in the eclipse environment.


2 Use the MAVEN command to package this application.

1) Packaging with development environment: MVN Clean package-dmaven.test.skip=true-p dev-e

The results are as follows:

View API Engineering:

View Core Project:



By analogy, you can run

MVN Clean package-dmaven.test.skip=true-p test-e

MVN Clean package-dmaven.test.skip=true-p prod-e


Note two points:

1 when you replace a variable in a property file, use @ Conformance.

In the beginning, using the $ symbol, you can't replace it, search on the Internet, the original Maven Maven-resources-plugin can define this replacement symbol. The Pom.xml file is opened through the Maven Pom Editor editor provided by Eclipse, and in the Effective Pom tab, you can see that the @ symbol is defined.


2 How to start the Spring boot application, print the profile you are using to avoid configuration errors. You can refer to the following code.

	public static void Main (string[] args) {
		ApplicationContext ctx = Springapplication.run (Restapiapplication.class, args);
		string[] Activeprofiles = Ctx.getenvironment (). Getactiveprofiles ();
		for (String profile:activeprofiles) {
			Logger.warn ("Spring Boot use profile is: {}", profile);
		}
	



Reference: http://docs.spring.io/spring-boot/docs/current/reference/html/

Http://www.tuicool.com/articles/NjqAF3

http://blog.csdn.net/jbgtwang/article/details/8642979

http://zilongsky-gmail-com.iteye.com/blog/2032001

http://www.huangyunkun.com/2015/01/01/run-code-after-spring-boot-started/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.