全面详解Maven的配置文件settings.xml

全面详解Maven的配置文件settings.xml

  • setting文件位置
  • maven多仓库查找依赖的顺序大致如下:
  • 基本配置选项
    • localRepository(本地仓库设置)
    • mirrors(镜像设置)
    • servers(服务器)
    • Profiles(配置文件)
    • Active Profiles(激活配置文件Profiles的Profile)

Maven是一个用于构建和管理Java项目的强大工具,它依赖于设置文件来配置和管理其行为。其中最重要的之一便是settings.xml文件。settings.xml文件是Maven的配置文件之一,用于定义Maven的全局设置、仓库、代理、插件、配置和个人用户信息等。这个文件通常存储在Maven安装目录的conf文件夹下。

让我们深入了解settings.xml文件的结构和功能。

setting文件位置

Maven使用setting.xml文件来配置它本身的行为。这个文件通常位于Maven的conf目录下。

  1. 在Windows系统中,它的默认位置**%USER_HOME%.m2\settings.xml**
  2. 在Linux或Mac OS X中,它的默认位置是**~/.m2/settings.xml**。

maven多仓库查找依赖的顺序大致如下:

1.本地仓库
2.项目profile仓库,通过 pom.xml 中的 project.profiles.profile.repositories.repository 配置
3.项目仓库,通过 pom.xml 中的 project.repositories.repository 配置
4.全局profile仓库,通过 settings.xml 中的 settings.repositories.repository 配置
5.镜像仓库,通过 sttings.xml 中的 settings.mirrors.mirror 配置
6.中央仓库,这是默认的仓库

基本配置选项

基本结构
settings.xml文件使用XML格式,其结构包含了Maven的全局设置以及个人或项目特定的配置。下面是一个典型的settings.xml文件的简化版本:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
   
   
   <localRepository>D:\IDEA\my-idea-repositorylocalRepository>

    
    <mirrors>
        
        <mirror>
            <id>alimavenid>
            <mirrorOf>centralmirrorOf>
            <name>aliyun mavenname>
            <url>http://maven.aliyun.com/nexus/content/repositories/central/url>
        mirror>

        
        <mirror>
            <id>repo1id>
            <mirrorOf>centralmirrorOf>
            <name>Human Readable Name for this Mirror.name>
            <url>http://repo1.maven.org/maven2/url>
        mirror>

        
        <mirror>
            <id>repo2id>
            <mirrorOf>centralmirrorOf>
            <name>Human Readable Name for this Mirror.name>
            <url>http://repo2.maven.org/maven2/url>
        mirror>
    mirrors>

settings>

localRepository(本地仓库设置)

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
   
<localRepository>usr/local/mavenlocalRepository> 
  ...
settings>

mirrors(镜像设置)

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  
    <mirrors>  
            
        

        <mirror>

            
            <id>aliyunid>

            
            <name>sjtug maven proxyname>

            
            <url>https://mirrors.sjtug.sjtu.edu.cn/maven-central/url>
            
            <mirrorOf>centralmirrorOf>
        mirror>  
           
    mirrors>  
      ...
