-
Notifications
You must be signed in to change notification settings - Fork 2
/
sonatype-publish.gradle
154 lines (137 loc) · 5.94 KB
/
sonatype-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
//example: https://github.com/GetStream/stream-chat-android/blob/main/scripts/publish-mavencentral.gradle
//worked with https://dev.to/kengotoda/deploying-to-ossrh-with-gradle-in-2020-1lhi
apply plugin: 'maven-publish'
apply plugin: 'signing'
def libVersion = LIB_VERSION // This is the library version used when deploying the artifact
def enableDeploy = ENABLE_DEPLOY //Flag whether the ci/cd workflow should deploy to sonatype or not
def libGroupId = LIB_GROUP_ID // Maven Group ID for the artifact
def libArtifactId = LIB_ARTIFACT_ID // Maven Artifact ID for the artifact
def libName = LIB_NAME // Library name
def siteUrl = SITE_URL // Homepage URL of the library
def gitUrl = GIT_URL // Git repository URL
def libDescription = LIB_DESCRIPTION // Library description
group = libGroupId
version = libVersion
//retrieve variables from system environmental variables
def sonatypeUser = enableDeploy ? System.getenv('SONATYPE_USER') : ''
def sonatypePwd = enableDeploy ? System.getenv('SONATYPE_PWD') : ''
def sonatypeGpgKey = enableDeploy ? System.getenv('SONATYPE_GPG_KEY') : ''
def sonatypeGpgPwd = enableDeploy ? System.getenv('SONATYPE_GPG_PWD') : ''
def sonatypeGpgPath = enableDeploy ? System.getenv('SONATYPE_GPG_PATH') : ''
//signing plugin config
ext["signing.keyId"] = sonatypeGpgKey
ext["signing.password"] = sonatypeGpgPwd
ext["signing.secretKeyRingFile"] = sonatypeGpgPath
task androidJavadocs(type: Javadoc) {
excludes = ['**/*.kt'] // < ---- Exclude all kotlin files from javadoc file.
if (project.plugins.findPlugin("com.android.library")) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
else {
source = sourceSets.main.java.srcDirs
}
}
task javadocJar(type: Jar, dependsOn: androidJavadocs) {
archiveClassifier.set('javadoc')
if (project.plugins.findPlugin("com.android.library"))
from androidJavadocs.destinationDir
else
from javadoc.destinationDir
}
task sourcesJar(type: Jar) {
archiveClassifier.set('sources')
if (project.plugins.findPlugin("com.android.library")) {
from android.sourceSets.main.java.srcDirs
from android.sourceSets.main.kotlin.srcDirs
} else {
from sourceSets.main.java.srcDirs
from sourceSets.main.kotlin.srcDirs
}
}
artifacts {
archives javadocJar, sourcesJar
}
//publish plugin config
publishing {
repositories {
//add sonatype repo as mavenCentral (used in gradle task names later)
maven {
name = "mavenCentral"
def releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2"
def snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots"
url = version.toString().endsWith("SNAPSHOT") ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username = sonatypeUser
password = sonatypePwd
}
}
}
publications {
// Creates a Maven publication called "release"
release(MavenPublication) {
if (!enableDeploy) return true //if ci/cd still build the project, will only upload a pom file
// Publication config
groupId = libGroupId
artifactId = libArtifactId
version = libVersion
// Add aar/jar to artifacts to upload
if (project.plugins.findPlugin("com.android.library")) {
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
} else {
artifact("$buildDir/libs/${project.getName()}-${version}.jar")
}
artifact(javadocJar) //add javadoc artifact
artifact(sourcesJar) //add sources artifact
//pom file config
pom {
name = libName
description = libDescription
url = siteUrl
licenses {
license {
name = 'MIT'
url = 'https://opensource.org/licenses/MIT'
}
}
developers {
developer {
id = 'stefanosiano'
name = 'Stefano Siano'
email = 'stefano.siano91@gmail.com'
}
}
scm {
connection = "scm:git:$gitUrl"
developerConnection = "scm:git:$gitUrl"
url = siteUrl
}
withXml {
def dependenciesNode = asNode().appendNode("dependencies")
project.configurations.implementation.allDependencies.each {
if (it.group == "" || it.name == "" || it.name == "unspecified") return
if (it.name.endsWith("-bom")) {
def dependencyNode = dependenciesManagementNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
dependencyNode.appendNode("version", it.version)
dependencyNode.appendNode("scope", "import")
dependencyNode.appendNode("type", "pom")
} else {
def dependencyNode = dependenciesNode.appendNode("dependency")
dependencyNode.appendNode("groupId", it.group)
dependencyNode.appendNode("artifactId", it.name)
if (it.version != "") {
dependencyNode.appendNode("version", it.version)
}
}
}
}
}
}
}
}
signing {
if (!enableDeploy) return true //don't sign anything if not allowed
sign publishing.publications.release
}