How to use maven-shade and felix to generate a shaded jar with a new manifest - maven

I'm currently working on a project that is attempting to integrate use both shade and felix. The goal is to create a shaded jar that contains only the code we need, then use felix to create the manifest we need. The build part of my pom is as follows:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>false</shadedArtifactAttached>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>${project.build.directory}</manifestLocation>
<niceManifest>true</niceManifest>
</configuration>
</plugin>
</plugins>
</build>
Now the issue I'm running in to is something I've seen elsewhere, but all of those threads seem to die right at this point. So the shaded jar is correctly created, then Felix runs afterwards and puts the MANIFEST.MF file (which is correct as far as I can tell) in target/classes/META-INF/ but it doesn't put that same manifest in the shaded jar. The one inside the jar is the same manifest that existed before Felix ran.
It almost seems like I need Shade to run, then Felix, then re-run the jar creation. Am I missing something?
I'm trying to figure out how to, for lack of a better term, re-package the JAR with the new manifest.

Your main problem is that the manifest must be generated in the jar file, you can generate your Manifest file from the shade plugin (adapt with your needs):
<configuration>
<transformers>
<!-- This bit sets the main class for the executable jar as you otherwise -->
<!-- would with the assembly plugin -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Import-Package>org.apache.common</Import-Package>
<Export-Package>org.test</Export-Package>
<Main-Class>com.br.iacit.tutorialdoJar.ImageLab</Main-Class>
<Specification-Title>Java Advanced Imaging Image I/O Tools</Specification-Title>
<Specification-Version>1.1</Specification-Version>
<Specification-Vendor>Sun Microsystems, Inc.</Specification-Vendor>
<Implementation-Title>com.sun.media.imageio</Implementation-Title>
<Implementation-Version>1.1</Implementation-Version>
<Implementation-Vendor>Sun Microsystems, Inc.</Implementation-Vendor>
</manifestEntries>
</transformer>
<!-- This bit merges the various GeoTools META-INF/services files -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
</configuration>
Output Manifest:
Manifest-Version: 1.0
Toto: test
Export-Package: org.test
Archiver-Version: Plexus Archiver
Built-By: NG673AB
X-Compile-Target-JDK: 1.7
Import-Package: org.apache.common
X-Compile-Source-JDK: 1.7
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_66
Main-Class: tt.tt.main
Edit: I managed to make it so that it compiles correctly, see below:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>META-INF/MANIFEST.MF</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/MANIFEST.MF</resource>
<file>src/main/resources/MANIFEST.MF</file>
</transformer>
</transformers>
<shadedArtifactAttached>false</shadedArtifactAttached>
<artifactSet>
<includes>
<include>${project.groupId}:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>package</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<manifestLocation>src/main/resources/</manifestLocation>
<niceManifest>true</niceManifest>
<instructions>
<Export-Package>test</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

Related

How to prevent a shaded jar to be installed to the local repository

At the end of my "package" step I end up with 2 jars : the one made by the "maven-jar-plugin" and the uber/fat/shaded one made by "maven-shade-plugin".
myapp.jar and myapp-uber.jar
At the "install" phase both jar are copied to the local repository. And I see little (=no) reason to have the uber jar copied to my local repository. Further more, it takes unless place on my HDD. And, anyway it will copied to right location at the "deploy" phase.
Here are those instructions :
<!-- Regular jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
</execution>
</executions>
<configuration>
<archive>
<manifestEntries>
<SplashScreen-Image>icons/splash.png</SplashScreen-Image>
<url>${project.url}</url>
</manifestEntries>
</archive>
</configuration>
</plugin>
<!-- Uber jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>exe</shadedClassifierName>
<finalName>${project.exe_finalname}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>myapp.MainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- Deploy -->
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>deploy</phase>
<configuration>
<target name="Pushing to builds repository"><!-- ... --></target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
So how can I prevent the uber jar from being copied to the local repository ?
Thanks.

Maven shade output not found

