How to set spring active profiles with maven profiles

后端 未结 6 884
耶瑟儿~
耶瑟儿~ 2020-12-01 01:05

I have an application with maven as a build tool.

I am using maven profiles to set up different properties from different profiles.

What i would like to do i

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 01:40

    The first thing you need is two properties files for keeping your configurations. The names of the files should match with the pattern application-{custom_suffix}.properties. Create them in the src/main/resources directory of your Maven project, next to the main application.properties file, which you’re going to use later to activate one of the others and to hold values shared by both profiles.

    Then it’s time to modify your pom.xml. You need to define a custom property in each of your Maven profiles and set their values to match with suffixes of corresponding properties files that you want to load with a particular profile. The following sample also marks the first profile to run by default, but it’s not mandatory.

    
        dev
        
            dev
        
        
            true
        
    
    
        release
        
            release
        
    
    

    Next, in the build section of the same file, configure filtering for the Resources Plugin. That will allow you to insert properties defined in the previous step into any file in the resources directory, which is the subsequent step.

    
        
            
                src/main/resources
                true
            
        
    

    Finally, add the following line to the application.properties.

    spring.profiles.active=@activatedProperties@
    

    When the build is run, the Resources Plugin will replace the placeholder with the value of the property defined in the active Maven profile. After starting your application, the Spring framework will load the appropriate configuration file based on the name of the active Spring profile, which is described by the value of the spring.profiles.active property. Note that Spring Boot 1.3 replaced the default Resources Plugin syntax for filtered values and uses @activatedProperties@ instead of ${activatedProperties} notation.

    It worked to perfection. Hope this can help you.

提交回复
热议问题