Maven私服Nexus

目录

一、Nexus下载与安装(windows)

1. 解压下载好的压缩包至本地磁盘,可获得 nexus-3.54.1-01 和 sonatype-work 两个目录

2. 进入nexus-3.54.1-01/bin目录,使用管理员运行cmd(不然会报错)

3. 访问地址:http://localhost:8081/

二、Nexus 使用

1. 仓库属性和分类

2. 仓库配置

(1)maven-central

(2)maven-releases

(3)maven-snapshots

(4)maven-public

(5)snapshots版本 VS releases版本 区别

3. 新建角色和用户

(1)新增角色

(1)新增用户

4. 配置本地maven,使项目从私有仓库获取依赖

(1) 配置私有仓库用户名密码

(2) 配置私有仓库镜像地址

(3) 配置私有仓库依赖下载地址

5. 配置项目,上传依赖至私有仓库

(1)在项目pom.xml文件添加以下配置:

(2)使用idea maven 插件上传依赖

(3)其他项目获取刚刚上传的com.boot包下的依赖

6. 手动上传依赖(jar)

(1)准备一个简单的项目,版本号不要是SNAPSHOT版本的

(2)在 Nexus 界面的upoad上传,默认是只允许上传releases仓库

(3)其他项目获取刚刚上传的com.nexus包下的依赖


一、Nexus下载与安装(windows)

Nexus 3和Nexus 2是Sonatype公司开发的两个不同版本的仓库管理器,这两个版本是并行的关系,Nexus 3相对于Nexus 2进行了一些重要的改进和增强。它引入了新的存储引擎、更多的仓库类型支持、改进的权限管理和用户界面,以及灵活的插件系统和增强的Docker支持。这些更新使得Nexus 3成为一个更强大、现代化和可扩展的仓库管理解决方案。

Nexus 3.x官网下载:Download Nexus Repository OSS | Sonatype

Nexus 3.54.1网盘下载:https://pan.baidu.com/s/1fRfVZzQAKu4y7azFPiCOjg?pwd=iv3o 
提取码:iv3o


1. 解压下载好的压缩包至本地磁盘,可获得 nexus-3.54.1-01 和 sonatype-work 两个目录

2. 进入nexus-3.54.1-01/bin目录,使用管理员运行cmd(不然会报错)

cmd执行命令

  • 安装服务:nexus.exe /install
  • 启动服务:nexus.exe /start
  • 停止服务:nexus.exe /stop
  • 卸载服务:nexus.exe /uninstall
3. 访问地址:http://localhost:8081/

账号:admin

密码:存放在 sonatype-work\nexus3\admin.password 文件中

首次登录请修改密码

禁用匿名访问

二、Nexus 使用

Maven私服是:公司在自己的局域网内搭建的 maven远程仓库服务器,每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,其它项目组就可以从私服服务器下载所依赖的组件(jar)

1. 仓库属性和分类

  • Type:仓库的类型,这里一共有三种    
    • proxy:用来代理远程公共仓库,如 Maven 中央仓库、JBoss 远程仓库。
    • hosted:又称 Nexus 本地仓库,该仓库通常用来部署本地项目所产生的构件。 hosted 宿主仓库又分为:releases 和shapshots,分别表示依赖的版本的发行版、快照版。快照版依赖不能上传到发行版仓库,反之亦然。nexus做了限制。
    • group:仓库组,用来聚合代理仓库和宿主仓库,为这些仓库提供统一的服务地址,以便 Maven 可以更加方便地获得这些仓库中的构件。      
  • Format:仓库的格式有两种maven2、nuget。我们是搞java的,所以新建仓库一般都是选择的maven2格式。
  • Status:仓库的状态,只有在线状态才能被访问。
    • online-remote available:表示该被代理仓库可用。
    • online-ready to connect:并不是代理仓库用不了。而是你根本还没有一个依赖去访问这个被代理仓库,nexus也不知道这个被代理仓库能不能用,所以它只能展示为待连接状态,新建的代理仓库都是 online-ready to connect状态。
  • URL:仓库的路径。
2. 仓库配置

刚安装好的nexus一共给我们创建了7个仓库(不同的nexus版本可能有一点差距),我们只需要关注Format为maven2的仓库即可,也就是一共有4个:

  • maven-central:该仓库用来代理 Maven 中央仓库,其策略为 Release,只会下载和缓存中央仓库中的发布版本的构件。
  • maven-releases:策略为 Release 的宿主仓库,用来部署公司或组织内部的发布版本构件。
  • maven-snapshots:策略为 Snapshot 的宿主仓库,用来部署公司或组织内部的快照版本构件。
  • maven-public:仓库组,默认聚合了以上三个仓库。
