Maven工具学习使用(九)——Maven Profile

使用profile实现在不同环境下不同方式的构建。

profile激活方式:

命令行激活

使用mvn命令行参数-P加上profile的id来激活profile,多个id之间以逗号分割。例如 -Ptest 激活id为test 的profile

settings文件显示激活

settings中激活的profile对所有项目都处于激活状态。settings.xml文件配置如下:


...
     
        profileid
     
...

settings.xml不支持直接配置repositories和pluginRepositories,可以通过Profile机制在setting.xml中配置仓库,activeProfiles激活profile。当执行Maven构建的时候激活的profile会将仓库配置应用到项目中去。


    
        
            nexus
            
                
                    nexus
                    Nexus
                    http://localhost:8081/nexus/content/groups/public/
                    true
                    true
                
            
            
                
                    nexus
                    Nexus
                    http://localhost:8081/nexus/content/groups/public/
                    true
                    true
                
            
        
    
    
        nexus
    

系统属性激活


    
        
            
            
                test
            
        
    



    
        
            
            
                test
                x
            
        
    
 

操作系统环境激活


    
        
            
            
                test
                x
                x86
                5.1.2600
            
        
    

文件存在与否激活


    
        
            
            
                xxxx.properties
                yyyy.properties
            
        
    

默认激活


    
        
            
            true
        
    

mvn help:active-profiles 查看当前激活的profile

mvn help:all-profiles 查看当前所有的profiles

profile的种类

根据项目需要可以在以下位置声明profile

pom.xml文件,该文件中声明的profile只对当前项目有效。

用户settings.xml,用户目录下.m2/settings.xml中定义的profile对本机上该用户所有的Maven项目有效。

全局settings.xml,Maven安装目录下conf/settings.xml中的profile对本机上所有的Maven项目有效。

profiles.xml(Maven2),可以在项目根目录下使用一个额外的profiles.xml文件来声明profile,该特性在Maven3中被移除,可以将这类profile移到settings.xml中。

profile可以声明的POM元素

不同类型的profile中可以声明的POM元素是不同的,因为pom.xml文件中profile可以能够随着pom.xml一起被提交到代码仓库、Maven本地仓库、Maven远程仓库中,伴随着某个特定的pom.xml一起存在,它可以修改或者增加很多POM元素。

pom profile中可以声明的POM元素


    
    
    
    
    
    
    
        
        
        
        
        
    

修改或添加仓库、插件仓库以及部署仓库地址;

修改或者添加项目依赖;

修改聚合项目的聚合配置;

自由添加或修改Maven属性

添加或修改项目报告配置

添加或修改插件配置、项目资源目录和测试资源目录配置以及项目构建的默认名称。

其他profile可以声明的POM元素

pom以外的profile中不允许添加或者修改绝大部分的pom元素,settings.xml的profile中不允许声明依赖或者插件,pom以外的profile只能声明、、三个元素。仅能用来影响到项目的仓库和Maven属性。


    
    
    

你可能感兴趣的:(Maven构建工具,maven,学习,profile)