搭建项目开发环境(四)—— 安装和配置私有库(Nexus)

1,609 阅读3分钟

下载Nexus

去Nexus官网 www.sonatype.com/products/re… 下载免费版nexus repository oss。

image.png

要求填写个人信息(business email不能用个人公共邮箱,但可以随便填写)。

image.png

nexus现在已经不提供war包,只能用内置jetty服务器版本。

解压缩nexus-3.32.0-03-unix.tar.gz到/usr目录下

tar -zxf nexus-3.32.0-03-unix.tar.gz

配置Nexus

在/usr/nexus-3.32.0-03/etc/nexus-default.properties中修改jetty端口和work目录

## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/

# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
 nexus-pro-feature

nexus.hazelcast.discovery.isEnabled=true

进入/usr/nexus-3.32.0-03/bin,执行命令

启动nexus ./nexus start 停止nexus ./nexus stop

使用Nexus

在浏览器中输入http://IP:8081, 页面如下

image.png

初次登录的密码在/usr/sonatype-work/nexus3/admin.password

(1)默认仓库说明:

  • maven-central:maven 中央库,默认从 repo1.maven.org/maven2/ 拉取 jar
  • maven-releases:私库发行版 jar,初次安装请将 Deployment policy 设置为 Allow redeploy
  • maven-snapshots:私库快照(调试版本)jar
  • maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 或项目 pom.xml 中使用

(2)仓库类型说明:

  • hosted: 本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
  1. Releases: 这里存放我们自己项目中发布的构建, 通常是Release版本的, 比如我们自己做了一个FTP Server的项目, 生成的构件为ftpserver.war, 我们就可以把这个构建发布到Nexus的Releases本地仓库.

  2. Snapshots: 这个仓库非常的有用, 它的目的是让我们可以发布那些非release版本, 非稳定版本, 比如我们在trunk下开发一个项目,在正式release之前你可能需要临时发布一个版本给你的同伴使用, 因为你的同伴正在依赖你的模块开发, 那么这个时候我们就可以发布Snapshot版本到这个仓库, 你的同伴就可以通过简单的命令来获取和使用这个临时版本.

  • proxy: 代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
  • group: 这是一个仓库聚合的概念,用户仓库地址选择 Group 的地址,即可访问 Group 中配置的,用于方便开发人员自己设定的仓库。maven-public 就是一个 Group 类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x 默认为 Releases、Snapshots、Central,当然你也可以自己设置。

新建一个第三方私有库

在实际开发中,由于版权问题,并不是都能从maven中央库获取到所需jar包,比如IBM和oracle的一些jar包(class12.zip), 我们除了可以用新增其他Proxy远程库来获取,也可以存放在公司私有库中。

先建立一个新用户,用以客户端链接时的身份认证

image.png

新建Repository,命名为3rd_part

image.png

然后将3rd_part库键入Maven_public群中

image.png

在IDE中配置使用私有库

默认Maven已经都安装完毕,IDE也正常配置。 在File-setting菜单下,查看Maven的user setting file和Local Respository.

image.png 将Maven安装目录下...\apache-maven-3.6.3\conf中的settings.xml文件拷贝到user setting file所指定的目录下,准备修改。

...\apache-maven-3.6.3\conf中的settings.xml是本机所有用户的默认maven配置,而后者是当前用户的Maven默认配置,后者优先级高于前者

setting.xml文件修改如下:

<?xml version="1.0" encoding="UTF-8"?>

<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">
  <pluginGroups>
  </pluginGroups>

  <proxies>
  </proxies>

  <servers>
    <server>
      <!--这里要和后面myProfile中对应上-->
      <id>myRepository</id>
      <!--这里是之前新建的nexus用户-->
      <username>jay-nexus</username>
      <password>xxxxxx</password>
    </server>
  </servers>

  <mirrors>
  </mirrors>

  <profiles>
    <profile>
        <id>myProfile</id>
        <repositories>
            <repository>
               <id>myRepository</id>
               <!--这个url从nenux中的maven-public中复制而来-->
               <url>http://ip:8081/repository/maven-public/</url>
               <releases>
                    <enabled>true</enabled>
               </releases>
               <snapshots>
                    <enabled>false</enabled>
               </snapshots>
            </repository>
        </repositories>
    </profile>  
  </profiles>

 <activeProfiles>
        <activeProfile>myProfile</activeProfile>
  </activeProfiles>
</settings>

勾选上override,点击Ok,可以看到Respository中看到私有库已经可以使用

image.png