使用 Gradle 和 Sonatype Nexus 搭建自己的 Maven 倉庫


2015-6-17 12:32| 投稿: redboy| 查看: 1459| 評論: 0

摘要: 如果你的公司有多個Android app應用需要開發,那麼很有可能你需要私有的公共庫。本文介紹如何使用sonar nexus搭建maven倉庫。 1. 安裝Nexus 從 http://www.sonatype.org/nexus/go/上下載tar.gz或者zip格式壓縮包。 ...
  • 公開課1108:域名與DNS系統
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

如果你的公司有多個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 可以看到這樣的頁面 


,表示已經安裝成功了。

2. 建立倉庫

使用nexus默認賬戶名admin,密碼admin123。登錄進去看到已經建立了十幾個倉庫。

點擊工具欄add -> 選擇hosted repository,然後填入repository id,和repository name-> 保存,這樣就可以建立新的倉庫了。

如圖,建立了一個名爲juude的倉庫。

3. 上傳library

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

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中加入:


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'
}

這樣就可以正常使用了。

發佈了44 篇原創文章 · 獲贊 31 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章