maven多环境部署 Filtering 和 Profile的使用

阅读更多

 

 

 

 

0 前言:

 

在不同的软件开发生命周期阶段、不同的最终客户(用户)环境、不同的运行平台都有可能需要不同配置或资源的情况。假如各个环境下的差别很小的话,我们可以在项目编译之后手工修改或者写个 shell script 自动修改,但如果需要修改的项目很多而且复杂的话,则应该使用  Apache Maven 的 Profile 和 Filtering 功能来解决。(当然前提是你的项目必须是用 Maven 构建)
1 Filtering: Maven Resource Plugin的一个功能, 会使用系统属性或者项目属性的值替换资源文件(*.properties *.xml)当中${...}符号的值
比如 numReduceTasks=${numReduceTasks} 最后在编译执行后变成 : numReduceTasks=3
pom.xml的配置写法:
 	   
	    
            src/main/filters/xuele-${build.profile.id}.properties  指定属性文件,作为下面目录src/main/resources里文件中filtering的来源
        
       
        
            
            
                src/main/resources    ----> 要替换的文件夹
                
                    **/*    -----> 替换的文件 
                
                
                
                
                true     
            
        	
	.....  maven的一些信息
	
 
Profile功能和在pom.xml中的写法:
	运行在pom.xml中定义多个profile段,然后在编译时选择其中一个用于覆盖项目文件原先的定义,如下:
	    
        
        
            dev
            
                true   ---> 项目编译时可以使用-P指定需要使用的profile的id, eg: mvn clean compile -Pdev 其中-P后是id的名称,或者用这个true这个属性设置默认值 
            
            
                dev
            
        

        
        
            test
            
                test
            
        

        
        
            product
            
                product
            
        
    
 
操作后的结果:
E:\workspace\bigdata_project\src\main\filters\bigdata-dev.properties      numReduceTasks=3   这是开发环境的配置文件,文件夹下是不同环境模板参数文件 里面还有 bigdata-pro.properties  bigdata-test.properties等文件
E:\workspace\bigdata_project\src\main\resources\mr_hbase.properties     numReduceTasks=${numReduceTasks}   这是模板文件 
E:\workspace\bigdata_project\target\classes\mr_hbase.properties         numReduceTasks=3               这是编译后的被模板文件替换后的目标文件  
 
 对应的 pom.xml中整体配置贴出如下:

        
        
            dev
            
                true
            
            
                dev
            
        

        
        
            test
            
                test
            
        

        
        
            product
            
                product
            
        
    

    
        
            src/main/filters/bigdata-${build.profile.id}.properties    
        

        
        
            
            
                src/main/resources
                
                    **/*
                
                
                
                
                true
            
        
        
            
                
                
                    
                
            
            
                
            
        
        

            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    UTF-8
                    1.6
                    1.6
                
            

            
                maven-assembly-plugin
                2.4
                
                    
                        jar-with-dependencies
                    
                    
                        assembly.xml
                    
                
                
                    
                        make-assembly 
                        package 
                        
                            single
                        
                    
                
            
        
    
 
 
 

你可能感兴趣的:(maven多环境部署 Filtering 和 Profile的使用)