How to set spring active profiles with maven profiles

后端 未结 6 893
耶瑟儿~
耶瑟儿~ 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:46

    There is a more elegant way to switch between 2 maven+spring profiles simultaneously.

    First, add profiles to POM (pay attention - maven+spring profile is activated by single system variable):

    
        
            postgres
            
                true
                
                    spring.profiles.active
                    postgres
                
            
            
                
                    postgresql
                    postgresql
                    9.1-901.jdbc4
                
            
        
        
            h2
            
                
                    spring.profiles.active
                    h2
                
                       
            
                
                    com.h2database
                    h2
                    1.4.191
                
            
        
    
    

    Second, set default profile for spring (for maven it is already set in POM). For web application, I inserted following lines to web.xml:

    
       spring.profiles.default
       postgres
    
    

    Third, add profile-dependent beans to your config. In my case (XML config), it is:

    
        
        
            
        
        
        
            
                my.test.model
            
        
    
    ...
    
        
            
            
            
            
        
    
    
    
        
            
            
            
            
        
    
    

    Now it is possible to:

    • Run my web-app on Postgres DB with mvn jetty:run or mvn jetty:run -Dspring.profiles.active=postgres commands
    • Run my web-app on H2 DB with mvn clean jetty:run -Dspring.profiles.active=h2

提交回复
热议问题