Home > Enterprise >  Is there a sample project in github that shows how to use the maven-shade-plugin?
Is there a sample project in github that shows how to use the maven-shade-plugin?

Time:09-27

As the title says, I'm looking for a simple example in github that contains a code example that shows how to use the [the maven-shade-plugin] to rename classes when two versions of a library must be included in an application.

I found this answer on SO: Diagram showing how maven-shade-plugin renames version 1 of dependency

In the mean time, I'm starting to create my on version of this example and will post what I've created if no one points one out to me.

I have found several web pages that describe the plugin but nothing that has included a pointer to a repo with sample code.

Some useful writes up on the maven-shade-plugin

  • The plugin documentation: Dependency Tree for application

    Each of the pom files create a JAR file so there are 4 (four) jar files created by this code example.

    Summary of the Problem

    The next diagram shows the loglib.jar file being used in both the main application (helloworld)

    Summary of Problem

    It shows the class LogIt contains a method sayHello which takes one argument String name in version 1.0.0 of the package (liblog.jar) and sayHello takes two arguments in the 2nd version of the package.

    While this example is contrived to demonstrate the general problem that faces Java developers on more complex code bases, this simplified version of the problem is intended to make the problem easy to understand and to fix.

    So by default, when a FAT jar is created for the HelloWorld application, the Fat jar only contains one copy of the class file com.steranka.play.LogIt. The result is that either the HelloWorld class will crash or the GoodFeature class will crash. Where crash means throw an exception Below is an example of the exception I saw when I first ran the application.

    Hello World!
    What's up, Sam
    Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.String com.steranka.play.LogIt.sayHello(java.lang.String)'
    at com.steranka.play.GoodFeature.sayGoodbye(GoodFeature.java:6)
    at com.steranka.play.HelloWorldApp.main(HelloWorldApp.java:15)
    

    This problem occurs because version 2.0.0 of the loglib.jar was used which contained the signature:

    sayHello(String name, String greeting)
    

    and the code in GoodFeature.sayGoodbye had bytecode which called the version 1.0.0 of the signature which was:

    sayHello(String name)
    

    Since that signature did not exist, the exception occured.

    The Solution

    The solution is to include both versions of the loglib jar file as shown in the next diagram. Summary of the Solution

    Next Steps

    If you want to follow along with what I did, and learn how I solved this problem continue here with the doc/01-Starting.md file.

  • Related