-
Notifications
You must be signed in to change notification settings - Fork 4
/
_publish.gradle
199 lines (168 loc) · 6.84 KB
/
_publish.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
apply plugin: 'maven-publish'
apply plugin: 'signing'
task androidSourcesJar(type: Jar) {
archiveClassifier.set('sources')
if (project.plugins.findPlugin("com.android.library")) {
// For Android libraries
from android.sourceSets.main.java.srcDirs
from android.sourceSets.main.kotlin.srcDirs
} else {
// For pure Kotlin libraries, in case you have them
from sourceSets.main.java.srcDirs
from sourceSets.main.kotlin.srcDirs
}
}
artifacts {
archives androidSourcesJar
}
group = PUBLISH_GROUP_ID
version = PUBLISH_VERSION
ext["signing.keyId"] = ''
ext["signing.password"] = ''
ext["signing.secretKeyRingFile"] = ''
ext["ossrhUsername"] = ''
ext["ossrhPassword"] = ''
ext["sonatypeStagingProfileId"] = ''
File secretPropsFile = project.rootProject.file('local.properties')
if (secretPropsFile.exists()) {
Properties p = new Properties()
p.load(new FileInputStream(secretPropsFile))
p.each { name, value ->
ext[name] = value
}
} else {
ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
ext["sonatypeStagingProfileId"] = System.getenv('SONATYPE_STAGING_PROFILE_ID')
}
nexusStaging {
packageGroup = PUBLISH_GROUP_ID
stagingProfileId = sonatypeStagingProfileId
username = ossrhUsername
password = ossrhPassword
serverUrl = "https://s01.oss.sonatype.org/service/local/"
delayBetweenRetriesInMillis = 30000
}
task lookForArtifacts {
group "upload"
doLast {
def pomFileName = "${PUBLISH_ARTIFACT_ID}-${PUBLISH_VERSION}.pom"
def artifactPath = "${PUBLISH_GROUP_ID.replace(".", "/")}/${PUBLISH_ARTIFACT_ID}/${PUBLISH_VERSION}/${pomFileName}"
def repositoryUrl = "https://s01.oss.sonatype.org/service/local/repositories/releases/content/${artifactPath}"
println("# searching for existing artifact wit id ${PUBLISH_VERSION}")
println("$repositoryUrl")
if (urlExists(repositoryUrl)) {
println("# Existing artifact found")
println("")
project.tasks.tasks.getByName("publishReleasePublicationToSonatypeRepository").enabled = false
} else {
println("# No existing artifact found. Preceding to publish")
println("")
}
println("${project} enabled: ${project.tasks.publish.enabled}")
}
}
def urlExists(String repositoryUrl) {
try {
def connection = (HttpURLConnection) new URL(repositoryUrl).openConnection()
connection.setRequestProperty("Authorization", "Basic " + getBase64EncodedCredentials())
connection.setConnectTimeout(10000)
connection.setReadTimeout(10000)
connection.setRequestMethod("HEAD")
def responseCode = connection.getResponseCode()
if (responseCode == 401) {
throw new RuntimeException("Unauthorized MavenUser user. Please provide valid username and password.")
}
return (200 == responseCode)
} catch (IOException ignored) {
println(ignored)
return false
}
}
def getBase64EncodedCredentials() {
def s = "${ossrhUsername}" + ":" + "${ossrhPassword}"
return s.bytes.encodeBase64().toString()
}
afterEvaluate {
tasks.getByName("publishReleasePublicationToSonatypeRepository").dependsOn lookForArtifacts
}
publishing {
publications {
release(MavenPublication) {
// The coordinates of the library, being set from variables that
// we'll set up later
groupId PUBLISH_GROUP_ID
artifactId PUBLISH_ARTIFACT_ID
println("$artifactId : $version")
version PUBLISH_VERSION
// Two artifacts, the `aar` (or `jar`) and the sources
if (project.plugins.findPlugin("com.android.library")) {
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
} else {
artifact("$buildDir/libs/${project.getName()}-${version}.jar")
artifact("$buildDir/libs/${project.getName()}-${version}-javadoc.jar")
}
artifact androidSourcesJar
// Mostly self-explanatory metadata
pom {
name = PUBLISH_ARTIFACT_ID
description = 'A simplified use of SharedPreferences and Preferences DataStore.'
url = 'https://github.com/ch4rl3x/settings'
licenses {
license {
name = 'Apache-2.0 License'
url = 'https://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'ch4rl3x'
name = 'Alexander Karkossa'
email = 'alexander.karkossa@googlemail.com'
}
developer {
id = 'kalinjul'
name = 'Julian Kalinowski'
email = 'j@kalinowski.email'
}
// Add all other devs here...
}
// Version control info - if you're using GitHub, follow the format as seen here
scm {
connection = 'scm:git:github.com/ch4rl3x/settings.git'
developerConnection = 'scm:git:ssh://github.com/ch4rl3x/settings.git'
url = 'https://github.com/ch4rl3x/settings/tree/main'
}
// A slightly hacky fix so that your POM will include any transitive dependencies
// that your library builds upon
withXml {
def dependenciesNode = asNode().appendNode('dependencies')
project.configurations.implementation.allDependencies.each {
def dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', it.group)
dependencyNode.appendNode('artifactId', it.name)
dependencyNode.appendNode('version', it.version)
}
}
}
}
}
repositories {
maven {
name = "sonatype"
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 {
username ossrhUsername
password ossrhPassword
}
}
}
}
signing {
sign publishing.publications
}