Configure active profile in SpringBoot via Maven

前端 未结 7 1579
执笔经年
执笔经年 2020-11-29 20:13

I\'m trying to set an active profile in Spring Boot application using Maven 3.
In my pom.xml I set default active profile and property spring.pr

7条回答
  •  隐瞒了意图╮
    2020-11-29 20:58

    In development, activating a Spring Boot profile when a specific Maven profile is activate is straight. You should use the profiles property of the spring-boot-maven-plugin in the Maven profile such as :

    
        <...>
        
            
                development
                
                    true
                
                
                    
                        
                            org.springframework.boot
                            spring-boot-maven-plugin
                            
                                
                                    development
                                
                            
                        
                    
                
            
        
        
    
    

    You can run the following command to use both the Spring Boot and the Maven development profile :

    mvn spring-boot:run -Pdevelopment
    

    If you want to be able to map any Spring Boot profiles to a Maven profile with the same profile name, you could define a single Maven profile and enabling that as the presence of a Maven property is detected. This property would be the single thing that you need to specify as you run the mvn command.
    The profile would look like :

        
            spring-profile-active
            
                
                    my.active.spring.profiles
                
            
            
                
                    
                        org.springframework.boot
                        spring-boot-maven-plugin
                        
                            
                                ${my.active.spring.profiles}
                            
                        
                    
                
            
        
    

    And you can run the following command to use both the Spring Boot and the Maven development profile :

    mvn spring-boot:run -Dmy.active.spring.profiles=development
    

    or :

    mvn spring-boot:run -Dmy.active.spring.profiles=integration
    

    or :

     mvn spring-boot:run -Dmy.active.spring.profiles=production
    

    And so for...

    This kind of configuration makes sense as in the generic Maven profile you rely on the my.active.spring.profiles property that is passed to perform some tasks or value some things.
    For example I use this way to configure a generic Maven profile that packages the application and build a docker image specific to the environment selected.

提交回复
热议问题