-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-mavencentral.gradle
147 lines (137 loc) · 4.84 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
apply plugin: 'maven-publish'
apply plugin: 'signing'
def isAar = project.plugins.hasPlugin('com.android.library')
if (isAar) {
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.source
}
} else {
task javadocJar(type: Jar) {
archiveClassifier.set("javadoc")
classifier = "javadoc"
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allJava
}
}
File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
println "Found secret props file, loading props"
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value -> ext[name] = value
}
} else {
println "No props file, loading env vars"
}
def configurePublication = { MavenPublication publication ->
groupId PUBLISH_GROUP_ID
artifactId PUBLISH_ARTIFACT_ID
version PUBLISH_VERSION
def uploadJar = "mavenJava" == publication.name
if (uploadJar) {
from components.java
artifact sourcesJar
artifact javadocJar
} else {
artifact tasks.getByName("bundleReleaseAar")
artifact androidSourcesJar
}
pom {
name = PUBLISH_ARTIFACT_ID
description = 'XPicker'
url = 'https://github.com/wangshuwen1107/XPicker'
licenses {
license {
//协议类型,一般默认Apache License2.0的话不用改:
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'wangshuwen1107'
name = 'wangshuwen1107'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:https://github.com/wangshuwen1107/XPicker.git'
developerConnection = 'scm:git:[email protected]:wangshuwen1107/XPicker.git'
url = 'https://github.com/wangshuwen1107/XPicker'
}
withXml {
if (!uploadJar) {
def dependenciesNode = asNode().appendNode('dependencies')
project.configurations.implementation.allDependencies.each {
if (null != it.group && null != it.name && null != it.version) {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
}
afterEvaluate {
publishing {
publications {
if (isAar) {
release(MavenPublication, configurePublication)
} else {
mavenJava(MavenPublication, configurePublication)
}
}
repositories {
maven {
url = uri('../repo')
}
}
}
}
signing {
sign publishing.publications
}
task zipRepo(type: Zip) {
group = "publishing"
dependsOn isAar ? 'publishReleasePublicationToMavenRepository' : 'publishMavenJavaPublicationToMavenRepository'
def componentDirPath = "${PUBLISH_GROUP_ID.replace(".", "/")}/$PUBLISH_ARTIFACT_ID/$PUBLISH_VERSION"
archiveBaseName = PUBLISH_ARTIFACT_ID
archiveVersion = PUBLISH_VERSION
destinationDirectory = file("../publish")
from("${rootProject.projectDir}/repo/$componentDirPath") {
into("$componentDirPath")
}
}
task publishSonatype {
group = "publishing"
dependsOn 'zipRepo'
doLast {
def publishZipFile = file("../publish/$PUBLISH_ARTIFACT_ID-$PUBLISH_VERSION" + ".zip")
def uploadToken = Base64.getEncoder().encodeToString("$ossrhUsername:$ossrhPassword".getBytes("UTF-8"))
println "发布 uploadToken=$uploadToken"
println "发布 bundle=${publishZipFile.absolutePath}"
def curlOutput = new ByteArrayOutputStream()
def curlError = new ByteArrayOutputStream()
try {
exec {
commandLine 'curl', '--request', 'POST',
'--header', "Authorization: Bearer $uploadToken",
'--form', "bundle=@${publishZipFile.absolutePath}",
'--form', "publishingType=AUTOMATIC",
//'--form', "publishingType=USER_MANAGED",
'https://central.sonatype.com/api/v1/publisher/upload'
standardOutput = curlOutput
errorOutput = curlError
}
println "发布结果*********: ${curlOutput.toString()}"
} catch (Exception e) {
println "发布失败*********: ${curlError.toString()}"
throw e
}
}
}