mrcool
mrcool

Reputation: 55

Bundlor not working properly with Java 8 lambdas

I am using the plugin org.eclipse.virgo.bundlor to create OSGi manifest files from a template. When I use lambdas, the plugin does not work properly. Can you suggest a change to my maven file or an alternative to bundlor that will allow me to use lambdas?

Here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>lamba</groupId>
  <artifactId>lamba</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>lamba</name>
  <properties>
     <java-source-version>1.8</java-source-version>
    <java-target-version>1.8</java-target-version>
  </properties>
  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <plugin>
        <groupId>org.eclipse.virgo.bundlor</groupId>
        <artifactId>org.eclipse.virgo.bundlor.maven</artifactId>
        <version>1.1.2.RELEASE</version>
        <dependencies>
          <dependency>
            <groupId>org.eclipse.virgo.bundlor</groupId>
            <artifactId>org.eclipse.virgo.bundlor</artifactId>
            <version>1.1.2.RELEASE</version>
          </dependency>
          <dependency>
            <groupId>org.eclipse.virgo.bundlor</groupId>
            <artifactId>org.eclipse.virgo.bundlor.blint</artifactId>
            <version>1.1.2.RELEASE</version>
          </dependency>

        </dependencies>


        <executions>
          <execution>
            <id>${project.artifactId}-bundle-manifest</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>bundlor</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>${java-source-version}</source>
            <target>${java-target-version}</target>
            <skip>false</skip>
          </configuration>
        </plugin>
    </plugins>
  </build>
</project>

The code that causes the problem is simply this:

package lamba.internal;

import java.util.function.Supplier;

/**
 *
 */
public class HiImpl {
  public Supplier<String> p=()->"hi";

}

My error is this:

[INFO] --- org.eclipse.virgo.bundlor.maven:1.1.2.RELEASE:bundlor (lamba-bundle-manifest) @ lamba ---
java.lang.ArrayIndexOutOfBoundsException: 28712
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.eclipse.virgo.bundlor.support.asm.AsmTypeArtifactAnalyzer.analyse(AsmTypeArtifactAnalyzer.java:37)
    at org.eclipse.virgo.bundlor.support.StandardManifestGenerator.analyzeEntry(StandardManifestGenerator.java:114)
    at org.eclipse.virgo.bundlor.support.StandardManifestGenerator.analyzeEntries(StandardManifestGenerator.java:99)
    at org.eclipse.virgo.bundlor.support.StandardManifestGenerator.generate(StandardManifestGenerator.java:64)
    at org.eclipse.virgo.bundlor.maven.plugin.internal.MavenBundlorExecutor.execute(MavenBundlorExecutor.java:68)
    at org.eclipse.virgo.bundlor.maven.plugin.BundlorMojo.execute(BundlorMojo.java:222)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:133)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:108)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:76)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:116)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:361)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Manifest written to '/Users/me/proj/lamba/target/classes/META-INF/MANIFEST.MF'

