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

jopen 9年前發布 | 129K 次閱讀 Gradle 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文件中,加入以下配置:

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

這樣就可以正常使用了。

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!