搭建私服以及私服的使用

【maven】nexus3 使用

nexus是一种常见的maven私服软件。

网上介绍的都是nexus2的使用,下面是最新版nexus3的使用方式。

首先需要从官网下载nexus3的包,很卡。

下载好以后解压会有两个文件夹:nexus的和sonatype-work。前者是功能的实现,后者负责存储数据。

进入nexus的bin目录下:

启动

./nexus start
关闭

./nexus stop
启动之后,可以访问:
http://localhost:8081/

点击右上角sign in,用户名admin密码admin123即可登录。

在etc下有一个nexus-defauly.properties文件,是nexus的配置文件:

DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties

Jetty section

application-port=8081
application-host=0.0.0.0
nexus-args= j e t t y . e t c / j e t t y . x m l , {jetty.etc}/jetty.xml, 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
我们可以在这里修改端口号或者项目的context。

登录之后就可以查看私服了。登录以后,最上面会有两个按钮,一个是查看仓库配置的(齿轮形状),另一个是查看仓库内容的(正方体):

内容:

我们只关心maven,所以在左侧点击maven,右面就是所有的仓库的maven的jar包。上面支持特定的搜索。第一次这个页面没有任何包,上面我已经加好的。

当然我们也可以点击左侧的browse来浏览仓库:

这是默认的仓库,当然这里我们只关心maven的,也就是前四个。name是仓库名,url是仓库的地址,会在我们的项目pom里被使用到。

1.maven-central:这是maven的中心仓库,nexus这里只是做一个代理,最后会转发到maven的central库,如下:

2.maven-public:这是一个仓库组,访问这个仓库组的地址其实会访问组内所有仓库

我们可以在配置页面看到这个public的仓库组的配置,默认是包含了members指定的三个仓库。所以在pom中使用这个仓库组的url时,实际上会从members里的所有仓库下载jar包。

3.maven-releases:这是nexus提供的一个默认的存放release版本jar包的仓库。

4.maven-snapshots:这是nexus提供的一个默认的存放snapshot版本jar包的仓库。

仓库的属性可以控制只存放snapshot或者release版本的jar包。

当然我们可以不使用这些默认的仓库,自行创建。

这里可以看到nexus可以支持很多种仓库,只看maven,其实就只有三种:

proxy就是代理类,负责转发,比如之前的maven-central;

hosted就是我们常用的存放jar包的仓库,可以选择jar包的类型,release,snapshot或者mixed;

group可以包含多个仓库,比如之前的maven-public;

以上就是nexus3的大致使用方式,具体的可以自行了解,比较简单。

下面介绍maven项目从nexus下包和上传。

1.上传jar包:

上传jar包需要认证,maven的认证是在.m2/settings.xml里servers标签下配置的。

release_user admin admin123 snapshot_user admin admin123 这里配置两个用户,一个部署release类型jar包的,一个是部署snapshot类型jar包的。

id用于唯一指定一条认证配信息,之后会在pom中使用。

接着新建一个quick-start的maven项目,在pom中配置distributionManagement标签,该标签负责描述maven deploy上传远程仓库:

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


4.0.0

com.liyao
up_nexus_test
1.0-SNAPSHOT

up_nexus_test

http://www.example.com

release_user Release Deploy http://localhost:8081/repository/maven-releases/ snapshot_user Snapshot Deploy http://localhost:8081/repository/maven-snapshots/ 这里配置了上传的url,具体的url可以在nexus的仓库浏览界面下点击仓库的url copy获得。使用刚才的两个认证信息,把jar包存在nexus提供的默认仓库下。id对应了setting.xml里配置的信息,name随意。

然后我们在项目根目录下执行:

mvn clean deploy
部署项目到nexus上。
之前上传的是快照版本,所以可以在maven-snapshots仓库下看到:

再打一个release的jar包,只需要把version改为1.0即可,再执行deploy命令。

所有的jar包:

2.拉取jar包:

我们需要再新建一个项目来拉取上面上传的jar包,还是新建一个maven的quick-start项目,然后需要在pom中加入依赖,并且配置我们的nexus仓库。

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


4.0.0

<groupId>com.liyao</groupId>
<artifactId>down_nexus_test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>down_nexus_test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>com.liyao</groupId>
        <artifactId>up_nexus_test</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>nexus-public</id>
        <name>Nexus Public</name>
        <url>http://localhost:8081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
由于之前已经将两个仓库加入到public组中,所以我们直接使用public仓库组的url下载。

执行:

mvn clean package
接着就可以看到下载的jar包了:

我们也可以拉刚才的快照包,把依赖里的version改为1.0-SNAPSHOT即可,重新package:

本地maven库配置settings.xml

org.sonatype.plugins nexus admin admin123 nexus * http://localhost:8081/repository/maven-public/ repo2 central Human Readable Name for this Mirror. http://repo2.maven.org/maven2/ nexus central http://central true true central http://central true true nexus

注:使用package可能不会更新jar包,可以使用idea的reimport功能或者删除本地仓库jar包,再package。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值