Update, I tried using the newer version of the ASM library, but that didn't work:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>lamba</groupId>
  <artifactId>lamba</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>lamba</name>
  <properties>
     <java-source-version>1.8</java-source-version>
    <java-target-version>1.8</java-target-version>
  </properties>

  <build>
    <finalName>${project.artifactId}</finalName>

    <plugins>
      <plugin>
        <groupId>org.eclipse.virgo.bundlor</groupId>
        <artifactId>org.eclipse.virgo.bundlor.maven</artifactId>
        <version>1.1.2.RELEASE</version>

        <dependencies>
          <dependency>
            <groupId>org.eclipse.virgo.bundlor</groupId>
            <artifactId>org.eclipse.virgo.bundlor</artifactId>
            <version>1.1.2.RELEASE</version>
            <exclusions>
                <exclusion>
                  <groupId>org.objectweb.asm</groupId>
                  <artifactId>com.springsource.org.objectweb.asm</artifactId>
                </exclusion>
                <exclusion>
                  <groupId>org.objectweb.asm</groupId>
                  <artifactId>com.springsource.org.objectweb.asm.tree</artifactId>
                </exclusion>
                <exclusion>
                  <groupId>org.objectweb.asm</groupId>
                  <artifactId>com.springsource.org.objectweb.asm.commons</artifactId>
                </exclusion>
             </exclusions>
          </dependency>
          <dependency>
            <groupId>org.eclipse.virgo.bundlor</groupId>
            <artifactId>org.eclipse.virgo.bundlor.blint</artifactId>
            <version>1.1.2.RELEASE</version>
              <exclusions>
                <exclusion>
                  <groupId>org.objectweb.asm</groupId>
                  <artifactId>com.springsource.org.objectweb.asm</artifactId>
                </exclusion>
                <exclusion>
                  <groupId>org.objectweb.asm</groupId>
                  <artifactId>com.springsource.org.objectweb.asm.tree</artifactId>
                </exclusion>
                <exclusion>
                  <groupId>org.objectweb.asm</groupId>
                  <artifactId>com.springsource.org.objectweb.asm.commons</artifactId>
                </exclusion>
             </exclusions>
          </dependency>


            <dependency>
              <groupId>org.objectweb.asm</groupId>
              <artifactId>com.springsource.org.objectweb.asm.commons</artifactId>
              <version>3.2.0</version>
              <scope>compile</scope>
            </dependency>
        </dependencies>


        <executions>
          <execution>
            <id>${project.artifactId}-bundle-manifest</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>bundlor</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>${java-source-version}</source>
            <target>${java-target-version}</target>
            <skip>false</skip>
          </configuration>
        </plugin>
    </plugins>

  </build>




</project>

Upvotes: 2

Views: 796

Answers (2)

Tim Ward
Tim Ward

Reputation: 1199

To the best of my knowledge the bundlor plugin is no longer updated, and so is unlikely to be enhanced to understand Java 8 bytecode.

There are two main alternatives available based on a tool called bnd, which is used by lots of OSGi projects.

The bnd project hosts a plugin called the bnd-maven-plugin. There is an example project using it here, and it can easily be added to your pom as follows:

<plugin>
  <groupId>biz.aQute.bnd</groupId>
  <artifactId>bnd-maven-plugin</artifactId>
  <version>2.4.1</version>

  <executions>
    <execution>
      <goals>
        <goal>bnd-process</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Note that in order to pick up the bnd generated manifest you will also need to tell the maven-jar-plugin to pull it in. Typically you would add something like this to a parent pom:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <useDefaultManifestFile>true</useDefaultManifestFile>
  </configuration>
</plugin> 

The bnd-maven-plugin pulls configuration from a file called bnd.bnd, the documentation for which is available here

The other tool based on bnd is called the maven-bundle-plugin and is hosted by Apache Felix. The documentation is visible here, and tells you how to configure the pom.

<plugin>
  <groupId>org.apache.felix</groupId>
  <artifactId>maven-bundle-plugin</artifactId>
  <extensions>true</extensions>
  <configuration>
    <instructions>
      <Export-Package>org.foo.myproject.api</Export-Package>
      <Private-Package>org.foo.myproject.*</Private-Package>
      <Bundle-Activator>org.foo.myproject.impl1.Activator</Bundle-Activator>
    </instructions>
  </configuration>
</plugin>

As you can see the maven-bundle-plugin requires you to either change the packaging type of your module to bundle or to extend the default lifecycle so that it can package things properly. As a result it doesn't always play nicely with other maven-based tools. It is widely used though.

My personal preference is for the bnd-maven-plugin (I'm a bnd/bndtools committer) but both are probably suitable for you.

Upvotes: 1

BJ Hargrave
BJ Hargrave

Reputation: 9384

The problem is that the version of bundlor uses an older version of ASM which does not understand Java 8 class files. So you either need to get an update to bundlor or change to use another tool. bnd has been updated to support Java 8 class files.

Upvotes: 1

Related Questions