# Gradle Bnd Plugin manifest issue

**URL:** <https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223>\
**Category:** GENERAL\
**Created:** [March 10, 2022, 9:47pm UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223 "2022-03-10T21:47:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![beccagaspard](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/beccagaspard/32/127_2.png) [@beccagaspard](https://bnd.discourse.group/u/beccagaspard)\
**Post date:** [March 10, 2022, 9:47pm UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223/1 "2022-03-10T21:47:07Z")

</div>

We are finally migrating from the deprecated OSGi Gradle plugin to the Bnd Gradle plugin to build our OSGi bundle. We have been using the deprecated plugin along with the `com.github.johnrengelman.shadow` plugin to create an “uber” jar with some flattened dependencies for our bundle and it has been working well.

After migrating we are running into an issue where the uber jar generated by the shadow plugin has a blank manifest (just one line `Manifest-Version: 1.0`) instead of the manifest generated by Bnd. The “regular” jar artifact from the `jar` task does have the correct manifest with all the macros executed, etc.

By default the shadow plugin [inherits](https://imperceptiblethoughts.com/shadow/configuration/#configuring-the-jar-manifest) from the manifest of the standard `jar` task - so I assume the Bnd plugin must be doing something a little different and that’s not getting set anymore. It’s possible to override the manifest used by shadow but I can’t figure out where to get the manifest object generated by Bnd.

I’m coming up empty googling this - has anyone run into a similar issue or have any ideas/suggestions on how to get this working? Thanks!!

Old build.gradle:

```auto
apply plugin: 'osgi'
apply plugin: 'com.github.johnrengelman.shadow'

jar {
  manifest {
    instruction 'Bundle-Activator', '${classes;IMPLEMENTS;org.osgi.framework.BundleActivator}'
    instruction 'Private-Package', 'com.mycompany.plugin'
    instruction 'Export-Package', ''
    instruction 'Import-Package', "$someHardcodedPackages,*;resolution:=optional;version=!"
  }
}

```

New build.gradle:

```auto
apply plugin: 'biz.aQute.bnd.builder'
apply plugin: 'com.github.johnrengelman.shadow'

jar {
 bundle {
   bnd('Bundle-Activator': '${classes;CONCRETE;IMPLEMENTS;org.osgi.framework.BundleActivator}',
       'Private-Package': 'com.mycompany.plugin',
       'Export-Package': '',
       'Import-Package': "$someHardcodedPackages,*;resolution:=optional;version=!")
}
shadowJar {
  manifest {
    inheritFrom somewhere.manifest //TODO how can I get this?
  }
}

```

---

<div class="post-metadata">

**Author:** ![chrisr3](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/chrisr3/32/58_2.png) [@chrisr3](https://bnd.discourse.group/u/chrisr3)\
**Post date:** [March 10, 2022, 9:55pm UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223/2 "2022-03-10T21:55:08Z")

</div>

Hi,

I think I’ve done something similar in the past, except that I created the `shadowJar` first, and then used that to create a bundle.

```auto
import aQute.bnd.gradle.Bundle

plugins {
    id 'com.github.johnrengelman.shadow'
    id 'biz.aQute.bnd.builder' apply false
}

tasks.named('jar', Jar) {
    enabled = false
}

def shadowJar = tasks.named('shadowJar', Jar) {
    archiveAppendix = 'shadow'
    archiveClassifier = ''
}

def bundle = tasks.register('bundle', Bundle) {
    from shadowJar.map { zipTree(it.archiveFile) }

    archiveClassifier = ''

    bundle {
        bnd shadowJar.flatMap { jar ->
            jar.archiveFile.map { file ->
                "-include: jar:${file.asFile.toURI()}!/META-INF/MANIFEST.MF"
            }
        }
    }
}

artifacts {
    archives bundle
}

```

Cheers,  
Chris

---

<div class="post-metadata">

**Author:** ![bjhargrave](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/bjhargrave/32/10_2.png) [@bjhargrave](https://bnd.discourse.group/u/bjhargrave)\
**Post date:** [March 11, 2022, 12:32am UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223/3 "2022-03-11T00:32:12Z")

</div>

Bnd does not use the Gradle [Manifest](https://docs.gradle.org/current/javadoc/org/gradle/api/java/archives/Manifest.html) object of the jar task for anything other than an input source. You can set manifest values through the jar’s manifest object which Bnd will use as input.

But the Bnd gradle plugin does not write the generated manifest back into the jar’s manifest object. It also does not supply the generated manifest in a form which could be used by [Manifest.from(Object…)](https://docs.gradle.org/current/javadoc/org/gradle/api/java/archives/Manifest.html#from-java.lang.Object...-). The generated manifest just exists as an entry in the output jar.

To be honest, I never considered any use cases like this. Perhaps you can open a Bnd issues describing what you need in terms of a change, and I can work on proposing some solution.

---

<div class="post-metadata">

**Author:** ![bjhargrave](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/bjhargrave/32/10_2.png) [@bjhargrave](https://bnd.discourse.group/u/bjhargrave)\
**Post date:** [March 11, 2022, 12:33am UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223/4 "2022-03-11T00:33:41Z")

</div>

This is actually a better order since Bnd will see the shadowed classes when generating the manifest.

---

<div class="post-metadata">

**Author:** ![beccagaspard](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/beccagaspard/32/127_2.png) [@beccagaspard](https://bnd.discourse.group/u/beccagaspard)\
**Post date:** [March 18, 2022, 6:53pm UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223/5 "2022-03-18T18:53:37Z")

</div>

@chrisr3 thanks for the response! For some reason I did not have luck w/ creating the shadowJar before the bundle - kept running into heap space gradle crashes. My bundle has a decent amount of dependencies. I’ve come up with this “hack” which seems to be working ok for now…

```auto
apply plugin: 'biz.aQute.bnd.builder'
apply plugin: 'com.github.johnrengelman.shadow'

jar {
 bundle {
   bnd('Bundle-Activator': '${classes;CONCRETE;IMPLEMENTS;org.osgi.framework.BundleActivator}',
       'Private-Package': 'com.mycompany.plugin',
       'Export-Package': '',
       'Import-Package': "$someHardcodedPackages,*;resolution:=optional;version=!")
}

task unpackJarManifest(type: Copy) {
  from ({zipTree(jar.outputs.files.singleFile)}) {
    include 'META-INF/MANIFEST.MF'
  }
  into layout.buildDirectory.dir('tmp')
}

shadowJar {
  manifest {
    inheritFrom layout.buildDirectory.file('tmp/META-INF/MANIFEST.MF')
  }
}

```

@bjhargrave I think it would be great if the bnd plugin wrote the generated manifest back to the jar object (similar to how deprecated osgi plugin worked), or if the bnd plugin provided a method to generate the manifest object to use in other tasks. I will work on creating a bnd issue to see if it is something you might consider. Thanks!

---

<div class="post-metadata">

**Author:** ![bjhargrave](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/bjhargrave/32/10_2.png) [@bjhargrave](https://bnd.discourse.group/u/bjhargrave)\
**Post date:** [May 1, 2022, 10:28am UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223/6 "2022-05-01T10:28:06Z")

</div>

For the forthcoming Bnd 6.3, I [updated BundleTaskExtension](https://github.com/bndtools/bnd/pull/5232/commits/9feea15b2570769a449e6a8b74edbe06ee2f2126) to make the generated manifest available from the task’s [manifest.effectiveManifest](https://docs.gradle.org/current/javadoc/org/gradle/api/java/archives/Manifest.html#getEffectiveManifest--) property.

---

<div class="post-metadata">

**Author:** ![beccagaspard](https://yyz2.discourse-cdn.com/free1/user_avatar/bnd.discourse.group/beccagaspard/32/127_2.png) [@beccagaspard](https://bnd.discourse.group/u/beccagaspard)\
**Post date:** [March 8, 2023, 7:11pm UTC](https://bnd.discourse.group/t/gradle-bnd-plugin-manifest-issue/223/7 "2023-03-08T19:11:21Z")

</div>

I have finally looped back to this a year later 🤪 Thanks so much @bjhargrave - the manifest property is now working for me!! In case it helps anyone else - here is my configuration that is working w/ bnd plugin version 6.4.0 and shadow plugin version 7.1.2.

```auto
apply plugin: 'biz.aQute.bnd.builder'
apply plugin: 'com.github.johnrengelman.shadow'

jar {
 bundle {
   bnd('Bundle-Activator': '${classes;CONCRETE;IMPLEMENTS;org.osgi.framework.BundleActivator}',
       'Private-Package': 'com.mycompany.plugin',
       'Export-Package': '',
       'Import-Package': "$someHardcodedPackages,*;resolution:=optional;version=!")
}

shadowJar {
  manifest {
    inheritFrom jar.manifest.effectiveManifest
  }
}

shadowJar.dependsOn jar

```
