How can I change a .properties file in maven depending on my profile?

后端 未结 1 912
清歌不尽
清歌不尽 2020-12-04 10:25

How can I change a .properties file in maven depending on my profile? Depending on whether the application is built to run on a workstation or the datacenter parts of the f

1条回答
  •  盖世英雄少女心
    2020-12-04 10:50

    As often, there are several ways to implement this kind of things. But most of them are variations around the same features: profiles and filtering. I'll show the most simple approach.

    First, enable filtering of resources:

    
      ...
      
        
          
            src/main/resources
            true
          
        
        ...
      
    
    

    Then, declare a place holder in your src/main/resources/my_config.properties, for example:

    myprop1 = somevalue
    myprop2 = ${foo.bar}
    

    Finally, declare properties and their values in a profile:

    
      ...
      
        
          env-dev
          
            
              env
              dev
            
          
          
            othervalue
          
        
        ...
      
    
    

    And run maven with a given profile:

    $ mvn process-resources -Denv=dev
    [INFO] Scanning for projects...
    ...
    $ cat target/classes/my_config.properties 
    myprop1 = somevalue
    myprop2 = othervalue
    

    As I said, there are variation around this approach (e.g. you can place values to filter in files), but this will get you started.

    References

    • Introduction to Build Profiles
    • Maven Resources Filtering

    More resources

    • A Maven2 multi-environment filter setup
    • Maven project filtering
    • Using Maven profiles and resource filtering
    • Building For Different Environments with Maven 2

    0 讨论(0)
提交回复
热议问题