1-Maven-settings配置

作者 : admin 本文共4906个字,预计阅读时间需要13分钟 发布时间: 2024-06-16 共1人阅读

1-Maven-settings配置

整理下Maven工具的使用。

【本地仓库、私服、镜像仓库、远程仓库、中央仓库】

本文基于阅读其他博客和对公司Maven配置的学习整理出来的。希望通过本此学习能对Maven有个整体性的掌控。

顺序:profile.repository > pom文件中的repository > mirror

配置文件整体结构

<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/>
    
    <interactiveMode/>
    
    <offline/>
    <pluginGroups/>
    <servers/>
    <mirrors/>
    
    <proxies/>
    <profiles/>
    <activeProfiles/>
</settings>

比较常用的:

  • localRepository :配置本地存储库的位(只有把包下载到本地仓库才能在项目中引用)
  • servers: 配置私服的用户名和密码(配合pom.xml中的 使用,用来把我们本地打的包推到配置的远程仓库中)
  • mirrors: mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
  • profiles: 配置环境
  • activeProfiles: 配置默认激活的环境

1-Maven-settings配置插图

配置文件各部分详解

①servers标签

用来配置私服的账号密码等权限相关信息。不管是repository还是mirror还是distributionManagement亦或者其它的不管是在pom.xml文件中还是在settings.xml中需要用户名密码等权限的都需要通过配置server标签来配置对应的权限。他们之间是通过彼此的id来关联的。

总之 server就是用来配置权限的,和其他标签关联都是用id关联的。

    
    <servers>
        <server>
            
            <id>server_id</id>

            
            <username>auth_username</username>

            
            <password>auth_pwd</password>

            
            <privateKey>path/to/private_key</privateKey>

            
            <passphrase>some_passphrase</passphrase>

            
            <filePermissions>664</filePermissions>

            
            <directoryPermissions>775</directoryPermissions>

            
            <configuration></configuration>

        </server>
    </servers>

如图:如果使用私服必然会涉及到配置其用户名密码等权限信息,则需要通过两者的来关联上。

1-Maven-settings配置插图(1)

如图:如果需要推送到私服的话需要配置并通过关联。

1-Maven-settings配置插图(2)

mirrors标签

mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。例如当您无法连接上国外的仓库是, 可以指定连接到国内的镜像服务器。【也可以把mirror理解为一个repository,因为如果代理服务器也需要用户名密码的话,也需要配置对应的server并且也是通过彼此的id进行关联的】

  • !!!私服的配置推荐用profile配置而不是mirror
<mirrors>
         
        <mirror>
            
            <id>sjtugmaven</id>
            
            <name>sjtug maven proxy</name>
            
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            
            <mirrorOf>central</mirrorOf>
        </mirror>
    </mirrors>
  • mirrorOf标签的详解:被镜像的服务器的id, 必须与 repository 节点设置的 ID 一致。

    central

    对于 central 这值是比较特殊。因为如果我们没有配置repository的话。maven是默认一个远程仓库的就是他们的中央仓库。而这个中央仓库的id就是central。但是如果我们自己配置了私服等也可以设置repository中的id为central。所以当这里的central代指什么需要看下我们自己有没有配置对应的仓库如果没有自定义的仓库的id等于central的话,那这里的central就代表的是maven中央仓库的id。

  • 如果配置了 mirrorOf = *, 则 不管项目的 pom.xml 配置了什么仓库, 最终都会被镜像到 镜像仓库。

  • 如果代理服务器也需要用户名密码的话,也需要配置对应的server并且也是通过彼此的id进行关联。

    1-Maven-settings配置插图(3)

阿里云云效Maven地址:https://developer.aliyun.com/mvn/guide

③profiles标签

构建方法的配置清单, maven 将根据不同环境参数来使用这些构建配置。

如果 settings 中的 profile 被激活, 它的值会覆盖任何其它定义在 pom.xml 中或 profile.xml 中的相同 id 的 profile。

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

    <profiles>
<profile>

<id>profile_id</id>

<activation>

<activeByDefault>false</activeByDefault>

<jdk>21</jdk>

<os>

<name>Windows XP</name>

<family>Windows</family>

<arch>x86</arch>

<version>5.1.2600</version>
</os>

<property>

<name>mavenVersion</name>

<value>2.0.3</value>
</property>

<file>

<exists>/path/to/active_on_exists</exists>

<missing>/path/to/active_on_missing</missing>
</file>
</activation>

<properties>

<profile.property>this.property.is.accessible.when.current.profile.actived</profile.property>
</properties>

<repositories>
<repository>

<id>maven_repository_id</id>

<name>maven_repository_name</name>

<url>http://host/maven</url>

<layout>default</layout>

<releases>

<enabled>false</enabled>

<updatePolicy>always</updatePolicy>

<checksumPolicy>warn</checksumPolicy>
</releases>

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

</repositories>

<!--
-->
</profile>
</profiles>
  • 标签详解:

    是用来配置profile的激活条件的。当系统满足 标签里面子标签的条件时就会把当前profile设置为有效。

    其他激活方式:

    1. 通过 settings.xml 文件中的标签配置的值为对应标签的id。(或者在idea中勾选!)。
    2. 在命令行, 使用-P标记和逗号分隔的列表来显式的激活, 如: mvn clean package -P 的id)。
  • 标签详解:

扩展属性设置。扩展属性可以在 POM 中的任何地方通过 ${扩展属性名} 进行引用。

属性引用方式(包括扩展属性, 共 5 种属性可以引用):

  1. env.x : 引用 shell 环境变量, 例如, “env.PATH”指代了 $path 环境变量(在 Linux / Windows 上是 %PATH% )
  2. project.x : 引用 pom.xml(根元素就是 project)中 xml 元素内容.例如 ${project.artifactId} 可以获取pom.xml 中设置的 元素的内容
  3. settings.x : 引用 setting.xml(根元素就是 setting) 中 xml 元素内容, 例如 ${settings.offline}
  4. Java System Properties : 所有可通过 java.lang.System.getProperties() 访问的属性都能在通过 ${property_name} 访问, 例如 ${java.home}
  5. x : 在 或者 外部文件 中设置的属性, 都可以 ${someVar} 的形式使用
  • 标签详解:

    用来设置远程仓库

    releases vs snapshots

    maven 针对 releases、snapshots 有不同的处理策略, POM 就可以在每个单独的仓库中, 为每种类型的 artifact 采取不同的策略

    例如:

    ​ 开发环境 使用 snapshots 模式实时获取最新的快照版本进行构建

    ​ 生成环境 使用 releases 模式获取稳定版本进行构建

    依赖包不更新问题:

    1. Maven 在下载依赖失败后会生成一个.lastUpdated 为后缀的文件。如果这个文件存在, 那么即使换一个有资源的仓库后, Maven依然不会去下载新资源。可以通过 -U 参数进行强制更新、手动删除 .lastUpdated 文件:
      find . -type f -name “*.lastUpdated” -exec echo {}” found and deleted” ; -exec rm -f {} ;
    2. updatePolicy 设置更新频率不对, 导致没有触发 maven 检查本地 artifact 与远程 artifact 是否一致
  • ..or.

    jar包更新策略

    每次执行构建命令时, Maven 会比较本地 POM 和远程 POM 的时间戳, 该元素指定比较的频率。

    有效选项是:

    1. always(每次构建都检查)

    2. daily(默认, 距上次构建检查时间超过一天)

    3. interval: x(距上次构建检查超过 x 分钟)

    4. never(从不)

    重要: 设置为 daily, 如果 artifact 一天更新了几次, 在一天之内进行构建, 也不会从仓库中重新获取最新版本

④activeProfiles标签

手动激活 profiles 的列表, 按照 profile 被应用的顺序定义 activeProfile。任何 activeProfile, 不论环境设置如何, 其对应的 profile 都会被激活, maven 会忽略无效(找不到)的 profile。

<activeProfiles>
<activeProfile>not-exits-profile</activeProfile>
</activeProfiles>

profile的激活方式:上面将profile标签的时候就有讲过激活方式。是其中一种激活方式。

④activeProfiles标签

手动激活 profiles 的列表, 按照 profile 被应用的顺序定义 activeProfile。任何 activeProfile, 不论环境设置如何, 其对应的 profile 都会被激活, maven 会忽略无效(找不到)的 profile。

<activeProfiles>
<activeProfile>not-exits-profile</activeProfile>
</activeProfiles>

profile的激活方式:上面将profile标签的时候就有讲过激活方式。是其中一种激活方式。

本站无任何商业行为
个人在线分享 » 1-Maven-settings配置
E-->