Using maven-bundle-plugin with the maven-shade-plugin

5.4k views Asked by At

I'm using the maven-shade-plugin to relocate some packages during the package phase of my build. I'm also using the maven-bundle-plugin to generate a manifest. The problem is the bundle plugin runs before the shade plugin (during the process-classes phase), and doesn't include any of my shaded packages in the generated manifest's exports.

How can I get these two plugins to play nice with each other, so that my relocated packages are treated like any other package by the bundle plugin?

--

By request, the Shade and bundle sections of my POM:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <filters>
        <filter>
          <artifact>cglib:cglib</artifact>
          <includes>
            <include>net/sf/cglib/core/**</include>
            <include>net/sf/cglib/proxy/**</include>
          </includes>
        </filter>
      </filters>
      <relocations>
        <relocation>
          <pattern>net.sf.cglib</pattern>
          <shadedPattern>org.modelmapper.internal.cglib</shadedPattern>
        </relocation>
        <relocation>
          <pattern>org.objectweb.asm</pattern>
          <shadedPattern>org.modelmapper.internal.asm</shadedPattern>
        </relocation>
      </relocations>
    </configuration>
  </plugin>

  <plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>2.3.7</version>
    <executions>
      <execution>
        <id>bundle-manifest</id>
        <phase>process-classes</phase>
        <goals>
          <goal>manifest</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <instructions>
        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
        <Export-Package>
          org.modelmapper,
          org.modelmapper.builder,
          org.modelmapper.config,
          org.modelmapper.convention,
          org.modelmapper.spi
        </Export-Package>
        <Private-Package>
          org.modelmapper.internal.**
        </Private-Package>
        <Import-Package>
          *
        </Import-Package>
        <Include-Resource>
          {maven-resources},
          {maven-dependencies}
        </Include-Resource>
      </instructions>
    </configuration>
  </plugin>

Taken from here

4

There are 4 answers

1
linski On

I assume that after the compile phase is done you want to:

  1. relocate some classes with the shade plugin
  2. create a manifest with the bundle plugin
  3. pack it all up with the jar plugin

The problem is the bundle plugin runs before the shade plugin

The bundle plugin is binded to process-classes phase which comes before the package phase to which the shade plugin is bound.

I suggest that you bind the shade plugin to process-classes phase also. Change shade plugin configuration like this:

<phase>process-classes</phase>

Since the shade plugin definition comes before the bundle plugin definition in the pom file, the shade plugin will run before the bundle plugin during the process-classes phase.

0
Anthony Accioly On

Another option is to dump maven bundle plugin altogether and use Maven Shade Plugin ManifestResourceTransformer to add the desired OSGI metadata to the manifest.

Take a look at xbean-asm-shaded/pom.xml for a example.

<transformers>
  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <manifestEntries>
      <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
      <Export-Package>
        org.apache.xbean.asm;org.modelmapper.builder; ...
      </Export-Package>
      <Import-Package>*</Import-Package>
      <Private-Package>org.modelmapper.internal ...</Private-Package>
   </manifestEntries>
</transformer>
1
splatch On

Solution is very simple. You still can use maven-bundle-plugin and maven-shade-plugin at the same time. You just need to remember about the order. If you use bundle packaging maven bundle plugin will get executed during package phase before maven-shade. But that's not so wrong.

Here is the deal.

  • Use Private-Package: pkg.name.before.shading
  • Make maven-shade-plugin with one <include>null:null</include> - this will prevent shade plugin from creating empty jar
  • use maven-shade-plugin relocations - from pkg.name.before.shading to other.pkg.

You may see this trick working in FasterXML jackson-module-paranamer

0
buer On

There is a neat transformer implementing just this functionality from Hazelcast - HazelcastManifestTransformer (ver 3.9). What it does is thoughtfully merging the Import-Package and Export-Package attributes, enabling the user to exclude extend/reduce the default merged result.

How to use it in your pom.xml:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.1.0</version>
  <dependencies>
    <dependency>
      <groupId>com.hazelcast</groupId>
      <artifactId>hazelcast-build-utils</artifactId>
      <version>3.9</version>
    </dependency>
  </dependencies>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>shade</goal>
      </goals>
      <configuration>
        <transformers>
          <transformer implementation="com.hazelcast.buildutils.HazelcastManifestTransformer">
            <mainClass>...</mainClass>
            <!-- the tag below is required due to a missing null-check it seems -->
            <overrideInstructions></overrideInstructions>
      </configuration>
    </executions>
</plugin>

The override instructions (Export/Import-Package) are comma delimited package names, preceded with an exclamation mark whenever we want to exclude those specific ones from the list.

Hope this helps! I do realize that's an old question, but seems the Hazelcast's transformer isn't gaining much publicity.