Maven私服的搭建(Nexus)

Maven私服的搭建(Nexus)

Nexus下载地址

官网地址:https://www.sonatype.com/

免费版地址:https://www.sonatype.com/download-nexus-repo-oss

Nexus安装教程

Windows安装Nexus

image-20201102231855091

安装流程:

下载之后解压即可,进入安装包bin目录下,cmd执行启动程序:
nexus.exe /run

浏览器访问:http://localhost:8081/
账号:admin
密码:admin123
密码在sonatype-work\nexus3下的admin.password文件中,登录之后可以修改,修改之后该文件会被删除。

Linux安装Nexus

上传安装包到服务器之后,解压:
tar -zxf nexus-3.28.1-01-unix.tar.gz

解压后会得到两个文件夹:nexus-3.28.1-01(nexus服务目录)、sonatype-work(私有库目录)。
注意:这里2个文件夹应该保持在同一个文件夹下,并且尽量不要放到根目录下。

进入nexus-3.28.1-01文件夹,其中etc/nexus-default.properties文件配置端口(默认为 8081)和work目录信息,我们可以按需修改。

执行命令启动:
cd nexus-3.28.1-01/bin
./nexus start

Docker安装Nexus

首先执行命令下载Nexus3镜像:
docker pull sonatype/nexus3

接着执行命令,创建宿主机挂载目录:
mkdir –vp /usr/local/nexus-data

最后执行命令运行Nexus3容器即可:
docker run -d --name nexus3 -p 8081:8081 -v /usr/local/nexus-data:/var/nexus-data sonatype/nexus3

查看密码:
-- 进入容器
docker exec -it nexus3 /bin/bash
-- 查看密码
cat /nexus-data/admin.password

Nexus仓库类型

Nexus用到的Repositories说明:
maven-central:maven中央仓库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases:私库发行版jar
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地Maven配置settings.xml或项目pom.xml中使用。

Nexus仓库类型:
Virtual:虚拟仓库。
Proxy:代理仓库,它们被用来代理远程的公共仓库,如Maven中央仓库。
hosted:本地仓库,通常我们会部署自己的构件到这一类型的仓库,比如公司的第二方库。
Group:仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

Nexus创建仓库

1)增加一个代理仓库,使用的是阿里云公共仓库。首先点击“Create repository”按钮开始创建一个仓库:

image-20201103100123521

2)类型选择maven2(proxy)

3)配置阿里云地址http://maven.aliyun.com/nexus/content/groups/public/,name可以随意填,然后点击最下方按钮创建:

image-20201103100429277

4)阿里云代理仓库创建完毕后,我们编辑maven-public,将其添加到放入group中,并调整优先级,然后保存:

image-20201103100631822

5)点击maven-public条目的copy按钮即可拷贝私服地址

Nexus发布依赖

第一步:修改settings.xml文件,配置连接私服的用户和密码。此用户名和密码用于私服校验,因为私服需要知道上传者的账号和密码是否和私服中的账号和密码一致。
<server>
    <id>releases</id>
    <username>admin</username>
    <password>admin123</password>
</server>

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

第二步:配置项目pom.xml。配置私服仓库的地址,自己的jar包可以上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为release则上传到私服的release仓库,如果版本为snapshot则上传到私服的snapshot仓库。注意:pom.xml这里的id和settings.xml配置的id对应。
<distributionManagement>
    <repository>
        <id>releases</id>
        <name>Releases</name>
        <url>http://localhost:8081/repository/maven-releases/</url>
    </repository>
    
    <snapshotRepository>
        <id>snapshots</id>
        <name>Snapshot</name>
        <url>http://localhost:8081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>

第三步:执行deploy命令发布到私服
若项目版本号末尾带有-SNAPSHOT,则会发布到snapshots快照版本仓库。
若项目版本号末尾带有-RELEASES或什么都不带,则会发布到releases正式版本仓库。

Nexus下载依赖

Maven配置私服下载有两种方式:
setting.xml:该文件配置的是全局模式。
pom.xml:该文件的配置的是项目独享模式。
注意:若pom.xml和setting.xml同时配置了,以pom.xml为准。
当我们在Maven使用maven-public仓库地址的时候,会按照如下顺序访问:本地仓库、私服maven-releases、私服maven-snapshots、远程阿里云maven仓库、远程中央仓库。

1)通过setting.xml文件配置。配置后不需要再配置pom.xml文件,即可通过私服下载jar依赖包。
<mirrors>
    <mirror>
        <!-- 该镜像的唯一标识符。id用来区分不同的mirror元素。-->
        <id>maven-public</id>
        <!-- 镜像名称 -->
        <name>maven-public</name>
        <!-- *指的是访问任何仓库都使用我们的私服 -->
        <mirrorOf>*</mirrorOf>
        <!-- 该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。-->
        <url>http://localhost:8081/repository/maven-public/</url>     
    </mirror>
</mirrors>

如果我们并没有搭建私服,属于个人开发,那么也可以直接配置使用阿里云Maven仓库:
<mirror>
  <id>nexus-aliyun</id>
  <name>Nexus aliyun</name>
  <mirrorOf>*</mirrorOf>
  <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

2)通过pom.xml文件配置。如果我们配置了pom.xml,则以pom.xml为准。
<repositories>
    <repository>
        <id>maven-nexus</id>
        <name>maven-nexus</name>
        <url>http://localhost:8081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

如果没有私服,我们同样也可以配置阿里云Maven仓库:
<repositories>
   <repository>
      <id>maven-aliyun</id>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
         <enabled>true</enabled>
      </releases>
      <snapshots>
         <enabled>true</enabled>
         <updatePolicy>always</updatePolicy>
         <checksumPolicy>fail</checksumPolicy>
      </snapshots>
   </repository>
</repositories>

Nexus下载插件

2)只修改pom.xml
1)只修改settings.xml
<profile>
    <!--profile的id-->
    <id>dev</id>
    
    <repositories>
        <repository>
            <!--仓库id,repositories可以配置多个仓库,保证id不重复-->
            <id>nexus</id>
            <!--仓库地址,即nexus仓库组的地址-->
            <url>http://localhost:8081/nexus/repository/maven-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/repository/maven-public/</url>
        </pluginRepository>
    </pluginRepositories>
</profile>

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

2)只修改pom.xml
<pluginRepositories>
    <pluginRepository>
        <id>maven-nexus</id>
        <name>maven-nexus</name>
        <url>http://localhost:8081/nexus/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
posted @ 2018-09-04 09:27  肖德子裕  阅读(239)  评论(0编辑  收藏  举报