通过maven 上传jar 到nexus3,cong nexus3下载jar

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=${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标签下配置的。

<servers>
<server>
<id>release_user</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshot_user</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
这里配置两个用户,一个部署release类型jar包的,一个是部署snapshot类型jar包的。

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

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

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

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

<name>up_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>

<distributionManagement>
<repository>
<id>release_user</id>
<name>Release Deploy</name>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>snapshot_user</id>
<name>Snapshot Deploy</name>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
这里配置了上传的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"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<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>
</project>
由于之前已经将两个仓库加入到public组中,所以我们直接使用public仓库组的url下载。

执行:

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

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

注:使用package可能不会更新jar包,可以使用idea的reimport功能或者删除本地仓库jar包,再package。
---------------------
作者:绝世好阿狸
来源:CSDN
原文:https://blog.csdn.net/u010900754/article/details/80213291
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/maohuidong/p/9878091.html