maven--镜像和仓库的用法

原文网址:maven--镜像和仓库的用法_-CSDN博客

本文介绍maven镜像和仓库的用法。

加快速度的配置

settings.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<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
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>E:\work\develop_env\maven\repository\</localRepository>

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <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>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <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>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <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>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <id>nexus-aliyun</id>
      <!-- <mirrorOf>central</mirrorOf> -->
      <mirrorOf>*</mirrorOf>
      <name>阿里云公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <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>

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

仓库和镜像

仓库

指的就是repository,项目中依赖的第三方库,这个库所在的位置叫做仓库。MAVEN仓库能帮助我们管理,它是放置所有JAR、WAR,ZIP,POM等等的地方。

镜像

指的是mirrors,它是一个拦截器,它会拦截MAVEN对远程仓库的相关请求,把请求里的远程仓库的地址,重新定向到镜像里配置的地址。修改镜像一般是出于网速考虑。

镜像本身并不是repository,它只是远程repository的网络加速器。 很多internal repository搭建工具往往也提供mirror服务,比如Nexus就可以让同一个URL,既用作internal repository,又使它成为所有repository的mirror。

镜像

简介

修改镜像方法

配置文件:conf/settings.xml (maven解压目录)

<settings>
  ...
  <mirrors>
    <mirror>
      <id>nexus-aliyun</id>
      <!-- <mirrorOf>central</mirrorOf> -->
      <mirrorOf>*</mirrorOf>
      <name>阿里云公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>
  ...
</settings>

概述

如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y获得的构件,都能够从它的镜像中获取。举个例子,http://maven.net.cn/content/groups/public/ 是中央仓库http://repo1.maven.org/maven2/ 在中国的镜像,由于地理位置的因素,该镜像往往能够提供比中央仓库更快的服务。

镜像的拦截作用

mirror相当于一个拦截器,它会通过<mirrorOf> xxx </mirrorOf>,把符合xxx条件的remote repository的请求重定向到mirror里配置的地址。

mirrorOf用法

配置

说明

<mirrorOf>*</mirrorOf>

匹配所有远程仓库。

<mirrorOf>central,repo2</mirrorOf>

匹配仓库central和repo2,使用逗号分隔多个远程仓库。

<mirrorOf>external:*</mirrorOf>

