使用 Gradle 和 Sonatype Nexus 搭建自己的 Maven 仓库

时间:2022-12-11 09:36:23

http://www.open-open.com/lib/view/open1434522817864.html


原文  http://juude.net/nexus_maven/  

如果你的公司有多个Android app应用需要开发,那么很有可能你需要私有的公共库。本文介绍如何使用sonar nexus搭建maven仓库。

1. 安装Nexus

从 http://www.sonatype.org/nexus/go/上下载tar.gz或者zip格式压缩包。并且解压到本地,然后进入bin目录,执行nexus

cd nexus-2.11.1-01/bin
./nexus start

在浏览器中输入:127.0.0.1:8081/nexus可以看到这样的页面

使用 Gradle 和 Sonatype Nexus 搭建自己的 Maven 仓库

,表示已经安装成功了。

回到顶部

2. 建立仓库

使用nexus默认账户名admin,密码admin123。登录进去看到已经建立了十几个仓库。

点击工具栏add -> 选择hosted repository,然后填入repository id,和repository name-> 保存,这样就可以建立新的仓库了。

如图,建立了一个名为juude的仓库。

使用 Gradle 和 Sonatype Nexus 搭建自己的 Maven 仓库
回到顶部

3. 上传library

在已经建好的android library项目的build.gradle文件中,加入以下配置:

?
12345678910111213141516171819202122232425262728 apply
plugin: 
'maven'
apply
plugin: 
'signing'
....signing
{
 required { has("release") && gradle.taskGraph.hasTask("uploadArchives") } sign configurations.archives} uploadArchives
{
 configuration = configurations.archives repositories.mavenDeployer { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } repository(url: 'http://127.0.0.1:8081/nexus/content/repositories/juude-id/') {//仓库地址 authentication(userName: "admin",//用户名 password: "admin123")//密码 } pom.project
{
 name 'juude-library' packaging 'aar' description 'none' url 'http://127.0.0.1:8081/nexus/content/repositories/juude-id/'//仓库地址 groupId "net.juude.droidviews" artifactId rootProject.name //LibA version android.defaultConfig.versionName } }}

现在在项目根目录执行./gradlew tasks,就可以看到多了一个选项:

Upload tasks
------------
uploadArchives - Uploads all artifacts belonging to configuration ':library:archives'

然后执行

./gradlew uploadArchives

如果没有报错,就成功上传自己的library了。

回到顶部

4. 使用library

由于没有使用maven center,使用的时候需要提供自己的url地址,在build.gradle中加入:

?
12345678 allprojects
{
 repositories { mavenCentral() //这里加入自己的maven地址 maven { url "http://127.0.0.1:8081/nexus/content/repositories/juude-id/" } }

然后在dependency里加入compile语句。

dependencies {
...
compile 'net.juude.droidviews:droidViews:1.0'
}

这样就可以正常使用了。