[GRADLE-904] Gradle creates invalid / non-standard conform jars? Created: 12/Apr/10  Updated: 04/Jan/13  Resolved: 24/Nov/10

Status: Resolved
Project: Gradle
Affects Version/s: 0.8, 0.9
Fix Version/s: 0.9-rc-1

Type: Bug
Reporter: Lars Heuer Assignee: Adam Murdoch
Resolution: Fixed Votes: 1

Attachments: Zip Archive gradle-osgi-ds.zip    


I have problems to use the OSGi Declarative Service (Karaf) together with a Gradle created jar.

I have a directory OSGI-INF in main/resources/ and an XML file which describes the OSGi service.

I added to build.gradle:

jar {

{ instruction 'Import-Package', 'org.osgi.framework', 'com.example.foo' instruction 'Export-Package', 'com.example.bar' instruction 'Service-Component', 'OSGI-INF/my-component.xml' }


but Apache Karaf is not able to discover the service automatically.
If I extract the Gradle-created jar and recreate the lib with the jar-tool
jar cvfm mylib.jar META-INF/MANIFEST.MF .
Karaf is able to discover the service. Note, that the new lib is created from the same input. I did not change anything, I just repackage it.

Does Gradle create non-standard jars?

Comment by Lars Heuer [ 16/Apr/10 ]

The attached project creates 2 jars (gradle build). One is created by Gradle, the other one by Ant (core-antgenerated). Drop one after the other into the karaf/deploy directory. You'll see that only the Ant-generated jar is detected by Apache Karaf <http://apache.mirror.clusters.cc/felix/apache-felix-karaf-1.4.0.zip>, and that the Gradle-gernerated jar produces no output at the console.
(You need to add <http://apache.mirror.digionline.de/felix/org.apache.felix.scr-1.4.0.jar> first to the deploy directory, the declarative service is not part of Karaf)
The Ant-generated jar has the same content of the Gradle-generated jar (it is the unpacked and re-packed Gradle jar)

Comment by Lars Heuer [ 07/May/10 ]

This issue is not OSGi Declarative Service specific.
Another project uses a BundleActivator with the same problem.
Example project: http://code.google.com/p/mio/source/browse/#svn/sandbox/gradle-osgi-activator
"core-1.0.0" works not with Karaf, "ant-core-1.0.0.jar" prints "Hello World" (aka works)

Comment by Bryan Keller [ 14/Jun/10 ]

I am encountering the same issue with Apache Felix. I believe the problem is that Gradle is simply zipping the files without paying attention to the order of the file headers. With jar, the first file header in the zip file should be the manifest. I believe this is why Felix is not recognizing the jar file as a bundle, because it doesn't locate the manifest where it thinks it should be. As a work around, in jar.doLast I am unzipping the jar Gradle created, and repackaging it using ant.jar.

Comment by Bryan Keller [ 14/Jun/10 ]

FYI, this problem also affects the war task, if you are using something like Pax Web to load wars as bundles. Basically both the Gradle jar and war tasks should use the equivalent Ant tasks, or be changed to behave the same way, rather than just zip the files to create the jar and war file.

Comment by Lars Heuer [ 14/Jun/10 ]

Good point, Bryan. Why not use the Ant tasks? Why does Gradle implement its own jar-packing mechanism?

Comment by Adam Murdoch [ 16/Jun/10 ]

Gradle uses the same Ant archive classes that the Ant tasks use. We don't use the tasks because they don't offer the flexibility which we want. However, the Ant tasks are just wrappers over the Ant archive classes, which we do use.

Comment by Adam Murdoch [ 16/Jun/10 ]

Moved the manifest to be the first entry in the JAR. Thanks for helping hunt this problem down.

Generated at Wed Jun 30 11:41:55 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.