Maven搭建私有仓库填坑日记

记录下,阿里仓库web地址:https://repo1.maven.org/maven2/

1.拉取Nexus镜像

docker search nexus #查找可用的镜像
docker pull docker.io/sonatype/nexus3 #拉取镜像

在这里插入图片描述

2.启动Nexus

mkdir -p /usr/local/nexus3/nexus-data #创建目录用于挂载放置nexus的数据
chomd -R 200 /usr/local/nexus3/nexus-data #修改目录权限
docker run -tid -p 8081:8081 --name nexus --privileged=true -e NEXUS_CONTEXT=nexus -v /usr/local/nexus3/nexus-data:/nexus-data docker.io/sonatype/nexus3 #启动nexus
注意:–privileged=true 是为了让容器中的root具有系统的root权限,以便可以操作挂载的目录,因为我即使修改了目录权限,在启动容器是仍然启动不了,告诉我权限不够。

在这里插入图片描述

3.访问Nexus管理系统

访问nexus:http://192.168.3.101:8081/nexus/
用户名:admin
密码:在容器中/nexus-data/admin.password中查看,进入系统后会要求更改密码
在这里插入图片描述

4.管理仓库

使用create repository可以创建仓库:
在这里插入图片描述
在这里插入图片描述

看起来貌似Nexus不仅仅只能管理maven仓库。
maven有3种类型仓库:

  • proxy:提供代理其它仓库的类型
  • hosted:本地存储。像官方仓库一样提供本地私库功能
  • group:组类型,能够组合多个仓库为一个地址提供服务

proxy

proxy是代理库,我们是无法向这个库里面推送我们自己的依赖的。
阿里云maven查看:https://developer.aliyun.com/mvn/guide
在这里插入图片描述
如下图配置就是建立一个my-proxy库来代理https://maven.aliyun.com/repository/public仓库,当从my-proxy拉取依赖的时候,如果私有仓库中没有对应的包,则会通过代理仓库https://maven.aliyun.com/repository/public去拉取。在这里插入图片描述

hosted

hosted是则可以作为一个私有仓库,我们自己的包可以推送到这个仓库供公司内部其他人员调用。

version policy(版本策略):

  • Relese:用于存放relese版本的依赖包。
  • Snapshot: 用于存放快照,快照是随时可能变更的,所以如果项目依赖快照,则每次构建时,maven都会远程仓库拉取最新的快照,这样对于正在开发的依赖的发布和引用时非常适用的。对于快照具体的讲解请参考:https://www.runoob.com/maven/maven-snapshots.html
  • Mixed:这个类型则是用于存放混合类型的。也就是既可以放Relese也可以放Snapshot。

发布策略:

对于hosted仓库我们还需要设置发布策略为允许重复发布的(当然,Relese库严格来讲应该是不允许重复发布)。

  • Allow redeploy:允许重复发布
  • Disable redeploy:不允许发布
  • Read-Only:只读
  • Deploy by Replication Only:仅通过复制部署

group

group的作用就是构建一个组,可以将前两种仓库进行组合,然后统一对外提供maven管理。这个类型的仓库也不接受依赖推送。
如下图是Nexus默认提供的一个group类型的仓库,名称:maven-public,然后版本策略是使用的混合型,然后将maven-releases、maven-snapshots、maven-central、spring-plugin组和起来。然后我们在使用的时候就可以仅配置这个库即可。
在这里插入图片描述

本例中使用nexus默认的仓库即可。

5.setting配置

以下是从maven中取出的setting文件,对部分注释进行了翻译,然后就是我对私有仓库的配置:

