Skip to content

Commit 017248d

Browse files
authored
Merge pull request #5280 from bjhargrave/issues/5279
gradle: Make default Bundle-SymbolicName and Bundle-Version inputs
2 parents b09493d + 8874576 commit 017248d

File tree

4 files changed

+76
-35
lines changed

4 files changed

+76
-35
lines changed

Diff for: gradle-plugins/biz.aQute.bnd.gradle/src/main/java/aQute/bnd/gradle/BundleTaskExtension.java

+65-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import static aQute.bnd.gradle.BndUtils.unwrap;
99
import static aQute.bnd.gradle.BndUtils.unwrapFile;
1010
import static aQute.bnd.gradle.BndUtils.unwrapFileOptional;
11-
import static aQute.bnd.gradle.BndUtils.unwrapOptional;
1211
import static java.util.Collections.singletonList;
1312
import static java.util.Objects.requireNonNull;
1413
import static java.util.stream.Collectors.toList;
@@ -29,6 +28,17 @@
2928
import java.util.jar.Manifest;
3029
import java.util.zip.ZipFile;
3130

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;
3242
import org.gradle.api.Action;
3343
import org.gradle.api.GradleException;
3444
import org.gradle.api.Project;
@@ -52,18 +62,6 @@
5262
import org.gradle.api.tasks.TaskInputFilePropertyBuilder;
5363
import org.gradle.work.NormalizeLineEndings;
5464

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-
6765
/**
6866
* BundleTaskExtension for Gradle.
6967
* <p>
@@ -96,6 +94,8 @@ public class BundleTaskExtension {
9694
private final ConfigurableFileCollection classpath;
9795
private final Provider<String> bnd;
9896
private final MapProperty<String, Object> properties;
97+
private final Provider<String> defaultBundleSymbolicName;
98+
private final Provider<String> defaultBundleVersion;
9999

100100
/**
101101
* The bndfile property.
@@ -131,7 +131,7 @@ public ConfigurableFileCollection getClasspath() {
131131
* If the bndfile property points an existing file, this property is
132132
* ignored. Otherwise, the bnd instructions in this property will be used.
133133
*
134-
* @return The property for the bnd instructions.
134+
* @return The provider for the bnd instructions.
135135
*/
136136
@Input
137137
@org.gradle.api.tasks.Optional
@@ -211,6 +211,13 @@ public BundleTaskExtension(org.gradle.api.tasks.bundling.Jar task) {
211211
classpath(mainSourceSet.getCompileClasspath());
212212
properties = objects.mapProperty(String.class, Object.class)
213213
.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());
214221
// need to programmatically add to inputs since @InputFiles in a
215222
// extension is not processed
216223
task.getInputs()
@@ -229,6 +236,10 @@ public BundleTaskExtension(org.gradle.api.tasks.bundling.Jar task) {
229236
.property("bnd", getBnd());
230237
task.getInputs()
231238
.property("properties", getProperties());
239+
task.getInputs()
240+
.property("default Bundle-SymbolicName", getDefaultBundleSymbolicName());
241+
task.getInputs()
242+
.property("default Bundle-Version", getDefaultBundleVersion());
232243
}
233244

