今天臨時開會討論要把不經(jīng)常用的呐萨,獨立的第三方庫上傳到本地nexus上缀棍。
首先說明下搭建本地maven庫的優(yōu)勢:
- 1 第三方庫代碼不是任何人都可以進行編輯的
- 2 很多庫在服務器的maven上會讓本地的project結構更簡單
經(jīng)過一上午的努力 通過gradle達成目標,這里做一下分享:
第一步在本地gradle.properites設置公共字段:
SNAPSHOT_REPOSITORY_URL=http://xxx.xxx.xxxx.xxx:8080/nexus/..../repositories/snapshots/
#前面配置的releases倉庫地址
RELEASE_REPOSITORY_URL=http://xxx.xxx.xxxx.xxx:8080/nexus/c...../repositories/releases/
其中xxx代表你本地的服務器url
nexus要有兩種地址一個是 snapshots一個是releases
NEXUS_USERNAME=admin
NEXUS_PASSWORD=pwd
其中 admin和pwd代表你nexus服務器的賬號和密碼
第二步 找到在本地需要上傳到nexus的lib庫內(nèi)部的* build.gradle *文件。
在apply plugin: 'com.android.library'下添加如下內(nèi)容:
apply plugin: 'maven'
def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}
def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
afterEvaluate { project ->
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = "你的lib庫的包名"
pom.artifactId = "你lib庫的項目名"
pom.version = "版本號"
repository(url: RELEASE_REPOSITORY_URL) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
snapshotRepository(url: SNAPSHOT_REPOSITORY_URL) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
}
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
artifacts {
archives androidSourcesJar
// archives androidJavadocsJar
}
}
由于你在gradle.properites設置過RELEASE_REPOSITORY_URL、SNAPSHOT_REPOSITORY_URL、NEXUS_USERNAME和NEXUS_PASSWORD的值观堂,所以這里可以直接引用。
第三步 選擇gradle你的項目呀忧,你可以在選在Build-->Make Project或者直接點擊Android Studio上面AVDManager右側(cè)的圓形按鈕师痕。均可以gradle你的項目。
第四步 gradle你的項目后會在gradle窗口下多出一個upload方法如圖
第五步 雙擊這個uploadArchives而账,如果輸出successfull胰坟,就表示成功,就會上傳到你nexus服務器泞辐。
我這里遇到的坑笔横,在這里說下:
1 url一定要對,我之前url出了一些問題咐吼。會報如下錯誤:
Failed to transfer file http:///xxxx. Return code is: 400"
出現(xiàn)上面的問題一般是如下問題導致的
1 url不對
2 證書不對
3 沒有權限去上傳到nexus庫
4 該用戶上傳這個庫是沒有權限的
5 這個release版本號已經(jīng)上傳過了
在 stackoverflow有相關答案
至此 你的庫已經(jīng)上傳到nexus上了
那么如果進行依賴那吹缔?
首先在你的整體項目的build.gradle的里面添加如下代碼即可:
是你的整體項目的build.gradle里面不是你的具體項目里面
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
flatDir {
dirs 'libs'
}
maven{
url 'http://xxx.xxx.xxx.xx:8080/nexus/....public/'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
http://xxx.xxx.xxx.xx:8080/nexus/....public/ 表示你本地nexus的服務器地址
然后在你具體項目里面去依賴就可以了
然后gradle一下就好了。