Can I inject properties from Maven (passwords defined in settings.xml) into my Spring container?

后端 未结 4 1275
自闭症患者
自闭症患者 2020-12-25 15:08

I define passwords to servers via properties I define in my ~/.m2/settings.xml (could be anywhere, though, including pom.xml) for my deployment plugin. I\'d like to use the

4条回答
  •  遥遥无期
    2020-12-25 15:25

    Sure. Maven resource filtering is the way to go.

    Here's a sample configuration (files matching *-context.xml will be filtered, others won't):

    
        
            
                src/main/resources
                true
                
                    **/*-context.xml
                
            
            
                src/main/resources
                false
                
                    **/*-context.xml
                
            
        
    
    

    A different approach would be to use the Properties Maven Plugin to write all project properties to a file and reference that file from Spring using the PropertyPlaceholderConfigurer mechanism.

    Maven Configuration:

    
        
            
                org.codehaus.mojo
                properties-maven-plugin
                1.0-alpha-2
                
                    
                        generate-test-resources
                        
                            write-project-properties
                        
                        
                            ${project.build.testOutputDirectory}/mavenproject.properties
                        
                    
                
            
        
    
    

    Spring configuration:

    
    

提交回复
热议问题