使用Artifactory搭建私有Maven倉庫
阿新 • • 發佈:2018-10-31
在涉及到模組化開發過程中,我們想要將公開或公用元件 aar/jar 上傳至Maven Center or JCenter 倉庫中心(將Android專案開源到JCenter兩種方式),以便於持續整合。但是對於有些元件我們不想對外公開這時候就需要我們搭建一個本地私有倉庫了。
使用Artifactory簡單搭建本地倉庫
1. 下載並解壓 Artifactory
由此可見 artfactory
使用的是自帶的tomcat
進行搭建服務的,而最新版tomcat
依賴jdk 1.8
, 故
- 配置
java
環境 jdk
需要是1.8
版本
2. 執行
進入bin
目錄下,可以發現以下內容 :
windows
下 可以通過.bat
和.exe
進行操作linux
與macOS
可以通過.sh
檔案操作
通過兩種方式可以進行使用 artifactory
:
- 安裝方式 及
installService
和uninstallService
- 非安裝方式 :直接使用命令
artifactory.ssh
artifactory.bat
我這裡僅僅使用的是非安裝方式進行實現;
進入bin目錄,執行下面命令(macOS) :
$./artifactory.sh
windows雙擊artifactory.bat
執行成功的標誌:
驗證執行是否成功瀏覽器訪問:瀏覽器訪問:http://localhost:8081/artifactory/ 如果沒問題則會出出現引導頁面新建 admin
賬戶密碼,如果選擇gradle
的話,會生成下面4個Repository
:
3. 上傳library
這裡以 android
android studio
中配置library
的 build.gradle
進行上傳,共分為3步 :
- 配置專案根目錄下的
build.gradle
:下面稱為root_gradle
- 配置專案下 library下的
build.gradle
: 下面稱為library_gradle
- 測試上傳
3.1 配置 root_gradle
3.2.1 apply plugin
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
3.2.2 setting pom
def MAVEN_LOCAL_PATH ='http://localhost:8081/artifactory'
def ARTIFACT_ID = 'zcmain'
def VERSION_NAME = '1.0.0'
def GROUP_ID = 'com.zc.zcmain'
publishing {
publications {
aar(MavenPublication) {
groupId GROUP_ID
version = VERSION_NAME
artifactId ARTIFACT_ID
//知道上傳包的位置
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each{
// 如果有compile fileTree(),group會為空,需要去除
if(it.group != null) {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', it.version)
}
}
}
}
}
}
3.2.3 setting artifactorypublish
artifactory {
contextUrl = MAVEN_LOCAL_PATH
publish {
repository {
// The Artifactory repository key to publish to
repoKey = 'gradle-release-local'
username = "zcmain" //賬戶
password = "pass123456" //密碼
}
defaults {
// Tell the Artifactory Plugin which artifacts should be published to Artifactory.
publications('aar')
publishArtifacts = true
// Properties to be attached to the published artifacts.
properties = ['qa.level': 'basic', 'dev.team': 'core']
// Publish generated POM files to Artifactory (true by default)
publishPom = true
}
}
}
這裡僅僅是簡單的配置使用,當然你也可以查些高階的使用,比如在 gradle.properties中進行配置的安全性做法,這裡就不深入了。
artifactory_user=${security.getCurrentUsername()}
artifactory_password=${security.getEncryptedPassword()!"insert password"}
artifactory_contextUrl=http://localhost:8081/artifactory
3.2.4 完整的gradle配置
這裡說明幾點 :
- 在這裡上傳的事
release.aar
包,故執行命令的時候需要執行release
打包 artifactory/publish/repository/repokey
是你要上傳的respository
名稱,當然可以新建
apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
def MAVEN_LOCAL_PATH ='http://localhost:8081/artifactory'
def ARTIFACT_ID = 'libararya'
def VERSION_NAME = '1.0.0'
def GROUP_ID = 'com.zc.zcmain'
publishing {
publications {
aar(MavenPublication) {
groupId GROUP_ID
version = VERSION_NAME
artifactId ARTIFACT_ID
// Tell maven to prepare the generated "*.aar" file for publishing
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
pom.withXml {
def dependencies = asNode().appendNode('dependencies')
configurations.compile.allDependencies.each{
// 如果有compile fileTree(),group會為空,需要去除
if(it.group != null) {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', it.group)
dependency.appendNode('artifactId', it.name)
dependency.appendNode('version', it.version)
}
}
}
}
}
}
artifactory {
contextUrl = MAVEN_LOCAL_PATH
publish {
repository {
// The Artifactory repository key to publish to
repoKey = 'gradle-release-local'
username = "zcmain"
password = "pass123456
defaults {
// Tell the Artifactory Plugin which artifacts should be published to Artifactory.
publications('aar')
publishArtifacts = true
// Properties to be attached to the published artifacts.
properties = ['qa.level': 'basic', 'dev.team': 'core']
// Publish generated POM files to Artifactory (true by default)
publishPom = true
}
}
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 18
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.0'
testCompile 'junit:junit:4.12'
}
3.3 測試上傳
- 命令上傳
- 圖形化
步驟如下 :
- assembleRelease : 打release 包
- generatePomFileForAarPublication : 生成 pom.xml 檔案
- artifactoryPublish :上傳
命令順序執行圖形化命令即可;
成功後,登入 Jfrog Artifactory 檢視檔案:4. 測試使用
使用分下面步驟實現:
- 配置 倉庫地址 : root_gradle
- 依賴配置 : app_gradle
- 使用
4.1 配置root_gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
}
}
allprojects {
repositories {
jcenter()
//配置本地倉庫
maven { url "http://localhost:8081/artifactory/gradle-release-local/" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
4.2 配置 app_gradle
重新Rebuild後可件庫檔案已經被引入專案:
com.zc.zcmain:librarya:1.0.0
基本組成為:
group_id:artifact_Id:version_name
- group_id : com.zc.zcmain
- artifact_id : libararya
- version_name: 1.0.0
參考: