maven配置详解

 下载地址:Maven – Download Apache Maven,

maven配置详解_第1张图片

添加环境变量:MAVEN_HOME

一、配置文件

maven的配置文件主要有 settings.xml 和pom.xml 两个文件。

1.其中在maven安装目录,例如apache-maven-3.8.1\conf目录下的settings.xml 文件是全局配置文件

2.用户目录的.m2子目录下面的settings.xml的配置只是针对当前用户的配置

3.项目根路径下的pom.xml主要是对当前项目的配置。

局部配置优先于全局配置。 配置优先级从高到低:pom.xml> user settings > global settings

二、settings.xml 配置详解

1.LocalRepository 本地仓库配置:

D:\repository

2.InteractiveMode 用户输入配置:

true

3.离线模式

false

3.插件组,当我们使用某个插件,并且没有在命令行为其提供组织Id(groupId)的时候,Maven就会使用该列表。默认情况下该列表包含了org.apache.maven.pluginsorg.codehaus.mojo


    org.sonarsource.scanner.maven

4.私服服务器配置,配置私服的用户名和密码。配置的私服服务器可以用来发布jar包,与pom.xml 中 发布标签distributionManagement 中配置的仓库ID相互对应。


    
        maven-releases
        developer
        123456
        
        664
        
        775
    

 5.镜像配置


    alimaven
    aliyun maven
    https://maven.aliyun.com/repository/central
    
    *

6.Profiles配置。

settings.xml中的profile元素是pom.xml中profile元素的子集。只包含了id、activation、repositories、pluginRepositories和 properties元素。
如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile。


    
        nexus
        
        
            true
        
        
            
            
                nexus
                gwm nexus
                http://nexus.maven.cn/repository/maven-public/
                
                    true
                
                
                    true
                
            
        
        
        
            
                nexus
                gwm nexus
                http://nexus.maven.cn/repository/maven-public/
                
                    true
                
                
                    true
                
            
        
    
    
        sonar
        
            true
        
        
        
            
                http://localhost:9000
            
            admin
            admin
        
    

7. Activation配置,用来设置profile配置激活的条件逻辑。


  
  false
  
  1.8
  
  
  
    
    mavenVersion
    
    2.0.3
  
  
  
    
    ${basedir}/file2.properties
    
    ${basedir}/file1.properties
  

8.properties 配置,对应profile的扩展属性和pom中properties的属性列表,这些值可以在pom.xml,setting.xml中使用标记${X}来使用,这里X是指属性的名称。

  1.0

9. Repositories 远程仓库配置,可以配置多个。可以配置在标签中,也可以配置在标签中(比较常见,配置在标签中可以根据profile的激活情况动态选择仓库)。配置形式参见《6.Profiles配置》。

10.插件仓库pluginRepositories 和repositories相同。

11. 激活profile配置 activeProfiles,用来激活配置的profile。和 activation 配置相比 activeProfiles 配置比较简单,也比较常用。


        nexus
    

 三、POM.xml配置文件:


    
    
    
    
    
    
    
    ../pom.xml



4.0.0

com.companyname.project-group

1.0


jar



    
    UTF-8
    2.3.7.RELEASE



    
        org.springframework.boot
        spring-boot-starter
        ${spring-boot.version}
        compile
    



    
        
            cn.hutool
            hutool-core
            ${hutool.version}
        
    
     



    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                ${spring-boot.version}
            
        
    
    
    
        
            org.sonarsource.scanner.maven
            sonar-maven-plugin
            3.6.0.1398
        
    
       



    
        aliyun-public
        https://maven.aliyun.com/repository/public
        
            false
        
        
            true
        
    



    
        aliyun-public
        https://maven.aliyun.com/repository/public
        
            false
        
        
            true
        
    




    
        
        maven-releases
        releases
        http://nexus.maven.cn/repository/maven-releases/
        true
    
    
        maven-snapshots
        snapshots
        http://nexus.maven.cn/repository/maven-snapshots/
    




    
        dev
        
            dev
        
        
            true
        
    
    
        prod
        
            prod
        
    

四、远程仓库的加载

maven仓库依赖下载顺序:

1,在settings.xml文件中配置的本地仓库中寻找依赖,没找到则进入第2步。

2,在settings.xml文件中配置的全局远程仓库中寻找,没找到则进入第3步。

3,在当前项目的pom.xml中配置的远程仓库中寻找,如果没找到则进入第4步。

4,在中央仓库 https://repo.maven.apache.org/maven2 中寻找,如果没找到则抛出依赖无法加载异常。

镜像替换:

1,如果在找寻的过程中,如果发现该仓库有镜像匹配,则直接从镜像仓库中加载。

2,如果仓库的 id 设置成 central,则会覆盖 maven 的中央仓库配置。

3,如果镜像 ID 设置为 * 表示匹配所有的仓库,则所有依赖只从此镜像仓库中下载。

4,如果镜像ID 设置为 repo1,repo2,则匹配仓库repo1和repo2,使用逗号分隔多个远程仓库

5,如果镜像ID设置为 *,!repo1匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除

 建议将镜像地址作为一个 普通仓库repository 进行配置,这样可以在其他 仓库下载不了的情况下查找到此仓库。如果配置了镜像仓库代替其他仓库容易出现在镜像中找不到依赖,导致项目无法编译的问题。

 原创文章,引用请注明出处,并联系本人征得本人同意后才可转载。

 

你可能感兴趣的:(Java,maven,java)