目前 Maven Repo 有兩個主要的開源,一個是 Nexus 的,一個是 Jfrog。我採用的是 JFrog,這邊附上 Nexus 的網頁。Jfrog 得很完整的說明及安裝流程。
第一步,架設:
1. 先有一台 Linux 電腦或是環境,我是用電腦
2. 安裝 Java8
3. 安裝 Artifactory + Tomcat Web Server
$ wget https://bintray.com/artifact/download/jfrog/artifactory-debs/pool/main/j/jfrog-artifactory-oss-deb/jfrog-artifactory-oss-4.5.1.deb
$ gpg --keyserver pgpkeys.mit.edu --recv-key 6B219DCCD7639232
$ gpg -a --export 6B219DCCD7639232 | sudo apt-key add -
$ apt-get update
$ dpkg -i jfrog-artifactory-oss-4.5.1.deb
安裝完重新開機,連到 http://127.0.0.1:8081/,如果可以連線,就算是安裝成功了,可以進行下一步:設定 Repository
------------- Trouble Shooting : 如果安裝不成功 --------------
a. 手動關閉 service
$ sudo service artifactory stop
b. 手動打開 service
$ sudo service artifactory start
c. 如果 b. 出了問題,那看看 service 啟動紀錄,看看哪邊有問題,發現是 Tomcat pid file 有問題,這樣還是很不清楚
$ sudo systemctl status artifactory.service or $ journalctl -xe
d. 手動執行
$sudo bash -x /etc/init.d/artifactory stop/start 看看那一步有問題
確認
netstat -vatn|grep LISTEN|grep 8015
這一步出問題了,進去 Netstat 看看狀況,把 8015 port 佔用的 process 關了
--------------------------------------------------------------------------
第二步,設定 Repository
1. 先用 admin/password 登入 http://127.0.0.1:8081
2. 增加群組:
2.1 進選單,Admin/Security/Groups
2.2 按 +,新增一個 Group,我是用 Contributors,說明是 Library Uploading Group
3. 設定權限,選單 Admin/Security/Permissions
3.1 按 +,增加一個 Permission,我取名義叫 "Contribute Libraries"
3.2 確定該 Permission 有增加全部的 repository,同時 "Any Local Repository" and "Any Remote Repository" 兩個 Checkbox 都要選到,然後按 "Next" 進到 Group
3.3 在這一頁,新增 Contributors group,同時確定除了 Management 之外的權限都打開
4. 增加 User,選單 Admin/Security/Users
4.1 增加一個 User,我是用 "neoneo:,設定密碼,然後加入到 contributors group
4.2 些換到 neoneo 登入,然後進到密碼設定頁,Unlock profile,然後就可以看到 Encrypted Password,變成明碼,然後Copy下來(後面用 neo_encrypted_pass 代表)
4.3 切換到 Admin/Security/General,然後把 Allow Anonymous Access 關掉
----------------- 注意 ----------------
如果沒有做這邊 2~4 步,還是可以用,可是就不用帳號密碼,就可以上傳 library 了
-----------------------------------------
第三步,進 Android Studio build library 囉
1. 把剛剛的 user/pass 加到系統預設參數裡面
編輯 $HOME_DIR/.gradle/gradle.properties,加入兩行,User name 和密碼記得要改成自己的
artifactory_username=neoneo
artifactory_password=neo_encrypted_pass
2. 建一個 Android Library Project
2.1 New Project ( 選擇 Empty Project 或是 No Activity )
2.2 在該 New Project 裡面,New Module,選擇 Android Library,名字假設是 com.mynewtest.hellotest
2.3 裡面放一個 Class,我這邊用的名稱是 GetHelloWorld,裡面有一個 Method : public String getHelloString().
3. 在 Root build.gradle 裡面,buildscript {} 設定中,加入一個 dependency
dependencies {
// Add this line
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.7.3"
}
4. 在 library build.gradle 最前面,加入 plugin
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
5. 在 library build.gradle 最後面,加入 publish declaration
def libraryGroupId = 'com.mynewtest.hellotest'def libraryVersion = '1.0.0' publishing { publications { aar(MavenPublication) { groupId libraryGroupId version libraryVersion artifactId project.getName() artifact("$buildDir/outputs/aar/${artifactId}-release.aar") } } } artifactory { contextUrl = 'http://127.0.0.1:8081/artifactory' publish { repository { repoKey = 'libs-release-local' username = artifactory_username password = artifactory_password } defaults { publications('aar') publishArtifacts = true properties = ['qa.level': 'basic', 'q.os': 'android', 'dev.team': 'core'] publishPom = true } } }
$ ./gradlew assembleRelease artifactoryPublish
如果成功,應該檔案就會上傳到 maven 上面了,可以登入進去 libs-release-local 看
----------------- Trouble Shooting --------------
如果 gradle build fail 了,
1. 先確認 $buildDir/outputs/aar/${artifactId}-release.aar 檔案在不在
2. 執行 ./gradlew --stacktrace --info assembleRelease artifactoryPublish 可以看到詳細錯誤的內容
3. 一定要是 Android Library Module 才會有 aar 可以上傳,一般的 activity 會出錯
第四步,進 Android Studio 使用 build 好的 library 囉
1. 建一個新的 project
2. 在 Top Project Level 的 build.gradle 裡面,allprojects {} 設定裡面,加入
allprojects {
repositories {
jcenter()
// Add these lines
maven {
url "http://IPADDRESS:8081/artifactory/libs-release-local"
credentials {
username = "${artifactory_username}"
password = "${artifactory_password}"
}
}
}
}
要修改的只有 IPADDRESS,另外兩帳號密碼是用 gradle settings 設定的,當然也以自己打上去
3. 在 app level build.gradle 增加
dependencies {
implementation "com.mynewtest.hellotest:hellotest:1.0.0"
}
或是
dependencies {
implementation(group: 'com.mynewtest.hellotest', name: 'hellotest', version: '1.0.0', ext: 'aar')
}
或是,登入進到 maven 網頁,進 Artifact 選單,libs-release-local 裡面,找到剛剛上傳的 aar,然後選擇左下的 gradle 設定,可以copy 出來用
4. resync project,應該就可以用 library 了
--------------- Trouble Shooting ----------------
a. 第三步一定要加在 allprojects {} 裡面,加錯地方會沒有用
b. 如果一直 build 不過,可以下
./gradlew clean assembleDebug --debug --info
可以看到最後去下載該 library 的 URL,實際看看該 URL 是否可以存取
------------------------------------------------------
終於寫完了,收工