8
8
import static aQute .bnd .gradle .BndUtils .unwrap ;
9
9
import static aQute .bnd .gradle .BndUtils .unwrapFile ;
10
10
import static aQute .bnd .gradle .BndUtils .unwrapFileOptional ;
11
- import static aQute .bnd .gradle .BndUtils .unwrapOptional ;
12
11
import static java .util .Collections .singletonList ;
13
12
import static java .util .Objects .requireNonNull ;
14
13
import static java .util .stream .Collectors .toList ;
29
28
import java .util .jar .Manifest ;
30
29
import java .util .zip .ZipFile ;
31
30
31
+ import aQute .bnd .exceptions .Exceptions ;
32
+ import aQute .bnd .osgi .Builder ;
33
+ import aQute .bnd .osgi .Constants ;
34
+ import aQute .bnd .osgi .Jar ;
35
+ import aQute .bnd .osgi .Processor ;
36
+ import aQute .bnd .stream .MapStream ;
37
+ import aQute .bnd .unmodifiable .Maps ;
38
+ import aQute .bnd .version .MavenVersion ;
39
+ import aQute .lib .io .IO ;
40
+ import aQute .lib .strings .Strings ;
41
+ import aQute .lib .utf8properties .UTF8Properties ;
32
42
import org .gradle .api .Action ;
33
43
import org .gradle .api .GradleException ;
34
44
import org .gradle .api .Project ;
52
62
import org .gradle .api .tasks .TaskInputFilePropertyBuilder ;
53
63
import org .gradle .work .NormalizeLineEndings ;
54
64
55
- import aQute .bnd .exceptions .Exceptions ;
56
- import aQute .bnd .osgi .Builder ;
57
- import aQute .bnd .osgi .Constants ;
58
- import aQute .bnd .osgi .Jar ;
59
- import aQute .bnd .osgi .Processor ;
60
- import aQute .bnd .stream .MapStream ;
61
- import aQute .bnd .unmodifiable .Maps ;
62
- import aQute .bnd .version .MavenVersion ;
63
- import aQute .lib .io .IO ;
64
- import aQute .lib .strings .Strings ;
65
- import aQute .lib .utf8properties .UTF8Properties ;
66
-
67
65
/**
68
66
* BundleTaskExtension for Gradle.
69
67
* <p>
@@ -96,6 +94,8 @@ public class BundleTaskExtension {
96
94
private final ConfigurableFileCollection classpath ;
97
95
private final Provider <String > bnd ;
98
96
private final MapProperty <String , Object > properties ;
97
+ private final Provider <String > defaultBundleSymbolicName ;
98
+ private final Provider <String > defaultBundleVersion ;
99
99
100
100
/**
101
101
* The bndfile property.
@@ -131,7 +131,7 @@ public ConfigurableFileCollection getClasspath() {
131
131
* If the bndfile property points an existing file, this property is
132
132
* ignored. Otherwise, the bnd instructions in this property will be used.
133
133
*
134
- * @return The property for the bnd instructions.
134
+ * @return The provider for the bnd instructions.
135
135
*/
136
136
@ Input
137
137
@ org .gradle .api .tasks .Optional
@@ -211,6 +211,13 @@ public BundleTaskExtension(org.gradle.api.tasks.bundling.Jar task) {
211
211
classpath (mainSourceSet .getCompileClasspath ());
212
212
properties = objects .mapProperty (String .class , Object .class )
213
213
.convention (Maps .of ("project" , "__convention__" ));
214
+ defaultBundleSymbolicName = task .getArchiveBaseName ()
215
+ .zip (task .getArchiveClassifier (), (baseName , classifier ) -> classifier .isEmpty () ? baseName : baseName + "-" + classifier );
216
+ defaultBundleVersion = task .getArchiveVersion ()
217
+ .orElse ("0" )
218
+ .map (version -> MavenVersion .parseMavenString (version )
219
+ .getOSGiVersion ()
220
+ .toString ());
214
221
// need to programmatically add to inputs since @InputFiles in a
215
222
// extension is not processed
216
223
task .getInputs ()
@@ -229,6 +236,10 @@ public BundleTaskExtension(org.gradle.api.tasks.bundling.Jar task) {
229
236
.property ("bnd" , getBnd ());
230
237
task .getInputs ()
231
238
.property ("properties" , getProperties ());
239
+ task .getInputs ()
240
+ .property ("default Bundle-SymbolicName" , getDefaultBundleSymbolicName ());
241
+ task .getInputs ()
242
+ .property ("default Bundle-Version" , getDefaultBundleVersion ());
232
243
}
233
244
234
245
/**
@@ -331,6 +342,30 @@ File getBuildFile() {
331
342
return buildFile ;
332
343
}
333
344
345
+ /**
346
+ * The default value for the Bundle-SymbolicName manifest header.
347
+ * <p>
348
+ * If the Bundle-SymbolicName manifest header is not set in the bnd instructions,
349
+ * the value of this provider will be used.
350
+ *
351
+ * @return The provider for the default Bundle-SymbolicName manifest header.
352
+ */
353
+ Provider <String > getDefaultBundleSymbolicName () {
354
+ return defaultBundleSymbolicName ;
355
+ }
356
+
357
+ /**
358
+ * The default value for the Bundle-Version manifest header.
359
+ * <p>
360
+ * If the Bundle-Version manifest header is not set in the bnd instructions,
361
+ * the value of this provider will be used.
362
+ *
363
+ * @return The provider for the default Bundle-Version manifest header.
364
+ */
365
+ Provider <String > getDefaultBundleVersion () {
366
+ return defaultBundleVersion ;
367
+ }
368
+
334
369
ProjectLayout getLayout () {
335
370
return layout ;
336
371
}
@@ -374,7 +409,7 @@ public void execute(Task t) {
374
409
.ofNullable (manifest .getEffectiveManifest ()
375
410
.getAttributes ())
376
411
.filterKey (key -> !Objects .equals (key , "Manifest-Version" ))
377
- .mapValue (Object :: toString )
412
+ .mapValue (this :: unwrapAttributeValue )
378
413
.collect (MapStream .toMap ((k1 , k2 ) -> {
379
414
throw new IllegalStateException ("Duplicate key " + k1 );
380
415
}, UTF8Properties ::new )));
@@ -412,9 +447,6 @@ public void execute(Task t) {
412
447
}
413
448
File archiveFile = unwrapFile (getTask ().getArchiveFile ());
414
449
String archiveFileName = unwrap (getTask ().getArchiveFileName ());
415
- String archiveBaseName = unwrap (getTask ().getArchiveBaseName ());
416
- String archiveClassifier = unwrap (getTask ().getArchiveClassifier ());
417
- String archiveVersion = unwrapOptional (getTask ().getArchiveVersion ()).orElse (null );
418
450
419
451
// Include entire contents of Jar task generated jar
420
452
// (except the manifest)
@@ -472,22 +504,18 @@ public void execute(Task t) {
472
504
.toArray (new File [0 ]));
473
505
getTask ().getLogger ()
474
506
.debug ("builder sourcepath: {}" , builder .getSourcePath ());
475
- // set bundle symbolic name from tasks's archiveBaseName
476
- // property if necessary
507
+ // set bundle symbolic name if necessary
477
508
String bundleSymbolicName = builder .getProperty (Constants .BUNDLE_SYMBOLICNAME );
478
509
if (isEmpty (bundleSymbolicName )) {
479
- bundleSymbolicName = archiveClassifier .isEmpty () ? archiveBaseName
480
- : archiveBaseName + "-" + archiveClassifier ;
510
+ bundleSymbolicName = unwrap (getDefaultBundleSymbolicName ());
481
511
builder .setProperty (Constants .BUNDLE_SYMBOLICNAME , bundleSymbolicName );
482
512
}
483
513
484
- // set bundle version from task's archiveVersion if
485
- // necessary
514
+ // set bundle version if necessary
486
515
String bundleVersion = builder .getProperty (Constants .BUNDLE_VERSION );
487
516
if (isEmpty (bundleVersion )) {
488
- builder .setProperty (Constants .BUNDLE_VERSION , MavenVersion .parseMavenString (archiveVersion )
489
- .getOSGiVersion ()
490
- .toString ());
517
+ bundleVersion = unwrap (getDefaultBundleVersion ());
518
+ builder .setProperty (Constants .BUNDLE_VERSION , bundleVersion );
491
519
}
492
520
493
521
getTask ().getLogger ()
@@ -532,6 +560,16 @@ private org.gradle.api.java.archives.Manifest mergeManifest(Manifest builtManife
532
560
return mergeManifest ;
533
561
}
534
562
563
+ private String unwrapAttributeValue (Object value ) {
564
+ while (value instanceof Provider ) {
565
+ value = ((Provider <?>) value ).getOrNull ();
566
+ }
567
+ if (value == null ) {
568
+ return null ;
569
+ }
570
+ return value .toString ();
571
+ }
572
+
535
573
private void failTask (String msg , File archiveFile ) {
536
574
IO .delete (archiveFile );
537
575
throw new GradleException (msg );
0 commit comments