-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish-groovy.gradle
164 lines (144 loc) · 6.48 KB
/
publish-groovy.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
// Apply Maven Publish and Signing plugins
apply plugin: 'maven-publish'
apply plugin: 'signing'
// Helper method to load properties from multiple files
def loadPropertiesFromFiles() {
def propertiesFiles = ['local.properties', 'credential.properties']
Properties p = new Properties()
propertiesFiles.each { filename ->
def propsFile = project.rootProject.file(filename)
if (propsFile.exists()) {
propsFile.withInputStream { stream -> p.load(stream) }
}
}
// Store properties in project.ext for global access
p.each { name, value -> ext[name] = value }
}
// Helper method to fetch required properties
def getRequiredProperty(String propertyName) {
if (!project.hasProperty(propertyName)) {
throw new GradleException("Missing required property: $propertyName")
}
return project.property(propertyName)
}
// Helper method to fetch optional properties (return null if not found)
def getOptionalProperty(String propertyName) {
return project.hasProperty(propertyName) ? project.property(propertyName) : null
}
loadPropertiesFromFiles()
// Root-level Nexus publishing setup
if (project == rootProject) {
def sonatypeUsername = project.findProperty('sonatype.username')
def sonatypePassword = project.findProperty('sonatype.password')
if (sonatypeUsername != null && sonatypePassword != null) {
nexusPublishing {
repositories {
sonatype {
username = sonatypeUsername
password = sonatypePassword
stagingProfileId = getRequiredProperty('sonatype.stagingProfileId')
// Optional URLs from configuration
def publishReleaseUrl = project.findProperty('sonatype.releaseUrl')
def publishSnapshotUrl = project.findProperty('sonatype.snapshotUrl')
if (publishReleaseUrl != null) {
nexusUrl.set(uri(publishReleaseUrl))
}
if (publishSnapshotUrl != null && version.endsWith('-SNAPSHOT')) {
snapshotRepositoryUrl.set(uri(publishSnapshotUrl))
}
}
}
}
}
}
// register the tasks base on Android library or Java/Kotlin library
if (project.plugins.findPlugin("com.android.library")) {
// Register the Android sources JAR task only if the project is an Android library
tasks.register('androidSourcesJar', Jar) {
from android.sourceSets.main.java.srcDirs
}
} else if (project.plugins.findPlugin("java") || project.plugins.findPlugin("java-library")) {
// Task to generate sources JAR only if the java plugin is applied
tasks.register('sourcesJar', Jar) {
archiveClassifier.set('sources')
from sourceSets.main.allSource
}
// Task to generate Javadoc JAR
tasks.register('javadocJar', Jar) {
dependsOn tasks.javadoc
archiveClassifier.set('javadoc')
from tasks.javadoc.destinationDir
}
}
// Use project-specific properties for group, artifactId, and version
// Only apply group and version if properties exist, otherwise skip
if (project.hasProperty('PUBLISH_GROUP_ID') && project.hasProperty('PUBLISH_VERSION')) {
group = getRequiredProperty('PUBLISH_GROUP_ID')
version = getRequiredProperty('PUBLISH_VERSION')
}
afterEvaluate {
if (project.hasProperty('PUBLISH_GROUP_ID') && project.hasProperty('PUBLISH_ARTIFACT_ID') && project.hasProperty('PUBLISH_VERSION')) {
publishing {
publications {
release(MavenPublication) {
groupId = group
artifactId = getRequiredProperty('PUBLISH_ARTIFACT_ID')
version = version
// Add components based on module type
if (project.plugins.findPlugin("com.android.library")) {
generateMetadataFileForReleasePublication.dependsOn tasks.named('androidSourcesJar')
from components.release
// Add sources JAR
artifact tasks.named('androidSourcesJar')
} else {
from components.java
// Add sources and javadoc JARs
artifact tasks.named('sourcesJar')
artifact tasks.named('javadocJar')
}
// Configure POM metadata
pom {
name = getRequiredProperty('PUBLISH_ARTIFACT_ID')
description = getRequiredProperty('PUBLISH_DESCRIPTION')
url = getRequiredProperty('PUBLISH_URL')
licenses {
license {
name = getRequiredProperty('PUBLISH_LICENSE_NAME')
//url = getRequiredProperty('PUBLISH_LICENSE_URL')
}
}
developers {
developer {
id = getRequiredProperty('PUBLISH_DEVELOPER_ID')
name = getRequiredProperty('PUBLISH_DEVELOPER_NAME')
email = getRequiredProperty('PUBLISH_DEVELOPER_EMAIL')
}
}
scm {
connection = getRequiredProperty('PUBLISH_SCM_CONNECTION')
developerConnection = getRequiredProperty('PUBLISH_SCM_DEVELOPER_CONNECTION')
url = getRequiredProperty('PUBLISH_SCM_URL')
}
}
}
}
// Maven repository configuration
repositories {
maven {
credentials {
username = project.findProperty('nexus.username')
password = project.findProperty('nexus.password')
}
def publishReleaseUrl = project.findProperty('nexus.releaseUrl')
def publishSnapshotUrl = project.findProperty('nexus.snapshotUrl')
url = version.endsWith('-SNAPSHOT') ? publishSnapshotUrl : publishReleaseUrl
allowInsecureProtocol = true
}
}
// Automatically sign publications
signing {
sign publishing.publications.release
}
}
}
}