Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 625
Jonathan Avatar asked Aug 16 '13 19:08

Jonathan


People also ask

What is the use of Maven Shade plugin?

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.

What is Maven Shade plugin version?

Apache Maven Shade Plugin/ | Last Published: 2022-09-11. Version: 3.4.0.

How do you shade a dependency?

In Java, to “shade” a dependency is to include all its classes and the classes from its transitive dependencies in your project, often renaming the packages and rewriting all affected bytecode.

What does shaded mean in Maven?

Shade (relocate) one version of a transitive dependency, but not the other.


1 Answers

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>
like image 142
Anthony Accioly Avatar answered Nov 04 '22 22:11

Anthony Accioly