I'm using the Maven Shade plugin, but I can't find its output.
The log says it clearly : the Maven-shade-plugin has worked.
But in the end, the output of the plugin is not where it is supposed to be.
Why ?
Maven log:
--- maven-shade-plugin:2.0:shade (default) # zploger ---
Including log4j:log4j:jar:1.2.17 in the shaded jar.
Including zparkingb:utils:jar:1.5.0 in the shaded jar.
Including zparkingb:swing:jar:1.5.6 in the shaded jar.
(...)
We have a duplicate org/xmlpull/v1/XmlPullParser.class in C:\Users\laurent\.m2\repository\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar
We have a duplicate org/xmlpull/v1/XmlPullParserException.class in C:\Users\laurent\.m2\repository\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar
Replacing D:\work\Zploger\builds\zplogerAudio-exe.jar with D:\work\Zploger\builds\zploger-3.2.0.002-exe.jar
Attaching shaded artifact.
And the pom/xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>exe</shadedClassifierName>
<finalName>${project.exe_finalname}</finalName>
<outputDirectory>../builds</outputDirectory>
<artifactSet>
<excludes>
<exclude>demo:*</exclude>
</excludes>
</artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.zparkingb.zploger.GUI.Zploger</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

Maven shade plugin configuration not replacing the package

Based on the requirement at Maven dependency incompatible library class, I have tried shade plugin as like below, but went in vain.
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.lib:Encoder</artifact>
<includes>
<include>x/y/z/**</include>
</includes>
<excludes>
<exclude>a/b/c/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
My target here is to replace the package with of a.b.c structure with x.y.z of classes.
Did I miss any crucial configurations here?
To replace the package a.b.c with x.y.z on your shaded jar you should add the relocations entry as follow on maven-shade-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>x.y.z</pattern>
<shadedPattern>a.b.c</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>

How to eliminate org/apache/jsp from the output path of the Apache Sling jspc-maven-plugin?

I am trying to use the jspc-maven-plugin from Apache Sling. When I compile my JSPs, they end up in my target/org/apache/jsp directory. I want them to go go directly under target or under target/classes w/o the org/apache/jsp part. How can I achieve this? Here is my plugin specification:
<plugin>
<groupId>org.apache.sling</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<executions>
<execution>
<id>compile-jsp</id>
<goals>
<goal>jspc</goal>
</goals>
<configuration>
<sourceDirectory>${basedir}/source/html</sourceDirectory>
<outputDirectory>${basedir}/target</outputDirectory>
<includes>
<include>**/*.jsp</include>
</includes>
<showSuccess>true</showSuccess>
<failOnError>false</failOnError>
<compilerSourceVM>${javac.source}</compilerSourceVM>
<compilerTargetVM>${javac.target}</compilerTargetVM>
<servletPackage/> <!-- doesn't seem to matter - deprecated -->
</configuration>
</execution>
</executions>
</plugin>

unpack dependency and repack classes using maven?

I am trying to unpack a maven artifact A and repack it into a new jar file in the maven project B.
Unpacking class files from artifact A into:
<my.classes.folder>${project.build.directory}/staging</my.classes.folder>
works fine using this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.test</groupId>
<artifactId>mvn-sample</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${my.classes.folder}</outputDirectory>
<includes>**/*.class,**/*.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
In the same pom I now want to generate an additional jar containing the classes just unpacked:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesdirectory>${my.classes.folder}</classesdirectory>
<classifier>sample</classifier>
</configuration>
</execution>
</executions>
</plugin>
A new jar is created but it does not contain the classes from the:
${my.classes.folder}
its simply a copy of the default project jar. Any ideas?
I have tried to follow this guide:
http://jkrishnaraotech.blogspot.dk/2011/06/unpack-remove-some-classes-and-repack.html
but its not working.
I would suggest you to use the maven-shade-plugin instead.
That will make the unpack-repack in the same invocation.
You could do something like this for example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.test:mvn-sample:1.0.0-SNAPSHOT</artifact>
<includes>
<include>**/*.class</include>
<include>**/*.xml</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
In the sample you have <classesdirectory>, the docs have the element as <classesDirectory>. Case sensitivity matters, I think.

Resources