Search code examples
javamavenbouncycastlemaven-assembly-plugin

Exclude BouncyCastle jar but include everything else in a fat jar


because BouncyCastle jars are signed and using jar-with-dependencies in maven-assembly-plugin will destroy these signature, I would like to know if it is possible to create this kind of output :

  • my code and every dependencies in a fat jar, but excluding BC jar
  • the BC jar in a lib/ subfolder

I manage to exclude BC jar in my fat jar using an assembly file looking like that :

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-dependencies</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <unpack>true</unpack>
        <excludes>
            <exclude>org.bouncycastle:bcprov-jdk15on</exclude>
        </excludes>
    </dependencySet>
</dependencySets>

Now, how can I tell maven-assembly-plugin to put the BC jar as standalone jar in a lib/ folder but still reference it in the manifest file ?

Best regards.

EDIT : I tried to do the following (not as clean as I wanted, but if it could works, it would be enough) :

I exclude the BC jar from the assembly, and I add a custom class-path injection in the maven archiver section of the maven-assembly-plugin :

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
                <descriptors>
                    <descriptor>src/main/assembly/jar.xml</descriptor>
                </descriptors>
                <archive>
                    <manifest>
                        <mainClass>${mainClass}</mainClass>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>lib/bcprov-jdk15on.jar</Class-Path>
                    </manifestEntries>
                </archive>
                <outputDirectory>${staging.dir}</outputDirectory>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

The manifest file is modified as expected :

Manifest-Version: 1.0
Implementation-Title: MyCode
Implementation-Version: 2.0.27.3
Built-By: ixo
Implementation-Vendor-Id: my.package
Class-Path: lib/bcprov-jdk15on.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_144
Implementation-Vendor: foo
Main-Class: my.package.Main

But the jvm doesn't seems to take the class-path entry into account, I don't understand why

EDIT 2 : Ok, I found why the previous edit don't works, maven-assembly-plugin generate the index.list file, and if this file is present, it is used instead of manifest.mf, and because I add BC jar by overriding class path in the maven-archiver section, then the index.list is wrong. Disabling indexing doesn't works if using fat jar generation.


Solution

  • Ok, so here is my really dirty solution, I am not very proud of it, and if someone can post a better one, would gladly up-vote his solution.

    Here are the step to make a fat jar with BC jar(s) outside :

    1. Create an assembly file that exclude any BC dependencies that your want <exclude>org.bouncycastle:bcprov-jdk15on</exclude>
    2. Use maven archiver to append custom class-path entries in the manifest.mf file <manifestEntries><Class-Path>lib/bcprov-jdk15on-1.57.jar</Class-Path></manifestEntries>
    3. Use truezip-maven-plugin to remove the index.list file from the fat jar

    There it is done.