-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaven_push.gradle
116 lines (103 loc) · 4.67 KB
/
maven_push.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// The Maven plugin adds support for deploying artifacts to Maven repositories.
// 一个可以让你把库上传到maven仓库的插件
apply plugin: 'maven'
// The signing plugin adds the ability to digitally sign built files and artifacts. These digital signatures can then be used to prove who built the artifact the signature is attached to as well as other information such as when the signature was generated.
// 对库文件进行数字签名的插件,可以通过签名知道谁创建了这个库文件,签名的时间等等信息
apply plugin: 'signing'
// 声明变量记录maven库地址
def mavenRepositoryUrl
// 判断是发布到正式库,还是snapshots库
if (isReleaseBuild()) {
println 'RELEASE BUILD'
// 下面的库地址指向的是我们私有仓库的Releases 仓库
mavenRepositoryUrl = hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL
: "http://localhost:32768/nexus/content/repositories/releases/"
} else {
println 'SNAPSHOTS BUILD'
// 下面的库地址指向的是我们私有仓库的snapshots 仓库
mavenRepositoryUrl = hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL
: "http://localhost:32768/nexus/content/repositories/snapshots/"
}
// NEXUS_USERNAME等变量在我们主项目的gradle.properties中可以找到
def getRepositoryUsername() {
return hasProperty('NEXUS_USERNAME') ? NEXUS_USERNAME : ""
}
def getRepositoryPassword() {
return hasProperty('NEXUS_PASSWORD') ? NEXUS_PASSWORD : ""
}
// 根据我们在likelib下gradle.properties中声明的版本名称,来分辨是Release版本还是 snapshots版本
def isReleaseBuild() {
return !VERSION_NAME.contains("SNAPSHOT");
}
//"afterEvaluate是什么鸟?你可以理解为在配置阶段要结束,项目评估完会走到这一步。" 引用自http://jiajixin.cn/2015/08/07/gradle-android/
afterEvaluate { project ->
// 我们声明我们要执行的上传到maven的task
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
// 我们类比下compile com.squareup.okhttp:okhttp:2.7.0
// artifactId 对应com.squareup.okhttp; groupId 对应okhttp;version对应2.7.0
// 这样就类似坐标的方式定位到了制定的库文件
pom.artifactId = POM_ARTIFACT_ID
pom.groupId = POM_GROUP_ID
pom.version = VERSION_NAME
// 授权验证,这里也就是你登陆搭建的私服服务器时候的用户名\密码
repository(url: mavenRepositoryUrl) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
}
// 这里是配置我们maven库需要的pom.xml文件的各个内容,具体意思我们在主目录gradle.properties中解释
pom.project {
name POM_NAME
packaging POM_PACKAGING
description POM_DESCRIPTION
url POM_URL
scm {
url POM_SCM_URL
connection POM_SCM_CONNECTION
developerConnection POM_SCM_DEV_CONNECTION
}
licenses {
license {
name POM_LICENCE_NAME
url POM_LICENCE_URL
distribution POM_LICENCE_DIST
}
}
developers {
developer {
id POM_DEVELOPER_ID
name POM_DEVELOPER_NAME
}
}
}
}
}
}
// 进行数字签名
signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
sign configurations.archives
}
// type显示指定任务类型或任务, 这里指定要执行Javadoc这个task,这个task在gradle中已经定义
task androidJavadocs(type: Javadoc) {
// 设置源码所在的位置
source = android.sourceSets.main.java.sourceFiles
}
// 生成javadoc.jar
task androidJavadocsJar(type: Jar) {
// 指定文档名称
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
// 生成sources.jar
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.sourceFiles
}
// 产生相关配置文件的任务
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
}