(1)maven-central

我们可以替换成阿里云镜像地址,通过阿里云镜像下载依赖要比maven中央仓库快的多。

阿里云镜像地址:https://maven.aliyun.com/repository/public

(2)maven-releases

这里设置为Allow redeploy,代表允许我们对仓库中的依赖维护升级新版本,如果设置为disable redeploy意味着我们无法将升级版本上传上来。

(3)maven-snapshots

我们经常在开发中会定义SNAPSHOT版本,SNAPSHOT(快照)是一种特殊的版本,它表示当前开发进度的副本。与常规版本不同,快照版本的构件在发布时,Maven 会自动为它打上一个时间戳,有了这个时间戳后,当依赖该构件的项目进行构建时,Maven 就能从仓库中找到最新的 SNAPSHOT 版本文件。

(4)maven-public

仓库组就是将这几个仓库汇集到一块,当项目使用nexus时,只需要配置仓库组的地址即可。

(5)snapshots版本 VS releases版本 区别

Maven 仓库分为两种,Snapshot 快照仓库和 Release 发行仓库。Snapshot 快照仓库用于保存开发过程中的不稳定 SNAPSHOT 版本,Release 发行仓库则用来保存稳定的 RELEASE 版本。Maven 会根据模块的版本号(pom.xml 文件中的 version 元素)中是否带有 -SNAPSHOT 来判断是 SNAPSHOT 版本还是正式 RELEASE 版本。带有 -SNAPSHOT 是SNAPSHOT(快照)版本,不带 -SNAPSHOT 的就是正式 RELEASE(发布)版本。


3. 新建角色和用户

一般我们不建议maven当中直接去配置admin账户,因为admin账户有新增和删除仓库的权限,我们可以选择新增一个账户,只给这个账号赋予上传依赖和下载依赖的权限即可,由于nexus是用户角色制,所以新增账户前,首先我们需要新建一个角色。然后让角色只具备上传依赖和下载依赖的权限。然后再给用户赋予该角色权限。

(1)新增角色

Role ID:deployment,Role Name:deployment

nx-apikey-all

nx-repository-view-*-*-*

nx-repository-view-*-*-browse

nx-repository-view-*-*-read

nx-repository-view-maven2-*-*

nx-repository-view-maven2-*-browse

nx-repository-view-maven2-maven-public-browse

nx-repository-view-maven2-maven-public-read

nx-repository-view-maven2-maven-releases-*

nx-repository-view-maven2-maven-releases-browse

nx-repository-view-maven2-maven-releases-read

nx-repository-view-maven2-maven-snapshots-*

nx-repository-view-maven2-maven-snapshots-add

nx-repository-view-maven2-maven-snapshots-browse

nx-repository-view-maven2-maven-snapshots-read

(1)新增用户

4. 配置本地maven,使项目从私有仓库获取依赖

完整的 /conf/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.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 http://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:\application\maven\repository3.8.4</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标签可以随便填,只需要在servers中唯一即可,后面很多地方会使用该id -->
      <id>nexus-maven</id>
      <username>deployment</username>
      <password>deployment</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>nexus-maven</id>
        <name>nexus name</name>
        <mirrorOf>*</mirrorOf>
        <url>http://localhost:8081/repository/maven-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>
    -->


<profile>
        <id>nexus</id>
        <!--声明一个或多个远程仓库  -->
		<repositories>
			<!-- 声明一个 Nexus 私服上的仓库  -->
			<repository>
				<!--仓库id,这个id就是上面配置的账号密码id  -->
				<id>nexus-maven</id>
				<!-- 仓库的名称 -->
				<name>nexus</name>
				<!--仓库的地址  -->
				<url>http://localhost:8081/repository/maven-public/</url>
				<!-- 是否开启该仓库的 release 版本下载支持 -->
				<releases>
					<enabled>true</enabled>
				</releases>
				<!-- 是否开启该仓库的 snapshot 版本下载支持 -->
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
		<!-- 声明一个或多个远程插件仓库 -->
		<pluginRepositories>
			<!--声明一个 Nexus 私服上的插件仓库  -->
			<pluginRepository>
				<!--插件仓库 id -->
				<id>nexus-maven</id>
				<!--插件仓库 名称 -->
				<name>nexus</name>
				<!-- 配置的插件仓库的地址 -->
				<url>http://localhost:8081/repository/maven-public/</url>
				<!-- 是否开启该插件仓库的 release 版本下载支持 -->
				<releases>
					<enabled>true</enabled>
				</releases>
				<!-- 是否开启该插件仓库的 snapshot 版本下载支持 -->
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</pluginRepository>
		</pluginRepositories>
          <properties>
              <maven.compiler.source>1.8</maven.compiler.source>
              <maven.compiler.target>1.8</maven.compiler.target>
              <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
	<!-- 默认该profile生效 -->
          <activation>
              <activeByDefault>true</activeByDefault>
          </activation>
      </profile>
 

  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>
(1)<server> 配置私有仓库用户名密码
<servers> 
<!-- 这是配置访问私有仓库的用户名密码 -->
    <server>
        <!-- id标签可以随便填,只需要在servers中唯一即可,后面很多地方会使用该id -->
        <id>nexus-maven</id>
        <!-- 用户名,这里使用新建的用户deployment -->
        <username>deployment</username>
        <password>deployment</password>
    </server>
 </servers>
(2)<mirror> 配置私有仓库镜像地址
<mirrors>
    <mirror>
        <!-- 之前server的id,访问仓库组也要拿用户名密码 -->
        <id>nexus-maven</id>
        <name>nexus name</name>
        <!-- *代表所有的依赖都从私服找 -->
        <mirrorOf>*</mirrorOf>
        <!-- 就是仓库组的,还是去仓库组右边copy即可 --> 
        <url>http://localhost:8081/repository/maven-public/</url>
    </mirror> 
</mirrors>
(3)<profile> 配置私有仓库依赖下载地址
<profiles>
    <profile>
        <id>nexus</id>
        <!--声明一个或多个远程仓库  -->
		<repositories>
			<!-- 声明一个 Nexus 私服上的仓库  -->
			<repository>
				<!--仓库id,这个id就是上面配置的账号密码id  -->
				<id>nexus-maven</id>
				<!-- 仓库的名称 -->
				<name>nexus</name>
				<!--仓库的地址  -->
				<url>http://localhost:8081/repository/maven-public/</url>
				<!-- 是否开启该仓库的 release 版本下载支持 -->
				<releases>
					<enabled>true</enabled>
				</releases>
				<!-- 是否开启该仓库的 snapshot 版本下载支持 -->
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</repository>
		</repositories>
		<!-- 声明一个或多个远程插件仓库 -->
		<pluginRepositories>
			<!--声明一个 Nexus 私服上的插件仓库  -->
			<pluginRepository>
				<!--插件仓库 id -->
				<id>nexus-maven</id>
				<!--插件仓库 名称 -->
				<name>nexus</name>
				<!-- 配置的插件仓库的地址 -->
				<url>http://localhost:8081/repository/maven-public/</url>
				<!-- 是否开启该插件仓库的 release 版本下载支持 -->
				<releases>
					<enabled>true</enabled>
				</releases>
				<!-- 是否开启该插件仓库的 snapshot 版本下载支持 -->
				<snapshots>
					<enabled>true</enabled>
				</snapshots>
			</pluginRepository>
		 </pluginRepositories>
         <properties>
              <maven.compiler.source>1.8</maven.compiler.source>
              <maven.compiler.target>1.8</maven.compiler.target>
              <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
          </properties>
	      <!-- 默认该profile生效 -->
          <activation>
              <activeByDefault>true</activeByDefault>
          </activation>
      </profile>
</profiles>
5. 配置项目,上传依赖至私有仓库
(1)在项目pom.xml文件添加以下配置:
 <!--    配置私有仓库,上传资源-->
    <distributionManagement>
        <repository>
            <!--注意这个id一定要填写我们maven的setting.xml当中配置的id-->
            <id>nexus-maven</id>
            <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <!--注意这个id一定要填写我们maven的setting.xml当中配置的id-->
            <id>nexus-maven</id>
            <url>http://localhost:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
  • distributionManagement:负责将指定的构件部署到 Nexus 指定的仓库中。
  • repository:代表上传release版本存储的仓库地址
  • snapshotRepository:代表上传snapshot版本存储的仓库地址
  • 标签中的 id,就是上面我们配置的server id,maven会通过这个id去server中拿用户名密码去访问私服仓库。
(2)使用idea maven 插件上传依赖

先clean,再deploy,由于项目设置的是1.0-SNAPSHOT,也就是SNAPSHOT版本,所以他会自动上传到maven-snapshots仓库当中

deploy 成功后会发现maven-snapshots中已经有了我们上传的依赖了

(3)其他项目获取刚刚上传的com.boot包下的依赖

6. 手动上传依赖(jar)
(1)准备一个简单的项目,版本号不要是SNAPSHOT版本的

(2)在 Nexus 界面的upoad上传,默认是只允许上传releases仓库

上传成功

(3)其他项目获取刚刚上传的com.nexus包下的依赖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值