forked from wttech/gradle-aem-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBundleCompose.kt
401 lines (333 loc) · 12.1 KB
/
BundleCompose.kt
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
package com.cognifide.gradle.aem.bundle.tasks
import aQute.bnd.gradle.BundleTaskConvention
import com.cognifide.gradle.aem.AemException
import com.cognifide.gradle.aem.AemExtension
import com.cognifide.gradle.aem.AemTask
import com.cognifide.gradle.aem.bundle.BundleException
import com.cognifide.gradle.aem.common.instance.service.osgi.Bundle
import com.cognifide.gradle.aem.common.utils.Formats
import com.fasterxml.jackson.annotation.JsonIgnore
import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.exception.ExceptionUtils
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.*
import org.gradle.api.tasks.bundling.Jar
import java.io.File
@Suppress("LargeClass", "TooManyFunctions")
open class BundleCompose : Jar(), AemTask {
@Internal
final override val aem = AemExtension.of(project)
@Internal
val javaConvention = project.convention.getPlugin(JavaPluginConvention::class.java)
@Internal
val bundleConvention = BundleTaskConvention(this).also { convention.plugins["bundle"] = it }
/**
* Shorthand for built OSGi bundle file.
*/
@get:JsonIgnore
@get:Internal
val composedFile: File
get() = archiveFile.get().asFile
/**
* Shorthand for directory of built OSGi bundle file.
*/
@get:JsonIgnore
@get:Internal
val composedDir: File
get() = composedFile.parentFile
/**
* Allows to configure BND tool specific options.
*
* @see <https://bnd.bndtools.org>
*/
fun bndTool(options: BundleTaskConvention.() -> Unit) {
bundleConvention.apply(options)
}
/**
* Content path for OSGi bundle jars being placed in CRX package.
*/
@Input
var installPath: String = aem.packageOptions.installPath
/**
* Suffix added to install path effectively allowing to install bundles only on specific instances.
*
* @see <https://helpx.adobe.com/experience-manager/6-4/sites/deploying/using/configure-runmodes.html#Definingadditionalbundlestobeinstalledforarunmode>
*/
@Input
@Optional
var installRunMode: String? = null
/**
* Determines if Vault workspace filter entry pointing directly to JAR file should be added automatically
* for built OSGi bundle.
*/
@Input
var vaultFilter: Boolean = true
/**
* Enable or disable support for auto-generating OSGi specific JAR manifest attributes
* like 'Bundle-SymbolicName', 'Export-Package' or AEM specific like 'Sling-Model-Packages'
* using 'javaPackage' property.
*/
@Input
var attributesConvention: Boolean = true
/**
* Determines package in which OSGi bundle being built contains its classes.
* Basing on that value, there will be:
*
* - generated OSGi specific manifest instructions like 'Bundle-SymbolicName', 'Export-Package'.
* - generated AEM specific manifest instructions like 'Sling-Model-Packages'.
* - performed additional component stability checks within 'packageDeploy' or separately using 'instanceAwait'.
*
* Default convention: '${project.group}.${project.name}'.
*
* Use empty string to disable automatic determining of that package and going further OSGi components checks.
*/
@Input
var javaPackage: String? = null
/**
* Determines how conflicts will be resolved when coincidental classes will be detected.
* Useful to combine Java sources with Kotlin, Scala etc.
*
* @see <http://bnd.bndtools.org/heads/private_package.html>
*/
@Input
var javaPackageOptions: String = "-split-package:=merge-first"
@Internal
@JsonIgnore
var importPackages: List<String> = listOf("*")
@Internal
@JsonIgnore
var exportPackages: List<String> = listOf()
@Internal
@JsonIgnore
var privatePackages: List<String> = listOf()
init {
applyArchiveDefaults()
applyBndToolDefaults()
}
private fun applyArchiveDefaults() {
destinationDirectory.set(AemTask.temporaryDir(aem.project, name))
archiveBaseName.set(aem.baseName)
from(javaConvention.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).output)
}
private fun applyBndToolDefaults() {
val instructionFile = File("${aem.project.file("bnd.bnd")}")
if (instructionFile.isFile) {
bundleConvention.setBndfile(instructionFile)
}
bundleConvention.bnd(mapOf(
"-fixupmessages.bundleActivator" to "${Bundle.ATTRIBUTE_ACTIVATOR} * is being imported *;is:=error"
))
}
override fun projectEvaluated() {
super.projectEvaluated()
ensureJavaPackage()
applyAttributesConvention()
combinePackageAttributes()
}
private fun ensureJavaPackage() {
if (javaPackage == null) {
if ("${aem.project.group}".isBlank()) {
throw AemException("${aem.project.displayName.capitalize()} must has property 'group' defined" +
" to determine bundle package default.")
}
javaPackage = Formats.normalizeSeparators("${aem.project.group}.${aem.project.name}", ".")
}
}
private fun combinePackageAttribute(name: String, pkgs: Iterable<String>) {
val combinedPkgs = mutableSetOf<String>().apply {
val existing = (attribute(name) ?: "")
.split(",")
.map { it.trim() }
addAll(pkgs)
addAll(existing)
}.filter { it.isNotBlank() }
if (combinedPkgs.isNotEmpty()) {
attribute(name, combinedPkgs.joinToString(","))
}
}
/**
* Generate attributes by convention using Gradle project metadata.
*/
private fun applyAttributesConvention() {
if (!attributesConvention) {
return
}
if (!hasAttribute(Bundle.ATTRIBUTE_NAME) && !aem.project.description.isNullOrBlank()) {
displayName = aem.project.description
}
if (!hasAttribute(Bundle.ATTRIBUTE_SYMBOLIC_NAME) && !javaPackage.isNullOrBlank()) {
symbolicName = javaPackage
}
if (!hasAttribute(Bundle.ATTRIBUTE_SLING_MODEL_PACKAGES) && !javaPackage.isNullOrBlank()) {
slingModelPackages = javaPackage
}
if (!hasAttribute(Bundle.ATTRIBUTE_ACTIVATOR) && !javaPackage.isNullOrBlank()) {
findActivator(javaPackage!!)?.let { activator = it }
}
}
/**
* Combine package attributes set explicitly with generated ones.
*/
private fun combinePackageAttributes() {
combinePackageAttribute(Bundle.ATTRIBUTE_IMPORT_PACKAGE, importPackages)
combinePackageAttribute(Bundle.ATTRIBUTE_PRIVATE_PACKAGE, privatePackages)
combinePackageAttribute(Bundle.ATTRIBUTE_EXPORT_PACKAGE, exportPackages.toMutableList().apply {
if (attributesConvention && !javaPackage.isNullOrBlank()) {
val javaPackageExported = if (javaPackageOptions.isNotBlank()) {
"$javaPackage.*;$javaPackageOptions"
} else {
"$javaPackage.*"
}
add(javaPackageExported)
}
})
}
private fun findActivator(pkg: String): String? {
for ((sourceSet, ext) in SOURCE_SETS) {
for (activatorClass in ACTIVATOR_CLASSES) {
if (aem.project.file("src/main/$sourceSet/${pkg.replace(".", "/")}/$activatorClass.$ext").exists()) {
return "$pkg.$activatorClass"
}
}
}
return null
}
@get:Input
var attributes: MutableMap<String, Any?>
get() = mutableMapOf<String, Any?>().apply { putAll(manifest.attributes) }
set(value) {
manifest.attributes(value)
}
fun attribute(name: String, value: String?) = manifest.attributes(mapOf(name to value))
fun attribute(name: String): String? = manifest.attributes[name] as String?
fun hasAttribute(name: String) = attributes.containsKey(name)
@get:Internal
@get:JsonIgnore
var displayName: String?
get() = attribute(Bundle.ATTRIBUTE_NAME)
set(value) {
attribute(Bundle.ATTRIBUTE_NAME, value)
}
@get:Internal
@get:JsonIgnore
var symbolicName: String?
get() = attribute(Bundle.ATTRIBUTE_SYMBOLIC_NAME)
set(value) {
attribute(Bundle.ATTRIBUTE_SYMBOLIC_NAME, value)
}
@get:Internal
@get:JsonIgnore
var activator: String?
get() = attribute(Bundle.ATTRIBUTE_ACTIVATOR)
set(value) {
attribute(Bundle.ATTRIBUTE_ACTIVATOR, value)
}
@get:Internal
@get:JsonIgnore
var category: String?
get() = attribute(Bundle.ATTRIBUTE_CATEGORY)
set(value) {
attribute(Bundle.ATTRIBUTE_CATEGORY, value)
}
@get:Internal
@get:JsonIgnore
var vendor: String?
get() = attribute(Bundle.ATTRIBUTE_VENDOR)
set(value) {
attribute(Bundle.ATTRIBUTE_VENDOR, value)
}
@get:Internal
@get:JsonIgnore
var license: String?
get() = attribute(Bundle.ATTRIBUTE_LICENSE)
set(value) {
attribute(Bundle.ATTRIBUTE_LICENSE, value)
}
@get:Internal
@get:JsonIgnore
var copyright: String?
get() = attribute(Bundle.ATTRIBUTE_COPYRIGHT)
set(value) {
attribute(Bundle.ATTRIBUTE_COPYRIGHT, value)
}
@get:Internal
@get:JsonIgnore
var docUrl: String?
get() = attribute(Bundle.ATTRIBUTE_DOC_URL)
set(value) {
attribute(Bundle.ATTRIBUTE_DOC_URL, value)
}
@get:Internal
@get:JsonIgnore
var developers: String?
get() = attribute(Bundle.ATTRIBUTE_DEVELOPERS)
set(value) {
attribute(Bundle.ATTRIBUTE_DEVELOPERS, value)
}
@get:Internal
@get:JsonIgnore
var contributors: String?
get() = attribute(Bundle.ATTRIBUTE_CONTRIBUTORS)
set(value) {
attribute(Bundle.ATTRIBUTE_CONTRIBUTORS, value)
}
@get:Internal
@get:JsonIgnore
var fragmentHost: String?
get() = attribute(Bundle.ATTRIBUTE_FRAGMENT_HOST)
set(value) {
attribute(Bundle.ATTRIBUTE_FRAGMENT_HOST, value)
}
@get:Internal
@get:JsonIgnore
var slingModelPackages: String?
get() = attribute(Bundle.ATTRIBUTE_SLING_MODEL_PACKAGES)
set(value) {
attribute(Bundle.ATTRIBUTE_SLING_MODEL_PACKAGES, value)
}
fun exportPackage(pkgs: Iterable<String>) {
exportPackages = exportPackages + pkgs
}
fun exportPackage(vararg pkgs: String) = exportPackage(pkgs.toList())
fun privatePackage(pkgs: Iterable<String>) {
privatePackages = privatePackages + pkgs
}
fun privatePackage(vararg pkgs: String) = privatePackage(pkgs.toList())
fun excludePackage(pkgs: Iterable<String>) {
importPackages = importPackages + pkgs.map { "!$it" }
}
fun excludePackage(vararg pkgs: String) = excludePackage(pkgs.toList())
fun importPackage(pkgs: Iterable<String>) {
importPackages = importPackages + pkgs
}
fun importPackage(vararg pkgs: String) = importPackage(pkgs.toList())
fun wildcardPackage(pkgs: Iterable<String>): List<String> {
return pkgs.map { StringUtils.appendIfMissing(it, ".*") }
}
fun wildcardPackage(vararg pkgs: String): List<String> {
return wildcardPackage(pkgs.toList())
}
@TaskAction
override fun copy() {
super.copy()
runBndTool()
}
@Suppress("TooGenericExceptionCaught")
private fun runBndTool() {
try {
bundleConvention.buildBundle()
} catch (e: Exception) {
aem.logger.error("BND tool error: https://bnd.bndtools.org", ExceptionUtils.getRootCause(e))
throw BundleException("OSGi bundle cannot be built properly.", e)
}
}
companion object {
const val NAME = "bundleCompose"
val ACTIVATOR_CLASSES = listOf("Activator", "BundleActivator")
val SOURCE_SETS = mapOf(
"java" to "java",
"kotlin" to "kt",
"scala" to "scala"
)
}
}