Maven使用总结


Maven使用总结

一、Maven的主要作用

  • Maven 翻译为"专家"、"内行",是 Apache 下的一个纯 Java 开发的开源项目
  • Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤
  • Maven 是一个项目管理工具,可以对 Java 项目进行构建、依赖管理

二、Maven仓库

Maven 仓库是项目中依赖的第三方库,这个库所在的位置叫做仓库。

  • 依赖搜索顺序:本地仓库 -> 中央仓库 -> 远程仓库

setting.xml文件配置

  • Level配置

    • User Level:当前用户共享配置,一般在${user.home}/.m2/settings.xml下
    • Global Level:同一计算机上所用用户共享配置,一般在${maven.conf}/settings.xml下
    • 优先级:User Level > Global Level
  • localRepository:本地仓库位置

    • interactiveMode:是否需要和用户交互以获得输入,默认为true
    • offline:如果构建系统要在离线模式下工作,设置为true,默认为false
  • pluginGroups:添加多个pluginGroup子元素列表,内容包含一个groupId, 当使用插件并且命令行中没有提供groupId时,会搜索列表。该列表会自动包含 org.apache.maven.plugins 和 org.codehaus.mojo

  • proxies:设置代理服务器

        
                
                    
                    testProxy
                    
                    true
                    
                    http
                    
                    lky
                    
                    123
                    
                    8099
                    
                    proxy.xxx.com
                    
    			   *.xx1.com|*.xx2.com
                
        
    
  • servers:私服发布的用户名密码

        
                
                    
                    server1
                    
                    wang
                    
                    123
                
                
                    server2
                    lky
                    456
                
        
    
  • mirrors: 用于定义一系列的远程仓库的镜像

       
        
        
          alimaven
          central
          aliyun maven
          https://maven.aliyun.com/nexus/content/repositories/central/
        
    
        
        
          repo1
          central
          Human Readable Name for this Mirror.
          http://repo1.maven.org/maven2/
        
    
        
        
          repo2
          central
          Human Readable Name for this Mirror.
          http://repo2.maven.org/maven2/
        
      
    
    • profiles:settings.xml中的profile是pom.xml中的profile的简洁形式。它包含了激活(activation),仓库(repositories),插件仓库(pluginRepositories)和属性(properties)元素

      • activation:当满足所有指定的条件时,就会激活该profile
      • repositories:当profile被激活时定义在profile里的远程仓库将被使用
      • pluginRepositories: 插件仓库, 结构与repositories类似
      • properties:定义参数,可以在pom文件中通过${属性名}引用

1、本地仓库配置

  • 默认位置: ${user.home}/.m2/repository
  • 修改默认位置:在maven目录的conf/setting.xml文件中 节点修改位置

2、中央仓库配置

  • 仓库由 Maven 社区管理,不需要进行配置,需要网络才能访问
  • 中央仓库访问地址: http://search.maven.org/#browse

3、远程仓库配置

三、Pom.xml文件配置

  • 常见Pom节点含义

    
    4.0.0
    
     
    asia.banseon
    
    banseon-maven2
    
    banseon-maven
    
    http://www.baidu.com/banseon
    
    A maven project to study maven.
    
    
     
        UTF-8
    
    
    
    
        
            
                ......
            
        
    
    
     
    
        
        
        
    
    
    
    
        
        
            ......
        
    

四、Maven构建生命周期

阶段 处理 描述
验证 validate 验证项目 验证项目是否正确且所有必须信息是可用的
编译 compile 执行编译 源代码编译在此阶段完成
测试 Test 测试 使用适当的单元测试框架(例如JUnit)运行测试。
包装 package 打包 创建JAR/WAR包如在 pom.xml 中定义提及的包
检查 verify 检查 对集成测试的结果进行检查,以保证质量达标
安装 install 安装 安装打包的项目到本地仓库,以供其他项目使用
部署 deploy 部署 拷贝最终的工程包到远程仓库中,以共享给其他开发人员和工程

五、Snapshot 版本与 Release 版本

  • Snapshot 版本代表不稳定、尚处于开发中的版本。Release 版本则代表稳定的版本

  • SNAPSHOT使用场景:协同开发时,如果 A 依赖构件 B,由于 B 会更新,B 应该使用 SNAPSHOT 来标识自己