settings>
  • id, name:镜像的唯一标识和对用户友好的名称。id用于区分镜像元素,并在连接到镜像时从节中挑选相应的凭据。
  • url:这个镜像的基本url。 构建系统将使用此URL连接到存储库,而不是默认的存储库URL。
  • mirrorOf:作为镜像的存储库id。 例如,要指向Maven中央存储库(https://repo.maven.apache.org/maven2/)的一个镜像,请将此元素设置为central。这必须和中央仓库的id完全一致。

servers(服务器)

用于下载和部署的存储库由项目的pom.xml 的repositories和distributionManagement元素定义。
但是,用户名和密码等某些身份设置不应该与pom.xml一起发布。
这类信息应该存在于构建服务器的settings.xml中。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  
    <servers>
        <server>
            
            <id>server_idid>

            
            <username>auth_usernameusername>

            
            <password>auth_pwdpassword>

            
            <privateKey>path/to/private_keyprivateKey>

            
            <passphrase>some_passphrasepassphrase>

            
            <filePermissions>664filePermissions>

            
            <directoryPermissions>775directoryPermissions>

            
            <configuration>configuration>

        server>
    servers>
  ...
settings>
  • id:这是服务器的ID(不是要作为用户登录的ID),用来匹配Maven试图连接到的存储库/镜像的id元素。
  • username, password:这些元素以一对的形式出现,表示向该服务器进行身份验证所需的登录名和密码。
  • privateKey, passphrase:与前面两个元素一样,这对元素指定私钥(默认为${user.home}/.ssh/id_dsa)的路径和一个passphrase(如果需要的话)。 passphrase和password元素将来可能会外部化,但目前它们必须在settings.xml文件中以明文方式设置。
  • filePermissions、directoryPermissions:文件、目录被创建时的权限。如果在部署的时候会创建一个仓库文件或者目录,这时候就可以使用权限(permission)。这两个元素合法的值是一个三位数字,其对应了unix文件系统的权限,如664,或者775。
    注意:如果使用私钥登录服务器,请确保忽略元素。 否则,该键将被忽略。

Profiles(配置文件)

作用:根据环境参数来调整maven管理的所有项目的配置。
settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了id、activation、repositories、pluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。
如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile。当所有的约束条件都满足的时候就会激活这个profile。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...

    <profiles>

        <profile>

            
            <id>profile_idid>

            
            <activation>

                
                <activeByDefault>falseactiveByDefault>

                
                <jdk>9.9jdk>

                
                <os>

                    
                    <name>Windows XPname>

                    
                    <family>Windowsfamily>

                    
                    <arch>x86arch>

                    
                    <version>5.1.2600version>

                os>

                
                <property>

                    
                    <name>mavenVersionname>

                    
                    <value>2.0.3value>

                property>
                
                
                <file>

                    
                    <exists>/path/to/active_on_existsexists>

                    
                    <missing>/path/to/active_on_missingmissing>

                file>

            activation>
            
            <properties>

                
                <profile.property>this.property.is.accessible.when.current.profile.activedprofile.property>

            properties>

            
            <repositories>
                

                
                <repository>

                    
                    <id>maven_repository_idid>

                    
                    <name>maven_repository_namename>

                    
                    <url>http://host/mavenurl>

                    
                    <layout>defaultlayout>

                    
                    <releases>

                        
                        <enabled>falseenabled>

                        
                        <updatePolicy>alwaysupdatePolicy>

                        
                        <checksumPolicy>warnchecksumPolicy>

                    releases>

                    
                    <snapshots>
                        <enabled />
                        <updatePolicy />
                        <checksumPolicy />
                    snapshots>

                repository>

                

            repositories>

            
            

        profile>

    profiles>
  ...
settings>

激活发生在所有指定的条件都满足时,尽管不是一次需要所有条件。

  • Activation:自动触发profile的条件逻辑。这是profile中最重要的元素。
    跟pom.xml中的profile一样,settings.xml中的profile也可以在特定环境下改变一些值,而这些环境是通过activation元素来指定的。activation元素并不是激活profile的唯一方式。
    settings.xml文件中的元素可以包含profile的id。profile也可以通过在命令行,使用-P选项和id(可以是逗号分隔的列表)来显式的激活(如,-P test)。
  • activeByDefault:当其值为true的时候表示如果没有其他的profile处于激活状态的时候,该profile将自动被激活。
    jdk:表示当jdk的版本满足条件的时候激活,在这里是1.6。这里的版本还可以用一个范围来表示,如 
    [1.4,1.7) 表示1.4、1.5和1.6满足;
    [1.4,1.7] 表示1.4、1.5、1.6和1.7满足;
  • os:表示当操作系统满足条件的时候激活。
  • property:property是键值对的形式,表示当Maven检测到了这样一个键值对的时候就激活该profile。
    当存在属性hello的时候激活该profile。

Active Profiles(激活配置文件Profiles的Profile)

手动激活profiles的列表,按照profile被应用的顺序定义activeProfile。
该元素包含了一组activeProfile元素,每个activeProfile都含有一个profile的id。
任何在activeProfile中定义的profile的id,不论环境设置如何,其对应的 profile都会被激活。如果没有匹配的profile,则什么都不会发生。
例如,env-test是一个activeProfile,则在pom.xml(或者profile.xml)中对应id的profile会被激活。如果运行过程中找不到这样一个profile,Maven则会像往常一样运行。

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  ...
  <activeProfiles>
    <activeProfile>profile_idactiveProfile>
  activeProfiles>
settings>

参考文档
【1】maven笔记(4)settings.xml详解
https://www.cnblogs.com/zhangxl1016/articles/14977862.html
【2】Maven中setting配置文件解析
https://zhuanlan.zhihu.com/p/169969234
【3】Maven的标准settings.xml文件
https://www.cnblogs.com/hepengju/p/11610451.html

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