-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOsgiBundleModule.scala
424 lines (362 loc) · 13.9 KB
/
OsgiBundleModule.scala
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
package de.tobiasroeser.mill.osgi
import java.io.FileOutputStream
import scala.collection.JavaConverters._
import scala.util.Try
import aQute.bnd.osgi.{Builder, Constants, Jar}
import de.tobiasroeser.mill.osgi.internal.{BuildInfo, copy => icopy, unpack => iunpack}
import mill._
import mill.define.{Sources, Task}
import mill.api.PathRef
import mill.modules.Jvm
import mill.scalalib.{JavaModule, PublishModule}
import os.Path
trait OsgiBundleModule extends OsgiBundleModulePlatform {
import OsgiBundleModule._
/**
* The build mode.
* Defaults to [[BuildMode.ReplaceJarTarget]], in which the [[jar]] target delegates to [[osgiBundle]].
* You can also select [[BuildMode.CalculateManifest]], in which only the manifest entries will be generated with
* bnd tool, but the JAR is creates by the regular (derived) [[jar]] target.
*
* @return
*/
def osgiBuildMode: BuildMode = BuildMode.ReplaceJarTarget
/**
* The transitive version of `localClasspath`.
* This overrides [[JavaModule.transitiveLocalClasspath]], but uses the final
* JAR files instead of just the classes directories where possible.
* This is needed, as only the final JARs contain proper OSGi manifest entries.
*/
override def transitiveLocalClasspath: T[Agg[PathRef]] = osgiBuildMode match {
case BuildMode.ReplaceJarTarget => T {
T.traverse(recursiveModuleDeps) { m =>
T.task {
Agg(m.jar())
}
}().flatten
}
case BuildMode.CalculateManifest => super.transitiveLocalClasspath
}
override def localClasspath: T[Seq[PathRef]] = osgiBuildMode match {
case BuildMode.ReplaceJarTarget => super.localClasspath
case BuildMode.CalculateManifest => T {
Seq(osgiManifest()) ++ super.localClasspath()
}
}
/**
* Build the final bundle.
* Overrides the [[JavaModule#jar]].
* If [[osgiBuildMode]] is [[BuildMode.ReplaceJarTarget]] then this links to [[osgiBundle]] instead.
*/
override def jar: T[PathRef] = osgiBuildMode match {
case BuildMode.ReplaceJarTarget => T { osgiBundle() }
case BuildMode.CalculateManifest => super.jar
}
/**
* The bundle symbolic name used to initialize [[osgiHeaders]].
* If the module is a [[PublishModule]], it calculated the bundle symbolic name
* from [[PublishModule.artifactMetadata]]
*/
def bundleSymbolicName: T[String] = this match {
case pm: PublishModule => T {
calcBundleSymbolicName(pm.pomSettings().organization, artifactId())
}
case _ =>
artifactId
}
/**
* The bundle version used to initialize [[osgiHeaders]].
* If the module is a [[PublishModule]], it uses the [[PublishModule.publishVersion]]
*/
def bundleVersion: T[String] = this match {
case pm: PublishModule => T {
pm.publishVersion()
}
case _ => "0.0.0"
}
/**
* Instruct bnd to create a reproducible bundle file.
*/
def reproducibleBundle: T[Boolean] = T {
true
}
/**
* Embed these JAR files and also add them to the bundle classpath.
*/
def embeddedJars: T[Seq[PathRef]] = T {
Seq[PathRef]()
}
/**
* Embed the content of the given JAR files into the bundle.
*/
def explodedJars: T[Seq[PathRef]] = T {
Seq[PathRef]()
}
def osgiHeaders: T[OsgiHeaders] = {
def withDefaults: OsgiHeaders => OsgiHeaders = h =>
h.copy(
`Import-Package` = Seq("*")
)
this match {
case pm: PublishModule => T {
val pom = pm.pomSettings()
withDefaults(OsgiHeaders(
`Bundle-SymbolicName` = bundleSymbolicName(),
`Bundle-Version` = Option(bundleVersion()),
`Bundle-License` = pom.licenses.map(l => l.url.toString),
`Bundle-Vendor` = Option(pom.organization),
`Bundle-Description` = Option(pom.description)
))
}
case _ => T {
withDefaults(OsgiHeaders(
`Bundle-SymbolicName` = bundleSymbolicName(),
`Bundle-Version` = Option(bundleVersion())
))
}
}
}
/**
* Iff `true` include sources in the final bundle under `OSGI-OPT/src`.
*/
def includeSources: T[Boolean] = T {
false
}
/**
* Resources to include into the final bundle.
* Defaults to include [[JavaModule.resources()]].
*/
def includeResource: T[Seq[String]] = T {
// default: add contents of resources to final bundle
resources()
// only take non-empty directories to avoid bnd warning/error
.filter(p => p.path.toIO.exists()) // && Option(p.path.toIO.list()).map(!_.isEmpty).getOrElse(false))
// add to the root of the JAR
.map(dir => dir.path.toIO.getAbsolutePath())
}
/**
* Exports the given packages but does not try to include them from the class path.
* The packages should be loaded with alternative means.
*/
def exportContents: T[Seq[String]] = T {
Seq[String]()
}
// TODO: do we want support default Mill Jar headers?
/**
* Additional headers to add to the bundle manifest.
* Warning: All headers added here will override their previous value,
* hence, be careful to not add standard OSGi headers here, but via [[osgiHeaders]].
*/
def additionalHeaders: T[Map[String, String]] = T {
Map[String, String]()
}
/**
* Build the OSGi Bundle by using the bnd tool.
*/
def osgiBundle: T[PathRef] = T {
val jar = osgiBundleTask()
val outputPath = T.ctx().dest / s"${bundleSymbolicName()}-${bundleVersion()}.jar"
jar.write(outputPath.toIO)
PathRef(outputPath)
}
/**
* Generated the OSGi Bundle manifest by using the bnd tool.
* @return The path containing `META-INF/MANIFEST.MF`, can be used as classpath too.
*/
def osgiManifest: T[PathRef] = T {
val jar = osgiBundleTask()
val manifest = jar.getManifest()
val outputFile = T.dest / "META-INF" / "MANIFEST.MF"
os.makeDir.all(outputFile / os.up)
val stream = new FileOutputStream(outputFile.toIO)
try {
manifest.write(stream)
} finally {
stream.close()
}
PathRef(T.dest)
}
/**
* Creates a manifest representation which can be modified or replaced.
* The default implementation generates OSGi manifest entries from compiled classes with bnd tool and
* additionally adds a `Main-Class`, if defined in [[mainClass]].
*/
override def manifest: T[Jvm.JarManifest] = T {
// Mill defined
val pre = super.manifest()
// bnd calculated
val manifest = osgiBundleTask().getManifest()
def entryAsStringPair(entry: java.util.Map.Entry[Object, Object]): (String, String) = {
entry.getKey().toString() -> Option(entry.getValue()).map(_.toString()).getOrElse("")
}
Jvm.JarManifest(
main = pre.main ++ manifest.getMainAttributes.entrySet().asScala.map(entryAsStringPair).toMap,
groups = pre.groups ++ manifest.getEntries().asScala.map(e =>
e._1 -> e._2.entrySet().asScala.map(entryAsStringPair).toMap
)
)
}
override def resources: Sources = osgiBuildMode match {
case BuildMode.ReplaceJarTarget => super.resources
case BuildMode.CalculateManifest => T.sources {
super.resources() ++ {
val dest = T.dest
if (includeSources()) {
sources().map(_.path).filter(os.exists).map { path =>
icopy(path, dest / "OSGI-OPT" / "src", createFolders = true, mergeFolders = true)
}
}
embeddedJars().foreach { jar =>
icopy(jar.path, dest / jar.path.last)
}
explodedJars().foreach { jar =>
iunpack.zip(jar.path, dest)
}
Seq(PathRef(dest))
}
}
}
/**
* Build the OSGi Bundle.
*/
def osgiBundleTask: Task[Jar] = osgiBuildMode match {
case BuildMode.ReplaceJarTarget => osgiBundleTask(localClasspath, calcPrivatePackage = true)
case BuildMode.CalculateManifest => osgiBundleTask(super.localClasspath, calcPrivatePackage = false)
}
def osgiBundleTask(localClasspath: Task[Seq[PathRef]], calcPrivatePackage: Boolean): Task[Jar] = T.task {
val log = T.ctx().log
val currentRunningMillVersion = T.ctx().env.get("MILL_VERSION")
if (!checkMillVersion(currentRunningMillVersion)) {
log.error(
s"Your used mill version is most probably too old. In case of errors use (at least) mill version ${BuildInfo.millVersion}."
)
}
val builder = new Builder()
if (reproducibleBundle()) {
builder.setProperty(Constants.REPRODUCIBLE, "true")
}
// TODO: check if all dependencies have proper Manifests (are bundled as jars instead of class folders)
val bndClasspath = (compileClasspath() ++ localClasspath()).toList.map(p => p.path.toIO).filter(_.exists()).asJava
builder.setClasspath(bndClasspath)
if (calcPrivatePackage) {
// We need to make sure we package all classfiles, event if they are not exported
// Unfortunately, this doesn't work very well for to top-level (no-name) package
// and also is known to include to much resource files (from dependencies) into the top-level package.
// That's why the BuildMode.CalcuateManifest is expected to produce better jars
// TODO: scan classes directory and auto-add all dirs as private package
val classesPath = compile().classes.path
val ps: Seq[Path] = if (!os.exists(classesPath)) Seq() else os.walk(classesPath)
val packages = ps
.filter(_.toIO.isFile())
.flatMap { pFull =>
val p = pFull.relativeTo(classesPath)
if (p.segments.size > 1) {
Seq((p / os.up).segments.mkString("."))
} else {
// Find way to include top-level package
Seq(".")
}
}
.distinct
if (!packages.isEmpty) {
builder.setProperty(Constants.PRIVATE_PACKAGE, packages.mkString(","))
}
}
// // Special case, files in top level package
// // Those can't be exported, but we include them
// val rootPackageFiles: LsSeq = ammonite.ops.ls ! (classesPath)
// if (!rootPackageFiles.filter(_.isFile).isEmpty) {
// println("Found files in top level package")
// // mergeSeqProps(builder, Constants.INCLUDERESOURCE, Seq(classesPath.toIO.getAbsolutePath() + ";recursive:=false"))
// mergeSeqProps(builder, Constants.PRIVATE_PACKAGE, Seq(".;-split-package:=last"))
// }
allSources().foreach { dir =>
builder.setProperty(Constants.SOURCEPATH, dir.path.toIO.getAbsolutePath())
}
if (includeSources()) {
mergeSeqProps(
builder,
Constants.INCLUDERESOURCE,
allSources().filter(_.path.toIO.exists()).map(s => "OSGI-OPT/src=" + s.path.toIO.getAbsolutePath()).toList
)
}
// TODO: Some validation that should at least war
// * Fragment and activator at the same time
// * Activator in exported package
// * Packages not part of export or private
// TODO: handle special props with defaults
// handle included resources
mergeSeqProps(builder, Constants.INCLUDERESOURCE, includeResource())
// handle embedded Jars
embeddedJars().foreach { jar =>
mergeSeqProps(builder, Constants.INCLUDERESOURCE, Seq(jar.path.toIO.getAbsolutePath()))
}
// handle exploded Jars
explodedJars().foreach { jar =>
mergeSeqProps(builder, Constants.INCLUDERESOURCE, Seq("@" + jar.path.toIO.getAbsolutePath()))
}
mergeSeqProps(builder, Constants.EXPORT_CONTENTS, exportContents())
builder.addProperties(osgiHeaders().toProperties)
builder.addProperties(additionalHeaders().asJava)
// println("Props:" + builder.getProperties().asScala.toList.map {
// case (k, v) =>
// if (v.indexOf(",") > 0) {
// s"${k}:\n ${v.split("[,]").mkString(",\n ")}"
// } else {
// s"${k}: ${v}"
// }
// }.mkString("\n "))
val jar = builder.build()
builder.getErrors().asScala.foreach(msg => log.error("bnd error: " + msg))
builder.getWarnings().asScala.foreach(msg => log.error("bnd warning: " + msg))
jar
}
}
object OsgiBundleModule {
def calcBundleSymbolicName(group: String, artifact: String): String = {
val groupParts = group.split("[.]")
val nameParts = artifact.split("[.]").flatMap(_.split("[-]"))
val parts =
if (nameParts.startsWith(groupParts)) nameParts
else (groupParts.lastOption, nameParts.headOption) match {
case (Some(last), Some(head)) if last == head => groupParts ++ nameParts.tail
case (Some(last), Some(head)) if head.startsWith(last) => groupParts.take(groupParts.size - 1) ++ nameParts
case _ => groupParts ++ nameParts
}
parts.mkString(".")
}
def mergeSeqProps(builder: Builder, key: String, value: Seq[String]): Unit = {
val existing = builder.getProperty(key) match {
case null => Seq()
case p => Seq(p)
}
builder.setProperty(key, (existing ++ value).mkString(","))
}
protected[osgi] def checkMillVersion(millVersion: Option[String]): Boolean =
checkMillVersion(BuildInfo.millVersion, millVersion)
protected[osgi] def checkMillVersion(buildVersion: String, millVersion: Option[String]): Boolean = millVersion match {
case Some(v) =>
/** Extract the major, minor and micro version parts of the given version string. */
def parseVersion(version: String): Try[Array[Int]] = Try {
version
.split("[-]", 2)(0)
.split("[.]", 4)
.take(3)
.map(_.toInt)
}
val buildMillVersion = parseVersion(buildVersion).getOrElse(Array(0, 0, 0))
val runMillVersion = parseVersion(v).getOrElse(Array(999, 999, 999))
(runMillVersion(0) > buildMillVersion(0)) ||
(runMillVersion(0) == buildMillVersion(0) && runMillVersion(1) > buildMillVersion(1)) ||
(runMillVersion(0) == buildMillVersion(0) && runMillVersion(1) == buildMillVersion(1) &&
runMillVersion(2) >= buildMillVersion(2))
case _ =>
// ignore
true
}
sealed trait BuildMode
object BuildMode {
object ReplaceJarTarget extends BuildMode
object CalculateManifest extends BuildMode
}
}