maven私服内容补充

1.添加阿里云*仓库

注意Download Remote Indexes选项为True

1.登陆nexus私服(默认账号密码:admin/admin123)

2.点击右侧Repositories

 maven私服内容补充

3.点击add,选择Proxy Repository

 maven私服内容补充

4.配置阿里云地址 http://maven.aliyun.com/nexus/content/groups/public/ , id,name随意填

 maven私服内容补充

5.将阿里云仓库加入仓库组中,并将其排在Central仓库之前

 maven私服内容补充

可供引用的代码:

(一)

  1 <?xml version="1.0" encoding="UTF-8"?>
  2  
  3 <!--
  4 Licensed to the Apache Software Foundation (ASF) under one
  5 or more contributor license agreements.  See the NOTICE file
  6 distributed with this work for additional information
  7 regarding copyright ownership.  The ASF licenses this file
  8 to you under the Apache License, Version 2.0 (the
  9 "License"); you may not use this file except in compliance
 10 with the License.  You may obtain a copy of the License at
 11     http://www.apache.org/licenses/LICENSE-2.0
 12 Unless required by applicable law or agreed to in writing,
 13 software distributed under the License is distributed on an
 14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15 KIND, either express or implied.  See the License for the
 16 specific language governing permissions and limitations
 17 under the License.
 18 -->
 19  
 20 <!--
 21  | This is the configuration file for Maven. It can be specified at two levels:
 22  |
 23  |  1. User Level. This settings.xml file provides configuration for a single user, 
 24  |                 and is normally provided in ${user.home}/.m2/settings.xml.
 25  |
 26  |                 NOTE: This location can be overridden with the CLI option:
 27  |
 28  |                 -s /path/to/user/settings.xml
 29  |
 30  |  2. Global Level. This settings.xml file provides configuration for all Maven
 31  |                 users on a machine (assuming they're all using the same Maven
 32  |                 installation). It's normally provided in 
 33  |                 ${maven.home}/conf/settings.xml.
 34  |
 35  |                 NOTE: This location can be overridden with the CLI option:
 36  |
 37  |                 -gs /path/to/global/settings.xml
 38  |
 39  | The sections in this sample file are intended to give you a running start at
 40  | getting the most out of your Maven installation. Where appropriate, the default
 41  | values (values used when the setting is not specified) are provided.
 42  |
 43  |-->
 44 <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
 45           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 46           xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 47    <localRepository>E:\maven\repository</localRepository>   
 48 
 49   <!-- 配置镜像 -->
 50   <mirrors>      
 51     <mirror>
 52       <!-- 此镜像一般用来作为公司内部开发的版本快照,作为public-snapshots仓库的镜像地址 -->
 53       <!-- 镜像的id,id用来区分不同的mirror元素。 --> 
 54       <id>nexus-public-snapshots</id>
 55       <!-- 被镜像的服务器的id。例如,如果我们要设置了一个Maven*仓库(http://repo1.maven.org/maven2)的镜像,
 56           就需要将该元素设置成central。这必须和*仓库的id “central”完全一致。 -->
 57       <mirrorOf>public-snapshots</mirrorOf>
 58       <!-- 该镜像的URL。 --> 
 59       <url>http://192.168.10.92:8081/nexus/content/groups/public-snapshots</url>
 60     </mirror>
 61     
 62     <mirror>
 63       <!-- 此镜像一般用来作为公司第三方引用基础类库镜像,是所有仓库的镜像地址 -->
 64       <id>nexus</id>
 65       <!-- 为*表示为所有的仓库做镜像,有了这个配置,所有的构建都会包含public组,如果你想包含public-snapshots组,
 66           你必须添加public-snapshots这个Profile,通过在命令行使用如下的 -P 标志:$ mvn -P public-snapshots clean install -->
 67       <mirrorOf>*</mirrorOf>
 68       <url>http://192.168.10.92:8081/nexus/content/groups/public</url>
 69     </mirror>    
 70   </mirrors>
 71   
 72   <!-- settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。它包含了activation, repositories, pluginRepositories 和 properties元素。
 73       这里的profile元素只包含这四个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。  
 74     如果一个settings中的profile被激活,它的值会覆盖任何其它定义在POM中或者profile.xml中的带有相同id的profile。 --> 
 75   <profiles>
 76     <profile>
 77       <id>development</id>
 78       <!-- 仓库。仓库是Maven用来填充构建系统本地仓库所使用的一组远程项目。而Maven是从本地仓库中使用其插件和依赖。
 79           不同的远程仓库可能含有不同的项目,而在某个激活的profile下,可能定义了一些仓库来搜索需要的发布版或快照版构件。有了Nexus,这些应该交由Nexus完成 -->
 80       <repositories>
 81         <repository>
 82           <id>central</id>
 83           <!-- 虚拟的URL形式,指向镜像的URL,因为所有的镜像都是用的是nexus,这里的central实际上指向的是http://repos.d.xxx.com/nexus/content/groups/public -->
 84           <url>http://central</url>
 85           <!-- 表示可以从这个仓库下载releases版本的构件-->
 86           <releases><enabled>true</enabled></releases>
 87           <!-- 表示可以从这个仓库下载snapshot版本的构件 -->
 88           <snapshots><enabled>true</enabled></snapshots>
 89         </repository>
 90       </repositories>
 91      
 92      <!-- 插件仓库。仓库是两种主要构件的家。第一种构件被用作其它构件的依赖。这是*仓库中存储大部分构件类型。
 93          另外一种构件类型是插件。Maven插件是一种特殊类型的构件。由于这个原因,插件仓库独立于其它仓库。
 94         pluginRepositories元素的结构和repositories元素的结构类似。每个pluginRepository元素指定一个Maven可以用来寻找新插件的远程地址。 -->  
 95      <pluginRepositories>
 96         <pluginRepository>
 97           <id>central</id>
 98           <url>http://central</url>
 99           <releases><enabled>true</enabled></releases>
100           <snapshots><enabled>true</enabled></snapshots>
101         </pluginRepository>
102       </pluginRepositories>
103     </profile>
104     
105     <profile>
106       <!--this profile will allow snapshots to be searched when activated-->
107       <id>public-snapshots</id>
108       <repositories>
109         <repository>
110           <id>public-snapshots</id>
111           <!-- 虚拟的URL形式,指向镜像的URL,这里指向的是http://repos.d.xxx.com/nexus/content/groups/public-snapshots -->
112           <url>http://public-snapshots</url>
113           <releases><enabled>false</enabled></releases>
114           <snapshots><enabled>true</enabled></snapshots>
115         </repository>
116       </repositories>
117      <pluginRepositories>
118         <pluginRepository>
119           <id>public-snapshots</id>
120           <url>http://public-snapshots</url>
121           <releases><enabled>false</enabled></releases>
122           <snapshots><enabled>true</enabled></snapshots>
123         </pluginRepository>
124       </pluginRepositories>
125     </profile>
126   </profiles>
127   
128   <!-- 激活的Profile。activation元素并不是激活profile的唯一方式。settings.xml文件中的activeProfile元素可以包含profile的id,
129       任何在activeProfile中定义的profile id,不论环境设置如何,其对应的profile都会被激活。如果没有匹配的profile,则什么都不会发生。
130     profile也可以通过在命令行,使用-P标记和逗号分隔的列表来显式的激活(如,-P test)。
131     要了解在某个特定的构建中哪些profile会激活,可以使用maven-help-plugin(mvn help:active-profiles)。 -->  
132   <activeProfiles>
133       <!-- 没有显示激活public-snapshots -->
134     <activeProfile>development</activeProfile>
135   </activeProfiles>
136  
137 <!-- 自定义本地仓库地址,其默认值为~/.m2/repository 
138 <localRepository>/data/maven-repository</localRepository>
139 -->
140   <!-- 发布的服务器和密码,暂时未限制权限 -->
141    <servers>
142     <server>
143       <!-- 发布的位置在POM中配置,以ID为关联,有很多公用的信息需要配置在POM文件里,最佳实践是定义一个公司级别的root pom -->
144       <id>nexus</id>
145       <username>admin</username>
146       <password>admin123</password>
147     </server>
148     <server>
149       <id>nexus-snapshots</id>
150       <username>admin</username>
151       <password>admin123</password>
152     </server>
153     <server>
154       <id>thirdparty</id>
155       <username>admin</username>
156       <password>admin123</password>
157     </server>
158   </servers>
159 </settings>

(二)

maven连接私服的配置分为两步:

1、配置maven可以连接私服打包上传项目(maven的deploy指令) 
在maven安装目录下的配置文件settings.xml中添加: 
添加到servers标签内部

<server>
      <id>releases</id>
      <username>admin</username>
      <password>admin123</password>
     </server>
     <server>
      <id>snapshots</id>
      <username>admin</username>
      <password>admin123</password>
</server>

在项目中的pom.xml文件中添加:

<distributionManagement>
    <repository>
        <id>releases</id>
    <url>http://localhost:8081/nexus/content/repositories/releases/</url>
    </repository> 
    <snapshotRepository>
        <id>snapshots</id>
    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
    </snapshotRepository> 
</distributionManagement>

2、配置maven可以从私服上下载jar包 
在maven安装目录下的配置文件settings.xml中添加: 
添加到profiles标签内部

<profile>   
    <!--profile的id-->
    <id>dev</id>   
    <repositories>   
      <repository>  
        <!--仓库id,repositories可以配置多个仓库,保证id不重复-->
        <id>nexus</id>   
        <!--仓库地址,即nexus仓库组的地址-->
        <url>http://localhost:8081/nexus/content/groups/public/</url>   
        <!--是否下载releases构件-->
        <releases>   
          <enabled>true</enabled>   
        </releases>   
        <!--是否下载snapshots构件-->
        <snapshots>   
          <enabled>true</enabled>   
        </snapshots>   
      </repository>   
    </repositories>  
     <pluginRepositories>  
        <!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
        <pluginRepository>  
            <!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
            <id>public</id>  
            <name>Public Repositories</name>  
            <url>http://localhost:8081/nexus/content/groups/public/</url>  
        </pluginRepository>  
    </pluginRepositories>  
</profile>  

在配置文件settings.xml中添加用以激活上面的配置:

  <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>