匹配所有不在本机上的远程仓库。(使用localhost的除外,使用file://协议的除外。)

<mirrorOf>*,!repo1</miiroOf>

匹配所有远程仓库,repo1除外,使用感叹号将仓库从匹配中排除。

mirror就是镜像,主要提供一个方便地切换远程仓库地址的途径。比如:上在公司,用电信的网络,连电信的仓库。回到家,是网通的网络,连网通的仓库。这样就可以通过mirror配置,统一把我工程里的仓库地址都改成联通的,而不用到具体工程配置文件里一个一个地改地址。 

可以配置Maven使用该镜像来替代中央仓库。编辑conf/settings.xml,代码如下:

​
<settings>
  ...
  <mirrors>
    <mirror>
      <id>nexus-aliyun</id>
      <mirrorOf>central</mirrorOf>
      <name>阿里云公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>
  ...
</settings>

该例中,<mirrorOf>的值为central,表示该配置为中央仓库的镜像。这样配置之后,以后向central这个仓库发的请求都会发到http://maven.aliyun.com/nexus/content/groups/public而不是默认的http://repo1.maven.org/maven2/

镜像大全

1、阿里的镜像地址

另见:仓库服务

<mirror>
  <id>nexus-aliyun</id>
  <!-- <mirrorOf>central</mirrorOf> -->
  <mirrorOf>*</mirrorOf>
  <name>阿里云公共仓库</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>

2、华为的镜像地址

<mirror>
    <id>huaweicloud</id>
    <name>mirror from maven huaweicloud</name>
    <url>https://mirror.huaweicloud.com/repository/maven/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

使用华为Maven中央仓库时,需要在servers节点增加一个server子节点,内容如下:

<server>
    <id>huaweicloud</id>
    <username>anonymous</username>
    <password>devcloud</password>
</server>

经过实际的生产使用后,貌似阿里的要比华为的要快些!

3、ibiblio 镜像地址(这个也比较快的呃)

<mirror>
    <id>ibiblio</id>
    <name>Mirror from Maven ibiblio</name>
    <url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

4、repo1.maven.org 镜像地址

<mirror>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>http://repo1.maven.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

5、repo1.maven.apache.org 镜像地址

<mirror>
    <id>central</id>
    <name>Maven Repository Switchboard</name>
    <url>http://repo1.maven.apache.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

6、repo2 镜像地址

<mirror>
    <id>repo2</id>
    <name>Mirror from Maven Repo2</name>
    <url>http://repo2.maven.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

7、spring.io 镜像地址

<mirror>
    <id>sprintio</id>
    <mirrorOf>central</mirrorOf>
    <name>Human Readable Name for this Mirror.</name>
    <url>https://repo.spring.io/libs-snapshot/</url>
</mirror>

8、UK 镜像地址

<mirror>
    <id>ui</id>
    <name>Mirror from UK</name>
    <url>http://uk.maven.org/maven2/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

9、JBoss 镜像地址

<mirror>
    <id>jboss-public-repository-group</id>
    <name>JBoss Public Repository Group</name>
    <url>http://repository.jboss.org/nexus/content/groups/public</url>
    <mirrorOf>central</mirrorOf>
</mirror>

10、Google 镜像地址

<mirror>
    <id>google</id>
    <name>google maven</name>
    <url>https://maven.google.com/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

11、Maven china镜像地址

<mirror>
    <id>maven.net.cn</id>
    <name>Mirror from Maven in china</name>
    <url>http://maven.net.cn/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

12、Maven oschina镜像地址

<mirror>
    <id>CN</id>
    <name>OSChinaCentral</name>
    <url>http://maven.oschina.net/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>

仓库

简介

maven里有两种仓库,Local Repository(本地仓库)和Remote Repository(远程仓库)。

本地仓库

默认放到此路径:C:\User\xxx\.m2\repository。此仓库随着使用会比较大,几个G

修改本地仓库路径的方法

配置文件:conf/settings.xml (maven解压目录)

本地库查找顺序(依次按照以下顺序查找)

  1. %USER_HOME%/.m2/settings.xml中指定的路径
  2. %M2_HOME%/conf/settings.xml中指定的路径
  3. %USER_HOME%/.m2/repository

远程仓库

修改远程仓库方法:pom.xml

<project ...>
	<repositories>
        <repository>  
		    <id>alimaven</id>
		    <name>aliyun maven</name>
		    <url>https://maven.aliyun.com/repository/public/</url>
		</repository> 
    </repositories>
</project>

Remote Repository(远程仓库)主要有3种

  • 中央仓库(central repository):http://repo1.maven.org/maven2/
  • 私服(internal repository):内网自建的maven repository,其URL是一个内部网址
  • 其他公共仓库:其他可以互联网公共访问maven repository,例如 jboss repository等。

仓库大全

1、阿里中央仓库(首推1)

<repository>  
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/public/</url>
</repository> 

2、camunda.com 中央仓库(首推2)

<repository>  
    <id>activiti-repos2</id>  
    <name>Activiti Repository 2</name>  
    <url>https://app.camunda.com/nexus/content/groups/public</url>  
</repository>  

3、alfresco.com 中央仓库(首推3)

<repository>  
    <id>activiti-repos</id>  
    <name>Activiti Repository</name>  
    <url>https://maven.alfresco.com/nexus/content/groups/public</url>  
</repository>  

4、maven.apache.org 中央仓库

<repository>  
    <id>central-repos</id>  
    <name>Central Repository</name>  
    <url>http://repo.maven.apache.org/maven2</url>  
</repository>

5、maven.org 中央仓库

<repository>  
    <id>central-repos1</id>  
    <name>Central Repository 2</name>  
    <url>http://repo1.maven.org/maven2/</url>  
</repository>

6、spring.io 中央仓库

<repository>  
    <id>springsource-repos</id>  
    <name>SpringSource Repository</name>  
    <url>http://repo.spring.io/release/</url>  
</repository>

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
如果你在使用 Maven 时遇到找不到插件 maven-assembly-plugin 的问题,可以尝试以下解决方法: 1. 检查 Maven 的配置文件 settings.xml 中是否配置了正确的镜像源。可以在该文件中添加如下配置: ``` <mirrors> <mirror> <id>aliyunmaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> ``` 这个配置会将 Maven 的默认镜像源替换为阿里云的镜像源,由于阿里云的镜像源是国内访问速度较快的镜像源之一,可以尝试使用这个配置来解决找不到插件的问题。 2. 检查 Maven 的本地仓库中是否已经下载了 maven-assembly-plugin 插件。可以在本地仓库的目录中查找是否有以下路径存在: ``` ~/.m2/repository/org/apache/maven/plugins/maven-assembly-plugin/ ``` 如果该路径不存在,说明 Maven 还没有下载该插件,可以尝试执行以下命令来下载该插件: ``` mvn dependency:get -Dartifact=org.apache.maven.plugins:maven-assembly-plugin:3.3.0 ``` 这个命令会下载 maven-assembly-plugin 插件的最新版本到本地仓库中。 3. 检查 Maven 的 pom.xml 文件中是否正确引入了 maven-assembly-plugin 插件。可以在 pom.xml 文件中添加以下配置: ``` <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> </plugin> </plugins> </build> ``` 这个配置会让 Maven 在构建时自动引入 maven-assembly-plugin 插件,如果该插件已经下载到本地仓库中,就可以正常使用了。 如果以上方法都无法解决问题,可以尝试在 Maven 的命令中加入 -U 参数来强制更新本地仓库中的插件版本。命令如下: ``` mvn clean package -U ``` 这个命令会清除 Maven 的缓存,并从远程仓库下载最新的插件版本到本地仓库中。如果该插件版本存在问题,可以尝试使用其他版本来解决问题。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT利刃出鞘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值