-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpublish-aar.gradle
486 lines (400 loc) · 19.5 KB
/
publish-aar.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//Version 2.2.2
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'maven-publish'
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
source += "${buildDir}/generated/source"
if (project.hasProperty('moduleExtraSourcePaths')) {
source += moduleExtraSourcePaths
}
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
android.libraryVariants.all { variant ->
if (variant.name == 'release') {
owner.classpath += variant.javaCompileProvider.get().classpath
}
}
exclude '**/R.html', '**/R.*.html', '**/index.html', '**/*.kt'
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
if (plugins.hasPlugin("kotlin-android")) {
//Add kotlin sources if we're using kotlin. This should be a superset of sourceSets.java,
//so ignore duplicates
from android.sourceSets.main.kotlin.srcDirs
}
if (project.hasProperty('moduleExtraSourcePaths')) {
from moduleExtraSourcePaths
}
duplicatesStrategy 'exclude'
}
project.afterEvaluate {
androidJavadocs.classpath += files(android.libraryVariants.collect { variant ->
variant.javaCompileProvider.get().classpath.files
})
def yotiRepoName = "release"
publishing {
publications {
def tasksContainer = project.getTasks()
def allTaskNames
def includeDebug = project.hasProperty('publishReleaseOnly') ?
!Boolean.valueOf(publishReleaseOnly) : true
if (project.hasProperty('productFlavors')) {
def parts = productFlavors.split(",")
allTaskNames = new ArrayList<?>(parts.length * 2)
for (part in parts) {
if (includeDebug) {
allTaskNames.add(tasksContainer.getByName("bundle${part.capitalize()}DebugAar"))
}
allTaskNames.add(tasksContainer.getByName("bundle${part.capitalize()}ReleaseAar"))
}
} else {
allTaskNames = new ArrayList<?>(2)
allTaskNames.add(tasksContainer.getByName("bundleReleaseAar"))
if (includeDebug) {
allTaskNames.add(tasksContainer.getByName("bundleDebugAar"))
}
}
// Create a task for debug and for release build task
for (art in allTaskNames) {
//Must build generated code first for Javadoc to work
androidJavadocs.dependsOn(art)
androidSourcesJar.dependsOn(art)
def buildVariantFromTask = art.name.substring(6, art.name.indexOf("Aar"))
"aar${buildVariantFromTask}"(MavenPublication) {
artifact art
artifact androidJavadocsJar
def includeSources = project.hasProperty('includeSources') ?
Boolean.valueOf(includeSources) : true
if (includeSources) {
artifact androidSourcesJar
}
// Check if we need to rename some artefacts id (includes publication and dependencies)
ArtefactFlavorProperties flavorsConf
if (project.hasProperty('omitFlavorSuffixFor')) {
flavorsConf = new ArtefactFlavorProperties(omitFlavorSuffixFor)
} else {
flavorsConf = ArtefactFlavorProperties.EMPTY
}
def buildTypeLower
def artifactIdSuffix
def productFlavorLower
def currentArtefact = new PropertyDependency(pomGroup, pomId, pomVersion)
if (buildVariantFromTask.contains("Release")) {
buildTypeLower = "release"
productFlavorLower = buildVariantFromTask.substring(0, buildVariantFromTask.indexOf("Release")).toLowerCase()
} else {
buildTypeLower = "debug"
productFlavorLower = buildVariantFromTask.substring(0, buildVariantFromTask.indexOf("Debug")).toLowerCase()
}
def removeFlavor = flavorsConf.ignoreFlavorFor(currentArtefact, productFlavorLower)
artifactIdSuffix = getIdSuffix(buildVariantFromTask, buildTypeLower, productFlavorLower, removeFlavor)
pom {
if (project.hasProperty('pomName')) {
name = pomName
} else {
name = pomDescription
}
if (project.hasProperty('pomUrl')) {
url = pomUrl
} else {
url = "https://github.com/getyoti"
}
groupId = pomGroup
artifactId = pomId + artifactIdSuffix
version = pomVersion
packaging = "aar"
description = pomDescription
scm {
url = pomScmUrl
connection = pomScmConnection
}
def resolvedLicenceName = project.hasProperty('pomLicenseName') ? pomLicenseName : null
def resolvedLicenceUrl = project.hasProperty('pomLicenseUrl') ? pomLicenseUrl : null
if (resolvedLicenceName == null || resolvedLicenceName.trim().length() == 0) {
resolvedLicenceName = "For internal use only"
resolvedLicenceUrl = null
}
licenses {
license {
name = resolvedLicenceName
if (resolvedLicenceUrl != null) {
url = resolvedLicenceUrl
}
}
}
developers {
developer {
id = "yoti-ci"
name = "Yoti"
}
}
}
pom.withXml {
final dependenciesNode = asNode().appendNode('dependencies')
ext.addDependency = { Dependency dep, String scope ->
if (dep.group == null || dep.version == null || dep.name == null ||
dep.name == "unspecified") {
return // ignore invalid dependencies
}
// For project we need to get the unspecified version, the group and the
// artifact id from the configuration
def localDep = dep
if (dep instanceof ProjectDependency) {
if (project.hasProperty('projectDependencies')) {
def propertiesDependencies = PropertyDependency.parseModuleToDependencyProperty(projectDependencies)
localDep = propertiesDependencies[dep.name]
// If it is null it's most likely because the user entered the
// mapping with a typo
if (localDep == null) {
throw new InvalidUserDataException("The 'projectDependencies gradle property':[$projectDependencies] must contain a typo as " +
"'${dep.name}' is not found in the list of dependencies")
}
// Check if the property dependency is name only, if so, use the
// group and version from the dependency (meaning the user has
// defined them on the root of the module gradle file). Only the
// name will be used from the mapping to resolve the artifact.
if (localDep.isNameOnly()) {
localDep = new PropertyDependency(dep.group, localDep.name, dep.version)
}
} else if (dep.version == "unspecified") {
throw new InvalidUserDataException("A project dependency: '${dep.name}' " +
"is used but no mapping has been found.\n" +
"Please add the property in your " +
"gradle.properties: projectDependencies=${dep.name}/yourGroup:yourName:yourVersion\n" +
"Or add ext.pomId, ext.pomGroup and ext.pomVersion to the " +
"module: '${dep.name}'")
} else {
logger.warn("Warning this project dependency: '${dep.name}' is using the" +
" module name as artefact id since no property mapping has" +
" been found, group is '${dep.group}' and version is '${dep.version}'")
}
def ignoreFlavorForCurrentDep = flavorsConf.ignoreFlavorFor(localDep, productFlavorLower)
def suffix = getIdSuffix(buildVariantFromTask, buildTypeLower, productFlavorLower, ignoreFlavorForCurrentDep)
//Use correct debug suffix for internal projects (assumes all
//dependencies also use this script)
if (localDep.group.startsWith("com.yoti")) {
localDep = new PropertyDependency(dep.group, localDep.name + suffix, dep.version)
}
} else if (dep.version == "unspecified") {
throw new InvalidUserDataException("A dependency: '$dep' is used but no version has been found.\n" +
"Please add a version for this dependency")
}
final dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', localDep.group)
dependencyNode.appendNode('artifactId', localDep.name)
dependencyNode.appendNode('version', localDep.version)
dependencyNode.appendNode('scope', scope)
if (!dep.transitive) {
// If this dependency is not transitive, we should force exclude all its
// dependencies from the POM
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
exclusionNode.appendNode('groupId', '*')
exclusionNode.appendNode('artifactId', '*')
} else if (!dep.properties.excludeRules.empty) {
// Otherwise, if we have exclude rules, add them
final exclusionNode = dependencyNode.appendNode('exclusions').appendNode('exclusion')
dep.properties.excludeRules.each { ExcludeRule rule ->
exclusionNode.appendNode('groupId', rule.group ?: '*')
exclusionNode.appendNode('artifactId', rule.module ?: '*')
}
}
}
def scopes = ["compile", "api", "implementation"]
for (scope in scopes) {
def scopeConfigurations = new ArrayList<String>(4)
//api
scopeConfigurations.add(scope)
//debugApi
scopeConfigurations.add(buildTypeLower + scope.capitalize())
if (!productFlavorLower.empty) {
//stagingApi
scopeConfigurations.add(productFlavorLower + scope.capitalize())
//stagingDebugApi
scopeConfigurations.add(productFlavorLower + buildTypeLower.capitalize() + scope.capitalize())
}
for (confName in scopeConfigurations) {
configurations.getByName(confName) {
getDependencies().each { dep ->
//Ignore kotlin transitive dependencies as they will be applied
// by the kotlin plugin
if (!(dep.group == "org.jetbrains.kotlin" &&
(dep.name.contains("kotlin-stdlib")))
) {
addDependency(dep, scope)
}
}
}
}
}
}
}
}
}
}
def publicationsDependencies = []
for (pub in publishing.publications) {
// Used to create an aggregation task for all publication
publicationsDependencies << "publish${pub.name.capitalize()}PublicationTo${yotiRepoName.capitalize()}Repository"
}
task publishToYoti(dependsOn: publicationsDependencies, group: "publishing")
}
String getIdSuffix(String buildVariant, String buildType, String flavor, boolean ignoreFlavor) {
if (buildVariant.contains("Release")) {
return ignoreFlavor ? "" : "-$flavor"
} else {
return ignoreFlavor ? "-debug" : "-$flavor-debug"
}
}
/**
* Process a string composed this way:
* $moduleName1/$group1:$name1:$version1,$moduleName2/$group2:$name2:$version2
* Or:
* $moduleName1/$name1,$moduleName2/$group2:$name2:$version2
* in which case $name1 will be used as the artifact name and group and version will stay null.
* and create
* a Map of $moduleName -> PropertyDependency{group = group, name = name, version = version}*
* */
class PropertyDependency {
public String group = null
public String name = null
public String version = null
PropertyDependency(String colonSeparated) {
def parts = colonSeparated.split(":")
if (parts.length == 3) {
group = parts[0].trim()
name = parts[1].trim()
version = parts[2].trim()
} else if (parts.length == 1) {
name = parts[0].trim()
}
}
PropertyDependency(String aGroup, String aName, String aVersion) {
group = aGroup
name = aName
version = aVersion
}
String toString() {
return group + ":" + name + ":" + version
}
boolean isNameOnly() {
return !group?.trim() && name != null && !version?.trim()
}
boolean equals(obj) {
if (this.is(obj)) return true
if (getClass() != obj.class) return false
PropertyDependency dep = (PropertyDependency) obj
if (dep.isNameOnly()) {
return name == dep.name
} else {
return group == dep.group && name == dep.name && version == dep.version
}
}
static Map<String, PropertyDependency> parseModuleToDependencyProperty(String propertyValue) {
def propertiesDependencies = [:]
for (propDep in propertyValue.split(",")) {
def parts = propDep.split("/")
propertiesDependencies[parts[0].trim()] = new PropertyDependency(parts[1])
}
return propertiesDependencies
}
static List<PropertyDependency> parsePropertyDependencyList(String propertyValue) {
def propertiesDependencies = []
for (propDep in propertyValue.split(",")) {
propertiesDependencies.add(new PropertyDependency(propDep))
}
return propertiesDependencies
}
}
class ArtefactFlavorConf {
public PropertyDependency artefact = null
public ArrayList<String> flavors = null
/**
* @param slashColonSeparated : expected format is
* group1:id1:version1/flavor1|flavor2
* name1/flavor1|flavor2
*/
ArtefactFlavorConf(String slashSeparated) {
def parts = slashSeparated.split("/")
artefact = new PropertyDependency(parts[0].trim())
flavors = parts[1].trim().toLowerCase().split("\\|")
}
String toString() {
return "artefact=${artefact.toString()}, flavors=$flavors"
}
// Allow the definition to be only artefact name-based
boolean isArtefact(PropertyDependency dependency) {
return (artefact == dependency || isNamedArtefact(dependency.name))
}
boolean isNamedArtefact(String name) {
return (artefact.isNameOnly() && artefact.name == name) || iusForAllArtefacts()
}
boolean hasFlavor(String flavorName) {
return flavors.contains(flavorName.toLowerCase()) || isForAllFlavors()
}
private boolean isForAllArtefacts() {
return artefact.isNameOnly() && artefact.name == "*"
}
private boolean isForAllFlavors() {
return flavors.contains("*")
}
}
class ArtefactFlavorProperties {
public List<ArtefactFlavorConf> configuration = null
/**
* @param propertyValue : expected format is
* group1:id1:version1/flavor1:flavor2,group2:id2:version2/flavor1:flavor2
* name1/flavor1:flavor2,name2/flavor1:flavor2
*/
ArtefactFlavorProperties(String propertyValue) {
this()
for (propDep in propertyValue.split(",")) {
configuration.add(new ArtefactFlavorConf(propDep))
}
}
ArtefactFlavorProperties() {
configuration = []
}
boolean ignoreFlavorFor(ProjectDependency dependency, String flavor) {
if (flavor.empty) {
//No flavours in project, treat this as ignored
return true
}
for (ArtefactFlavorConf conf : configuration) {
if (conf.isNamedArtefact(dependency.name) && conf.hasFlavor(flavor)){
return true
}
}
return false
}
boolean ignoreFlavorFor(PropertyDependency dependency, String flavor){
if (flavor.empty) {
//No flavours in project, treat this as ignored
return true
}
for (ArtefactFlavorConf conf : configuration) {
if (conf.isArtefact(dependency) && conf.hasFlavor(flavor)){
return true
}
}
return false
}
@Override
String toString() {
StringBuilder sb = new StringBuilder()
for (ArtefactFlavorConf conf : configuration) {
sb.append(conf.toString())
sb.append(" <> ")
}
return sb.toString()
}
public static ArtefactFlavorProperties EMPTY = new ArtefactFlavorProperties()
}