Pulkit Anchalia
Pulkit Anchalia

Reputation: 774

Create dependency folder with Dependent jars with Maven Shade plugin

I am creating a fat jar using maven shade plugin, which includes some bouncy castle jars too. But this creates the problem because of the unsigned version of Bouncy Castle.

java.lang.SecurityException: JCE cannot authenticate the provider BC

Now one of the solution is to have external folder of dependencies and define this class path in manifest file of fat jar.

For example:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                            <artifactSet>
                                <excludes>
                                    <exclude>org.bouncycastle:*:*:*</exclude>
                                </excludes>
                            </artifactSet>
                        <finalName>Relay-S3-Monitor-jar-with-dependencies</finalName>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>at.seresunit.lecturemanager_connector.App</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                 <manifestEntries>
                                    <Main-Class>com.pb.relay.s3.CamelBoot</Main-Class>
                                    <Class-Path>. bouncycastle_libs/bcpg-jdk15on-1.55.jar bouncycastle_libs/bcprov-jdk15on-1.55.jar bouncycastle_libs/bcprov-jdk16-1.45.jar</Class-Path>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin

Now what I need: in the same pom.xml, I need to insert a section (plugin) which creates depencies folder(with only bouncy castle jar)

Upvotes: 5

Views: 2815

Answers (3)

kriegaex
kriegaex

Reputation: 67297

If you are looking for a Fat JAR solution without unpacking the libraries but with a special JAR classloader, take a look at my project here.

Disclaimer: I did not write the code, just package it and publish it on Maven Central and describe in my read-me how to use it.

I personally use it for creating runnable uber JARs which containing BouncyCastle dependencies. Maybe it is useful for you, too.

Upvotes: 1

MattW
MattW

Reputation: 812

I was seeing the same error when compiling an uber jar that relied on BouncyCastle:

Exception in thread "main" java.security.NoSuchProviderException: JCE cannot authenticate the provider BC

I realize in your case you were good with exporting jars to a separate directory, but for those interested in the single jar, the problem is that Maven's shade plugin explodes the jar files which breaks the cryptographic signature (this explanation for details).

You can instead use the executable packer maven plugin solution that uses a jar-in-jar approach which preserves the signature for JCE in a single, executable jar.

Upvotes: 1

J Fabian Meier
J Fabian Meier

Reputation: 35805

With the dependency plugin, you can call dependency:copy to copy a dependency to a folder

https://maven.apache.org/plugins/maven-dependency-plugin/copy-mojo.html

Upvotes: 2

Related Questions