Maven仓库介绍及使用Nexus搭建Maven仓库(私服)

 一、Maven仓库介绍

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>D://DvptTools//MavenRepository</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>
    -->
    <server>
      <id>nexus-releases</id>
      <username>deployment</username>
      <password>deployment123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>deployment</username>
      <password>deployment123</password>
    </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>central</id>
      <mirrorOf>central</mirrorOf>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
    </mirror>

    <mirror>
      <id>alimaven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyunmaven</name>
      <url>http://maven.aliyun.com/nexus/content/repositories/central</url>
    </mirror>
    -->

    <mirror>
        <id>nexus-public</id>
        <mirrorOf>public</mirrorOf>
        <url>http://192.168.80.29:8081/nexus/content/groups/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>
View Code

Maven仓库介绍。原文地址:Maven的Mirror和Repository 的详细讲解

Maven的安装与配置。参考网址:1)Maven的安装与配置  2)Maven的安装与配置

1 Repository(仓库)

 

1.1 Maven仓库主要有2种:

  • remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问
  • local repository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\Users\{用户名}\.m2\repository目录

1.2 Remote Repository主要有3种:

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

repository里存放的都是各种jar包和maven插件。当向仓库请求插件或依赖的时候,会先检查local repository,如果local repository有则直接返回,否则会向remote repository请求,并缓存到local repository。也可以把做的东西放到本地仓库,仅供本地使用;或上传到远程仓库,供大家使用。 

2 Mirror

mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。

2.1 没有配置mirror:

 

2.2 配置mirror:

 

此时,B Repository被称为A Repository的镜像。

如果仓库X可以提供仓库Y存储的所有内容,那么就可以认为X是Y的一个镜像。换句话说,任何一个可以从仓库Y获得的构件,都胡够从它的镜像中获取。

2.3 <mirrorOf></mirrorOf> 

<mirrorOf></mirrorOf>标签里面放置的是要被镜像的Repository ID。为了满足一些复杂的需求,Maven还支持更高级的镜像配置: 

  • <mirrorOf>*</mirrorOf> 

            匹配所有远程仓库。 

  • <mirrorOf>repo1,repo2</mirrorOf> 

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

  • <mirrorOf>*,!repo1</miiroOf> 

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

3 Repository与Mirror

3.1 定义

其实,mirror表示的是两个Repository之间的关系,在maven配置文件(setting.xml)里配置 了<mirrors><mirror>..........</mirror></mirrors>, 即定义了两个Repository之间的镜像关系。

3.2 目的

配置两个Repository之间的镜像关系,一般是出于访问速度和下载速度考虑。

例如, 有一个项目,需要在公司和住所都编码,并在项目pom.xml配置了A Maven库。在公司,是电信网络,访问A库很快,所以maven管理依赖和插件都从A库下载;在住所,是网通网络,访问A库很慢,但是访问B库很快。这 时,在住所的setting.xml里,只要配置一 下<mirrors><mirror>....</mirror></mirrors>,让B库成为A 库的mirror,即可不用更改项目pom.xml里对于A库的相关配置。

如果该镜像仓库需要认证,则配置setting.xml中的<server></server>即可。

3.3  注意

需要注意的是,由于镜像仓库完全屏蔽了被镜像仓库,当镜像仓库不稳定或者停止服务的时候,Maven仍将无法访问被镜像仓库,因而将无法下载构件。

4 私服

私服是一种特殊的远程Maven仓库,它是架设在局域网内的仓库服务,私服一般被配置为互联网远程仓库的镜像,供局域网内的Maven用户使用。

当Maven需要下载构件的时候,先向私服请求,如果私服上不存在该构件,则从外部的远程仓库下载,同时缓存在私服之上,然后为Maven下载请求提供下载服务,另外,对于自定义或第三方的jar可以从本地上传到私服,供局域网内其他maven用户使用。

优点主要有:

  1. 节省外网宽带
  2. 加速Maven构建
  3. 部署第三方构件
  4. 提高稳定性、增强控制:原因是外网不稳定
  5. 降低中央仓库的负荷:原因是中央仓库访问量太大

 

PS:Maven仓库示意图

 

 

 

二、使用Nexus搭建Maven仓库(私服)

Nexus仓库搭建与Maven集成介绍。

强烈推荐:maven使用Nexus私有仓库进行管理

参考文档:Nexus 私有仓库搭建与 Maven 集成,该文使用的Nexus版本为使用的 Nexus 版本为 Nexus Repository Manager OSS 2.14.5-02。

使用Nexus搭建maven服务器 

Nexus Repository Manager OSS 3.x 安装配置

1、Nexus的安装和使用(Windows),Linux系统参考使用Nexus搭建maven服务器 