234245
/**
@@ -331,6 +342,30 @@ File getBuildFile() {
331342
return buildFile;
332343
}
333344

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+
334369
ProjectLayout getLayout() {
335370
return layout;
336371
}
@@ -374,7 +409,7 @@ public void execute(Task t) {
374409
.ofNullable(manifest.getEffectiveManifest()
375410
.getAttributes())
376411
.filterKey(key -> !Objects.equals(key, "Manifest-Version"))
377-
.mapValue(Object::toString)
412+
.mapValue(this::unwrapAttributeValue)
378413
.collect(MapStream.toMap((k1, k2) -> {
379414
throw new IllegalStateException("Duplicate key " + k1);
380415
}, UTF8Properties::new)));
@@ -412,9 +447,6 @@ public void execute(Task t) {
412447
}
413448
File archiveFile = unwrapFile(getTask().getArchiveFile());
414449
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);
418450

419451
// Include entire contents of Jar task generated jar
420452
// (except the manifest)
@@ -472,22 +504,18 @@ public void execute(Task t) {
472504
.toArray(new File[0]));
473505
getTask().getLogger()
474506
.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
477508
String bundleSymbolicName = builder.getProperty(Constants.BUNDLE_SYMBOLICNAME);
478509
if (isEmpty(bundleSymbolicName)) {
479-
bundleSymbolicName = archiveClassifier.isEmpty() ? archiveBaseName
480-
: archiveBaseName + "-" + archiveClassifier;
510+
bundleSymbolicName = unwrap(getDefaultBundleSymbolicName());
481511
builder.setProperty(Constants.BUNDLE_SYMBOLICNAME, bundleSymbolicName);
482512
}
483513

484-
// set bundle version from task's archiveVersion if
485-
// necessary
514+
// set bundle version if necessary
486515
String bundleVersion = builder.getProperty(Constants.BUNDLE_VERSION);
487516
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);
491519
}
492520

493521
getTask().getLogger()
@@ -532,6 +560,16 @@ private org.gradle.api.java.archives.Manifest mergeManifest(Manifest builtManife
532560
return mergeManifest;
533561
}
534562

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+
535573
private void failTask(String msg, File archiveFile) {
536574
IO.delete(archiveFile);
537575
throw new GradleException(msg);

Diff for: gradle-plugins/biz.aQute.bnd.gradle/src/test/groovy/aQute/bnd/gradle/TestBundlePlugin.groovy

+4-2
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,19 @@ class TestBundlePlugin extends Specification {
147147
result.task(":bundle").outcome == SUCCESS
148148
result.task(":jar").outcome == SUCCESS
149149

150-
File jartask_result = new File(testProjectBuildDir, "libs/${testProject}-1.0.0.jar")
150+
File jartask_result = new File(testProjectBuildDir, "libs/${testProject}.jar")
151151
jartask_result.isFile()
152152
JarFile jartask_jar = new JarFile(jartask_result)
153153
Attributes jartask_manifest = jartask_jar.getManifest().getMainAttributes()
154154

155-
File bundletask_bundle = new File(testProjectBuildDir, "libs/${testProject}-1.0.0-bundle.jar")
155+
File bundletask_bundle = new File(testProjectBuildDir, "libs/${testProject}-bundle.jar")
156156
bundletask_bundle.isFile()
157157
JarFile bundletask_jar = new JarFile(bundletask_bundle)
158158
Attributes bundletask_manifest = bundletask_jar.getManifest().getMainAttributes()
159159

160160
jartask_manifest.getValue("XX-Signed") == "true"
161+
bundletask_manifest.getValue("Bundle-SymbolicName") == "${testProject}-bundle"
162+
bundletask_manifest.getValue("Bundle-Version") == "0.0.0"
161163
bundletask_manifest.getValue("XX-Signed") == "true"
162164
bundletask_manifest.getValue("YY-Sealed") == "true"
163165
bundletask_manifest.getValue("ZZ-Delivered") == "true"

Diff for: gradle-plugins/biz.aQute.bnd.gradle/testresources/builderplugin1/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies {
2525
jar {
2626
ext.taskprop = 'prop.task'
2727
manifest {
28-
attributes('Implementation-Title': project.archivesBaseName,
28+
attributes('Implementation-Title': providers.provider({ -> project.archivesBaseName}),
2929
'Implementation-Version': project.version,
3030
'-includeresource': '{${.}/bar.txt}',
3131
'-include': '${.}/other.bnd',

Diff for: gradle-plugins/biz.aQute.bnd.gradle/testresources/builderplugin2/build.gradle

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ plugins {
1010
}
1111

1212
group = 'test.bnd.gradle'
13-
version = '1.0.0'
1413

1514
repositories {
1615
mavenCentral()
@@ -19,7 +18,6 @@ repositories {
1918
ext {
2019
extraInstructions = provider {
2120
'''\
22-
YY-Sealed: true
2321
ZZ-Delivered: true
2422
'''
2523
}
@@ -28,7 +26,7 @@ ZZ-Delivered: true
2826
// Not a bundle.
2927
def jarTask = tasks.named('jar', Jar) {
3028
manifest {
31-
attributes('XX-Signed': true)
29+
attributes('XX-Signed': provider({true}))
3230
}
3331
}
3432

@@ -37,16 +35,19 @@ task bundle(type: Bundle) {
3735
group = 'build'
3836
from jarTask.map { zipTree(it.archiveFile) }
3937
archiveClassifier = 'bundle'
38+
manifest {
39+
attributes('YY-Sealed': provider({true}))
40+
}
4041

4142
bundle {
4243
bnd = jarTask.flatMap { jar ->
4344
jar.archiveFile.map { file ->
4445
"-include: jar:${file.asFile.toURI()}!/META-INF/MANIFEST.MF"
4546
}
4647
}
47-
48+
4849
bnd extraInstructions
49-
50+
5051
bnd '''
5152
Bundle-Name: ${project.group}:${task.archiveBaseName}-${task.archiveClassifier}
5253
'''

0 commit comments

Comments
 (0)