为啥要搭建Maven私服呢,因为有时候公司不便将一些内部封装好的jar或者aar的架包放到外网上,或者当项目大一点的时候需要将一些业务封装成一个模块,来使用就需要本地私服了,注意上传上去的.aar文件不是整个项目哦。
一 Nexus的搭建

1:首先得安装Java的jdk;

2:下载Nexus:https://support.sonatype.com/hc/en-us/articles/218637467-Download-Nexus-Repository-Manager-3并安装;

我的下载的是Windows的:

nexus maven权限设置 nexus搭建maven_java

也就是最新的那一个,下载解压后放到一个盘下,我的在D盘下,可以将目录路径添加到Path中,我的路径是D:\nexus-3.2.1-01-win64\nexus-3.2.1-01\bin也可以不放。

使用Windows的黑窗口的,进到刚才下载下解压的文件夹下,里面有两个文件如图:

nexus maven权限设置 nexus搭建maven_java_02


进入第一个文件下的bin目录下

执行 nexus.exe /run 就可以啦,如果是第一次运行时间比较长一些,之后每次用的时候都要去运行起来。

注意一哈直接运行nexus.exe是不行的,使用管理员权限也不行,只能在黑窗口里运行。

如果不出错运行起来后,在浏览器中:http://localhost:8081/

Nexus的默认端口号是8081,如果出了冲突需要解决一哈,这个就百度一哈吧。成功后的界面如下:

nexus maven权限设置 nexus搭建maven_本地私服_03


现在什么也木有,需要登录后端去创建库。搭建Nexus的工作已经完成了。

二:在Nexus中创建库

右上角Sgin in登录,登陆默认账户,账户名:admin 账户密码:admin123;登陆后点击 小齿轮就是那个设置项,如图:

nexus maven权限设置 nexus搭建maven_nexus_04

点击Repositories

nexus maven权限设置 nexus搭建maven_java_05

点击创建那个左上角的+号create repository,之后点击maven2(hosted);

nexus maven权限设置 nexus搭建maven_nexus maven权限设置_06

在下面的界面里面填入库名和其他的一些设置如图所示:

nexus maven权限设置 nexus搭建maven_本地私服_07

之后点击Create repository就可以了,这是我创建库:

nexus maven权限设置 nexus搭建maven_nexus_08

三:将Android Studio中的lib上传库中

在全局的build.gradle的allprojects 》repositories 下添加 mavenLocal()的支持。
如图:

nexus maven权限设置 nexus搭建maven_maven_09

之后在本地上lib的build.gradle 中添加对maven
在最开头apply plugin: ‘com.android.library’下面添加apply plugin: ‘maven’
同时添加uploadArchives对上传的lib 的一些设置。

uploadArchives{
    repositories.mavenDeployer{
 repository(url:"http://localhost:8081/repository/com.shaodf.shaodflib/"){
            authentication(userName:"admin",password:"admin123")
      }
        pom.version="0.0.1"
        pom.artifactId="shaodflib"
        pom.groupId="shaodf"
    }
}

url对应的是你的库的路径,获取路径的方法如下:

nexus maven权限设置 nexus搭建maven_maven_10

点击copy就可以获得路径。
authentication设置上传到的账户名密码;
pom.version=”0.0.1”是版本号;
pom.artifactId=”shaodflib”;是这个lib在库中的名字
pom.groupId=”shaodf”;这个是组名,指明这个lib在库中是那个组里的
这里的 pom.groupId和pom.artifactId和注意一哈,一会儿会用到。

完整的图:

nexus maven权限设置 nexus搭建maven_本地私服_11

之后就是上传了,点击最右下角的gradle之后找到自己的那个lib点开Tasks>upload双击uploadArchives等待上传完场。操作图示:

nexus maven权限设置 nexus搭建maven_java_12

有时候会报上传失败,但是如不是该项目的错一般不会影响上传的就如我的:

nexus maven权限设置 nexus搭建maven_nexus maven权限设置_13


虽然这儿报错了,但是我到后端看了一哈数据是传上去了。估计是在gradle运行到这儿之前已把我的那个lib上传上去了。

看看后端是否有数据,这是我的图

nexus maven权限设置 nexus搭建maven_maven_14

双击自己的库看一哈有数据没,我的上传上去了。

nexus maven权限设置 nexus搭建maven_nexus_15

这就把lib上传上来了。

四:下载lib库到自己的项目中引用。

首先在引用的项目的build.gradle中添加本地maven库的地址,就是之前的那个地址:

allprojects{
    repositories{
        maven{
            url "http://localhost:8081/repository/com.shaodf.shaodflib/"
        }
    }
}

注意依赖关系中添加对该项目的依赖:

compile 'shaodf:shaodflib:0.0.1'

里面的内容格式是: pom.groupId:pom.artifactId:pom.version,错了会获取不到。
或者在后端查看如图:

nexus maven权限设置 nexus搭建maven_nexus maven权限设置_16

完整的图

nexus maven权限设置 nexus搭建maven_nexus maven权限设置_17

好了,sync now一哈,如果中间没出什么错就可以下载下来了。如果出错了那就仔细回头看看,一般都是有地方写错了啥的。
我在lib中写了一个方法,试了一下是可以没问题的。上图:

nexus maven权限设置 nexus搭建maven_本地私服_18

nexus maven权限设置 nexus搭建maven_本地私服_19

到此结束啦!