<?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.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository

  -->
  <localRepository>D:\JAVA\Maven\repo</localRepository>
  <!-- interactiveMode
   | 这将决定maven在需要输入时是否会提示您。  If set to false,
   | Maven将为所讨论的参数使用一个合理的默认值,可能是基于其他一些设置。  
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   |确定maven在执行构建时是否应该尝试连接到网络。  
   | 这将对工件下载、工件部署以及其他方面产生影响。  
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   |这是一个附加组标识符的列表,当根据前缀解析插件时,将搜索这些组标识符,  例如,当调用像“mvn prefix:goal”这样的命令行。 
   Maven会自动添加组标识符“org.apache.maven”。 插件”和“org.codehaus。 Mojo,如果列表中没有这些内容。  
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | 这是可以在这台计算机上用于连接到网络的代理的列表。 除非另有规定(通过系统属性或命令行开关),否则将使用此列表中第一个标记为活动的代理规范。  
   |-->
  <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
   | 这是身份验证配置文件的列表,由系统中使用的服务器id进行键控。  
   |只要maven必须连接到远程服务器,就可以使用身份验证配置文件。
   |-->
  <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>my-repo-snapshots</id>
      <username>lwl</username>
      <password>lwl123</password>
    </server>
    -->
    
    <!--另一个示例,使用密钥进行身份验证。
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | 这是从远程存储库下载工件时使用的镜像列表。
   |
   | 它的工作方式是这样的:POM可以声明一个存储库来解析特定的工件。
   | 然而,这个存储库有时可能会遇到流量大的问题,因此人们将它映射到多个地方。  
   |
   | 该存储库定义将有一个惟一的id,因此我们可以为该存储库创建一个镜像引用,以用作替代下载站点。 镜像站点将是该存储库的首选服务器。  
   |-->
  <mirrors>
    <!-- mirror
     | 指定要使用的存储库镜像站点,而不是给定的存储库。 此镜像服务的存储库具有与此镜像的mirrorOf元素相匹配的ID。 
     id用于继承和直接查找,并且在镜像集中必须是唯一的。  
     |
    <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>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
	

	 -->
	 <mirror>
      <id>maven-public</id>
      <mirrorOf>*</mirrorOf>
      <name>我的私有库</name>
      <url>http://192.168.3.101:8081/nexus/repository/maven-public/</url>
      <blocked>false</blocked>
    </mirror>
  </mirrors>
  <!-- profiles
   | 这是一个配置文件列表,可以以多种方式激活,并且可以修改构建过程。 
   xml中提供的概要文件旨在提供允许构建在本地环境中工作的本地机器特定路径和存储库位置。  
   |
   | 例如,如果您有一个集成测试插件(如cactus),它需要知道Tomcat实例安装在哪里,那么可以在这里提供一个变量,
   以便在构建过程中解除对该变量的引用,以配置cactus插件。 
   |
   | 如上所述,可以通过多种方式激活概要文件。
    一种方法—本文档的activeProfiles部分(settings.xml)—将在稍后讨论。
    另一种方法基本上依赖于对系统属性的检测,或者匹配属性的特定值,或者仅仅测试它的存在。 
    配置文件也可以通过JDK版本前缀激活,其中值'1.4'可能会在JDK版本'1.4.2_07'上执行构建时激活配置文件。 
    最后,可以直接从命令行指定活动概要文件的列表。  
   
   |
   |注意:对于在settings.xml中定义的概要文件,您被限制为只指定工件存储库、插件存储库,以及作为POM中插件的配置变量使用的自由形式的属性。
   |
   |-->
  <profiles>
    <!-- profile
     | 指定构建过程的一组介绍,将使用上面描述的一个或多个机制激活。 
     为了继承,并且要通过<activatedProfiles/>或命令行激活概要文件,概要文件必须有一个惟一的ID。  
     |
     | 一个被鼓励的概要文件识别的最佳实践是对概要文件使用一致的命名约定,例如'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett'等。 
     这将使您更直观地理解所引入的概要文件集试图完成什么,特别是当您只有一个用于调试的概要文件id列表时。  
     |
     | 这个配置文件示例使用JDK版本来触发激活,并提供了一个特定于JDK的回购。  
    <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>
	
	<profile>
	  <id>dev</id>
      <repositories>
        <repository>
          <id>maven-public</id>
          <name>Repository for dev</name>
          <url>http://192.168.3.101:8081/nexus/repository/maven-public/</url>
          <layout>default</layout>
		  <releases>
			<enabled>true</enabled>
		  </releases>
		  <snapshots>
			<enabled>true</enabled>
			<updatePolicy>always</updatePolicy>
	      </snapshots>
        </repository>
      </repositories>
    </profile>
    -->
	
    <!--
     | 下面是另一个配置文件,由系统属性'target-env'激活,
     其值为'dev',它提供了到Tomcat实例的特定路径。 要使用它,你的插件配置可能像这样:  
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | 注意:如果你只想在某人将'target-env'设置为任何值时注入这个配置,你可以在激活属性中去掉<value/>。  
     |
    <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
   | 对于所有构建都是活动的配置文件列表。
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
<activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
  -->
 
</settings>


注意mirrorOf的使用,mirrorOf是说这个镜像的使用条件,如果说所有情况都使用这个镜像,那么就用*即可。

6.打包至中央仓库

在项目的pom.xm文件中进行配置好要打包的仓库即可。

    <distributionManagement>
        <repository>
            <id>maven-releases</id>
            <name>快照库</name>
            <url>http://192.168.3.101:8081/nexus/repository/maven-releases/</url>
        </repository>
    </distributionManagement>

随后使用mvn compile命令即可将包发布到中央仓库。

7.QA

搭建过程中还是有不少问题,不过难得从头复现了,不过问题都不难,主要是看清楚报错的异常日志,然后根据日志去排查问题即可。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值