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

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


 

如果你的公司有多个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文件中,加入以下配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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中加入:

?
1
2
3
4
5
6
7
8
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'
}

这样就可以正常使用了。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值