本文使用的 Nexus 版本为 Nexus Repository Manager OSS 2.14.5-02 ,之所以不使用最新的 Nexus Repository Manager OSS 3 是因为个人感觉版本3还没有版本2友好,特别是在图形管理界面的操作上。

  首先需要到官网上下载 Nexus Repository Manager OSS 2.14.5-02 的安装包,下载地址为:https://www.sonatype.com/download-oss-sonatype,解压后的目录结构:

   

  在命令行窗口,进入 \nexus-2.14.5-02-bundle\nexus-2.14.5-02\bin 目录,输入 nexus install 命令,安装 Nexus(需要等待一小段时间)。安装成功后,进入服务管理界面,找到一个名为 nexus 的服务,启动该服务,服务启动完成后,打开浏览器,输入 http://localhost:8081/nexus ,如果可以看到欢迎界面,说明 Nexus 安装成功了。

   

  点击右上角的登录按钮,输入管理员账号和密码(默认账号:admin,密码:admin123)登录。

  登录成功后,选择左边 Views/Repositories 菜单下的 Repositories,可以看到一些预设的仓库,我们会用到的一般只有 Public Repositories 和 3rd party , Public Repositories 为公共仓库,3rd party 为第三方仓库,可以上传第三方的 Jar (当然也可以是自己封装的 Jar)。

  

2、增加aliyun仓库

点击“Add”—“Proxy Repository”增加阿里云代理仓库

配置信息如下:

Repository ID: aliyun
Repository Name: aliyu
Repository Type: proxy
Repository Policy: Release
Repository Format: maven2
Contained in groups: 

Remote URL: http://maven.aliyun.com/nexus/content/groups/public/

 

2、第三方jar包管理 3rd party

可以利用我们刚刚搭建的 Nexus 私有仓库,在网上自己下载缺失的 Jar 包,然后以第三方 Jar 的形式上传到私有仓库,依赖信息可以设置跟中央仓库一样。例如添加 jbosscache-core-3.2.1.GA.jar ,在外网上的依赖为:

<dependency>
    <groupId>org.jboss.cache</groupId>
    <artifactId>jbosscache-core</artifactId>
    <version>3.2.1.GA</version>
</dependency>

选择 3rd party,把我们自己下载的  jbosscache-core-3.2.1.GA.jar 上传到 3rd party。在 Artifact Upload 选择卡页面,点击 Select Artifact(s) to Upload 按钮,选择需要上传的 Jar 包,选择完成后点击 Add Artifact 按钮。然后设置 Jar 的 Maven 依赖信息(后续引用 Jar 包需要用到),其中 Maven 依赖的相关信息可以设置成和中央仓库一致,如下图:

  Jar 包上传成功后,切换到 Browse Index 选项卡页面,点击刷新按钮,可以看到我们刚刚上传的 Jar 包,选中 Jar 包我们可以在右边看到 Jar 包对应的 Maven 依赖信息,可以用于项目的构建。

 

三、本地项目配置引用私服

一. 自动发布构件到远程仓库 
1. 在工程的pom.xml中添加:

<distributionManagement>
 
    <repository>
           <id>releases</id><!--这个ID需要与你的release仓库的Repository ID一致-->
           <url>http://ip:8081/nexus/content/repositories/releases</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id><!--这个ID需要与你的snapshots仓库的Repository ID一致-->
        <url>http://ip:8081/nexus/content/repositories/snapshots</url>
    </snapshotRepository>
 
</distributionManagement>

2.修改$MAVEN_HOME\conf目录下的settings.xml配置文件,添加如下配置:

<servers>
     <server>
        <id>releases</id>
        <username>admin</username>
        <password>dev123</password>
    </server>
    <server>
        <id>snapshots</id>
        <username>admin</username>
        <password>dev123</password>
    </server>
  </servers>

3.打开windows命令行,在工程所在目录下执行

mvn deploy

所部署的包就自动上传到了nexus安装目录下的 
这里写图片描述

二.配置Maven从Nexus下载构件 
1.在POM中配置Nexus私服,这样的配置只对当前的Maven项目有效。

<repositories>
      <repository>
          <id>public</id>
          <name>Team Maven Repository</name>
          <url>http://ip:8081/nexus/content/groups/public/</url>
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
              <enabled>true</enabled>
          </snapshots>
      </repository>
  </repositories>

2.在settings.xml中配置profile元素,这样就能让本机所有的Maven项目都使用自己的Maven私服。

 参考:在Maven中设置Nexus私有服务为中央工厂(repository)

 

Nexus Repository Manager OSS 2 配置阿里云私服做代理的坑

 

Nexus Repository Manager OSS 3.x 安装配置

 

目录:

Maven介绍

Maven服务器搭建和包管理

Maven项目创建和配置

 

posted @ 2020-06-03 16:04  十二楼C  阅读(599)  评论(0编辑  收藏  举报