-
Notifications
You must be signed in to change notification settings - Fork 83
/
publish-mavencentral.gradle
113 lines (106 loc) · 4.17 KB
/
publish-mavencentral.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
apply plugin: 'maven-publish'
apply plugin: 'signing'
task androidSourcesJar(type: Jar) {
archiveClassifier.set("sources")
from android.sourceSets.main.java.source
exclude "**/R.class"
exclude "**/BuildConfig.class"
}
// 强制 Java/JavaDoc 等的编码为 UTF-8
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
tasks.withType(Javadoc) {
options.encoding = "UTF-8"
}
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def sonatypeUsername = properties.getProperty("sonatypeUsername")
def sonatypePassword = properties.getProperty("sonatypePassword")
publishing {
publications {
mavenJava(MavenPublication) {
// group id,发布后引用的依赖的 group id
groupId publishedGroupId
// 发布后引用的依赖的 artifact id
artifactId artifact
// 发布的版本
version libraryVersion
// 发布的 arr 的文件和源码文件
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
artifact androidSourcesJar
pom {
// 构件名称,可以自定义
name = libraryName
// 构件描述
description = libraryDescription
// 构件主页
url = siteUrl
// 许可证名称和地址
licenses {
license {
name = licenseName
url = licenseUrl
}
}
// 开发者信息
developers {
developer {
id = developerId
name = developerName
email = developerEmail
}
}
// 版本控制仓库地址
scm {
url = siteUrl
connection = gitUrl
developerConnection = gitUrl
}
// 解决依赖关系
withXml {
def dependenciesNode = asNode().appendNode('dependencies')
project.configurations.all { configuration ->
def name = configuration.name
if (name != "implementation" && name != "compile" && name != "api") {
return
}
println(configuration)
configuration.dependencies.each {
println(it)
if (it.name == "unspecified") {
// 忽略无法识别的
return
}
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
if (name == "api" || name == "compile") {
dependencyNode.appendNode("scope", "compile")
} else { // implementation
dependencyNode.appendNode("scope", "runtime")
}
}
}
}
}
}
}
repositories {
maven {
// 发布的位置,这里根据发布的版本区分了 SNAPSHOT 和最终版本两种情况
def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
// 这里就是之前在 issues.sonatype.org 注册的账号
username sonatypeUsername
password sonatypePassword
}
}
}
}
signing {
sign publishing.publications
}