DEV Community

Mohammed Ammer
Mohammed Ammer

Posted on

Optimized Keycloak SPI Deployment using Maven Shade Plugin

When you develop Keycloak SPI, you probably add external dependencies (e.g. libraries). These dependencies are required to be in the classpath of Keycloak so your SPI can work as you expect.

To include the external libraries in Keycloak path, as mentioned in Keycloak Configuring providers:

Using third-party dependencies

When implementing a provider you might need to use some third-party dependency that is not available from the server distribution.
In this case, you should copy any additional dependency to the providers directory and run the build command. Once you do that, the server is going to make these additional dependencies available at runtime for any provider that depends on them.

To achieve that, you will probably need to download the JARs and copy it during building the Keycloak Docker Image.

This is a bit messy, especially when you have a new release and every time want to make sure to have the same dependencies versions.

To avoid all the hassle, I used 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.

It is also very useful when you've multiple SPIs but use different versions of same library (through the renaming feature).

A sample build configuration can be like:

...
<build>
    <plugins>
        <plugin>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <phase>package</phase>
                </execution>
            </executions>
            <groupId>org.apache.maven.plugins</groupId>
            <version>${maven-shade.version}</version>
        </plugin>
    </plugins>
</build>
...
Enter fullscreen mode Exit fullscreen mode

Follow the plugin documentation to configure the plugin by including/excluding/rename the dependencies.

That is all. I hope you find it useful.

Top comments (0)