搭建一个本地jFrog Artifactory并上传库文件

前情提要

前段时间,公司想搞hybrid开发,老司机觉得web里的url跳转挺方便的,稍稍调研了下,就自己撸了一个AndroidRouter库。
差不多市面上该有的功能都有:

  • 根据URL跳转到Activity
  • 通过Helper跳转到Activity
  • 同一个Activity可以映射多个URL
  • 跳转Activity的时候可以通过URL参数将变量的值注入到Activity中(支持基本类型和实现序列化接口的类)
  • 通过H5打开Native页面
  • 统一解决savedInstanceState的注入与读取

用着也还挺舒服的,他就想推广到其他项目里,但是每个工程重新拷贝一份Library代码,对代码管理和版本管理什么的都不太方便,于是就有了今天这出:
搭建一个本地的maven仓库,用gradle的compile进行引用,再方便不过了。
在网上看的时候,都说jFrog Artifactory比较好用,就随大流啦。

本文使用环境

item info
系统 osx 10.11.6
artifactory版本 5.1.0

安装 & 配置

官网有两种方式进行安装:

  1. 下载安装包(安装包里有各个平台的安装文件)
  2. 用docker

因为对docker不是很熟,而且安装包安装方式听说也挺简单的,就着最简原则,就用第一种方式安装了。

预安装

jFrog Artifactory得用java8(自带的tomcat需要),如果没有安装或者安装了多个版本的java需要进行切换(mac下可以用jenv),请自行解决:)

安装jFrog Artifactory

这里下载zip包,解压并进入bin文件夹,执行对应的安装程序:

  • mac/linux: artifactory.sh
  • windows: artifactory.bat

安装过程挺快的,完成以后,浏览器里输入:http://localhost:8081/artifactory
首先会让你设置admin等信息,然后就可以用了
我默认创建了一个gradle repository,生成了4个子Repository:

  • gradle-dev
  • gradle-release
  • gradle-dev-local
  • gradle-release-local (我们内部库默认上传到这里)

直到这里都还挺顺利的,随后就要入一个自己挖的大坑了!

library中配置上传信息

项目构成

1
2
3
4
5
android_router //workspace
app //测试工程
annotation-lib //通用注解部分,它被其他3部分所引用
api-lib //Router and Helper,被app引用
compiler-lib //生成帮助类的apt库,被app引用

配置

一开始是按这篇教程配置的
虽然能正常生成和上传,但是因为有依赖工程(annotation-lib),引用的时候报错:

1
android_router:annotation-lib:unspecified

试了半天,怀疑是api-libcompiler-lib中引用的annotation-lib库是以project的形式引用,而不是以jar方式
于是就想能不能把pom.xml中的依赖重写一下,google一下还真有,所以就有了以下的配置。

修改android_router下的build.gradle

添加本地仓库

1
2
3
4
5
6
7
8
9
10
11
buildscript {
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
}
}
allprojects {
repositories {
maven { url "http://server_address:8081/artifactory/gradle-release-local" }
//...
}
}

修改annotation-lib下的build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
def MAVEN_LOCAL_PATH ='http://server_address:8081/artifactory'
def ARTIFACT_ID = 'annotation'
def VERSION_NAME = '1.0.0'
def GROUP_ID = 'com.router'
publishing {
publications {
aar(MavenPublication) {
groupId GROUP_ID
version = VERSION_NAME
artifactId ARTIFACT_ID
//----- !!!为了别的项目能读到当前的配置!!! -----
project.ext.group = GROUP_ID
project.ext.version = VERSION_NAME
project.ext.artifactId = ARTIFACT_ID
// Tell maven to prepare the generated "*.aar" file for publishing
artifact("$buildDir/libs/${project.getName()}.jar")
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 = artifactory_username
password = artifactory_password
}
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
}
}
}
  • 说明
    artifactory_usernameartifactory_password定义在gradle.properties中,下同

修改api-lib下的build.gradle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
apply plugin: 'com.jfrog.artifactory'
apply plugin: 'maven-publish'
def MAVEN_LOCAL_PATH ='http://server_address:8081/artifactory'
def ARTIFACT_ID = 'api'
def VERSION_NAME = '1.0.0'
def GROUP_ID = 'com.router'
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{
def depGroup = it.group
def depName = it.name
def depVersion = it.version
//如果当前库是annotation-lib,重写依赖
if(it.name == "annotation-lib") {
for (proj in project.parent.subprojects) {
if(proj.name == "annotation-lib") {
//这些信息是在annotation-lib的build.gradle中定义的
depGroup = proj.ext.group
depName = proj.ext.artifactId
depVersion = proj.ext.version
}
}
}
if(it.group != null) {
def dependency = dependencies.appendNode('dependency')
dependency.appendNode('groupId', depGroup)
dependency.appendNode('artifactId', depName)
dependency.appendNode('version', depVersion)
}
}
}
}
}
}
artifactory {
contextUrl = MAVEN_LOCAL_PATH
publish {
repository {
// The Artifactory repository key to publish to
repoKey = 'gradle-release-local'
username = artifactory_username
password = artifactory_password
}
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
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile project(':annotation-lib')
}
  • compiler-lib的build.gradle配置同上

实测

配置好以后,怀着激动的心情敲下如下代码:

1
2
./gradlew clean assembleRelease
./gradlew artifactoryPublish

编译上传都成功!!!(≧▽≦)/

然后放到其他项目里一试…..一…..试……😱,什么鬼…还是那个错:

1
android_router:annotation-lib:unspecified

尝试了小半个下午都没解决(有时候想不通的时候,真该停下来,也许反而能增加不少效率!)
第二天突然开窍,会不会是缓存的问题(AndroidStudio的dataBinding抽风的时候clean再重启就好了),试了下,更改了一下上传的lib版本号,果然可以了!

总结

做事的时候能静下心好好做了,但是思维漏洞有时候还是难以填补,需要更多的经验。

参考

jEnv官网
在OS X中使用jEnv管理多个Java版本
30分钟搭建一个android的私有Maven仓库
ArtifactoryExample
Maven-publish cannot not apply withXML on pom file (No signature of method)
Gradle Prject Properties

PS

今天妇女节,下午所有女生放假,啥时候也给男生放一个啊

热评文章