Maven 学习(二)

Maven 学习(二)

Maven 第二篇,本篇核心主要讲Maven在基础使用。包含安装、配置等。着重于开发人员在开发中如何使用、配置Maven

1. Maven安装

前提: 之前说过Maven 依赖于Jdk, 且不同的Maven版本支持的Jdk版本不一样。所以在安装之前建议安装好指定Jdk环境。这里默认已经安装好了相关外在环境。

1.1 软件下载源

要想安装一个软件,势必需要先得到这个软件(本地)。Maven的下载源很清晰。如下地址:

下载的版本有类型有几种,一般Linux环境 选择tar.gz, windows环境选择 zip.

*-source是源码版本 一般在没有特殊要求情况下 下载bin版本即可。

1.2 安装

这里安装以window环境下的Jdk1.8、maven 3.3.9 版本为例说明,其他版本参考即可。

  1. 建立系统环境变量。一般创建一个 MAVEN_HOME,值为maven的下载文件路径。补充Path系统环境变量,补充内容是 %MAVEN_HOME%\bin; 即可,Path环境变量中有非常多参数,各个参数是以分号(;)结尾,如果前面参数没有分号,自己补充在填写内容。
  2. 验证。打开命令行环境,输入 mvn -v ,展示maven的版本信息即安装成功。

maven的安装非常简单。

2. settings.xml

settings.xml 是Maven的核心配置,也是通用配置,存放在 %MAVEN_HOME%\conf 目录中。在这里主要针对基本属性进行基础性介绍。

这里仅针对部分属性进行介绍

2.1 localRepository

本地仓库配置。默认 ${user.home}/.m2/repository ,用于存放依赖

<localRepository>D:/repo</localRepository>
2.2 offline

是否离线,当执行一个构建的时候 是否连接互联网。默认为 false, 这将对 artiafct(可以称为依赖) 下载、部署等有影响。

 <offline>false</offline>
2.3 proxies

代理列表,用于当前机器连接网络,除非命令行等指定,默认第一个代理配置被标记为active。换句话说就是列表中的第一个会生效。

  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>
2.4 servers

服务器列表配置,用于连接远程服务器。具体就不说了, 目前我个人没用到过

  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>
2.5 mirrors

一系列镜像清单,用于从远程仓库中下载 artifact (构件),它的工作原理是这样的,pom配置了 repository用来解决artifact问题,但是有时候仓库访问会阻塞,所以人们设计了镜像,将这个仓库镜像到多个地方,每个仓库都有一个唯一的id, 所以我们可以和仓库关联起来,作为可选的下载地址,这个镜像站点将是首先服务器,国内常见的镜像有 阿里云、华为等。

仓库有一个特定的id,对应的是镜像里面的mirrorOf属性值

镜像里面的id属性 是针对镜像唯一性而言,而mirrorOf属性指向的是远程仓库。

<mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
       // id具有唯一性
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
</mirrors>
2.6 profiles
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>
2.7 activeProfiles

生效的profiles 可以指定多个。

值对应 profile.id

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>

3. Maven镜像及配置

参考: https://maven.apache.org/guides/mini/guide-mirror-settings.html

镜像设置在settings.xml中是比较重要的,里面配置了maven镜像地址及规则。其实第二节已经简单说了镜像的基础配置,不太深入。这里在进一步说明。

首先说镜像这个概念,从文字也知道,是源仓库的复制品,重新部署的一个站点,方便你下载资源。

使用镜像的理由:

  1. 镜像资源是同步源仓库的,位置更近更快,例如阿里镜像,因为国内缘故,下载资源更快
  2. 你想用你自己内部的仓库去替换一个特定的仓库,方便更好的控制
  3. 你想运行一个本地仓库管理器为镜像提供本地缓存,和需要使用它的url

配置给定仓库的镜像,具体如下:

mirror中属性解释

1. id 镜像的唯一标识
2. name 镜像名称
3. **mirrorOf 被镜像的仓库的id标识 默认的仓库id为central, mirrorOf id只能唯一 如果多个 默认第一个匹配**
4. url 镜像仓库地址
<settings>
  ...
  <mirrors>
    <mirror>
      <id>other-mirror</id>
      <name>Other Mirror Repository</name>
      <url>https://other-mirror.repo.other-company.com/maven2</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>

Maven官方仓库地址 : https://repo.maven.apache.org/maven2

为了实现这一点,mirrorOf使用 *, 全匹配, 这个功能仅支持Maven2.5+版本。

<settings>
  ...
  <mirrors>
    <mirror>
      <id>internal-repository</id>
      <name>Maven Repository Manager running on repo.mycompany.com</name>
      <url>http://repo.mycompany.com/proxy</url>
      <mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>
  ...
</settings>

Maven 镜像就是一种远程仓库

4. 个人总结

这篇文章主要是对Maven的安装和使用,以及对settings.xml文件的基本说明。重点是要了解Maven镜像知识。

Maven mirror 是一个用于加速 Maven 库下载的服务器。Maven 是一个常用的 Java 应用程序构建工具,它使用一个中央仓库来存储所有的依赖项和插件。Maven mirror 就是在其他服务器上保存了中央仓库中的内容的一个副本,因此在使用 Maven 构建应用程序时,可以从这个副本中快速访问依赖项和插件。这样做的主要目的是减少从中央仓库中下载内容的延迟,提高构建应用程序的效率。

Maven中央仓库地址: https://repo.maven.apache.org/maven2

如果你配置了一个镜像,Maven首先会访问该镜像的URL,如果找不到资源,Maven才会去这个中央仓库去查找,关于Maven的中央仓库,它默认是maven官方提供的中央仓库,但是你也可以通过配置指定其他中央仓库,这于镜像配置的过程类似。

id 唯一即可

<repositories>
  <repository>
    <id>my-repo</id>
    <url>http://my.repo.com/maven</url>
  </repository>
</repositories>

<mirrors>
  <mirror>
    <id>my-mirror</id>
    <mirrorOf>central</mirrorOf>
    <url>http://my.mirror.com/maven</url>
  </mirror>
</mirrors>

最后 镜像的设计,其实是为了减少对Maven官方中央仓库的访问,从而缓解对官方中央仓库的压力,提高访问速度,一般镜像仓库通常设置在国内,访问距离短,大大提高访问速度。支持备份、支持自定义优点。

这种设计模式,其实在很多软件设计、甚至工业设计中被广泛运用的。例如Redis

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值