What is Jar hell?
Jar hell or classpath hell or dependency hell is a term used to describe all the various ways in which the classloading process can end up not working. It ends with this list of the different circles of JAR hell: unexpressed dependencies, transitive dependencies shadowing, version conflicts, complex class loading.
Simple solution
Overwrite jar file in container by your libraries. Simply, copy your libraries to folder container/lib or some kind like that.
Several pitfalls may occur:
• We have to do it every freaking time we deploy the project.
• Depending on the container, we have to know how to do it properly.
• If we get the bad luck that the version we prefer to use is not compatible with the container, we may get more problem at runtime.
Use maven-shade-plugin
A better way is shading them. maven-shade-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.
How to do
For example, we have Google Guice lib is conflicting with Container
Step 1
Create a new maven project, for example repacked and add conflict dependency.
</pre> <project> <groupId>com.com.javacoffie</groupId> <artifactId>repacked</artifactId> <version>4.0</version> <dependencies> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.0</version> </dependency> </dependencies> </project> <pre>
Step 2 Configure shade plugin
</pre> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <configuration> <artifactSet> <includes> <include>com.google.inject:guice</include> <include>com.google.guava:guava</include> </includes> </artifactSet> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <relocations> --> <relocation> <pattern>com.google</pattern> <shadedPattern>com.example.shaded.google</shadedPattern> </relocation> <relocation> <pattern>com.google.thirdparty.publicsuffix</pattern> <shadedPattern>com.axonivy.shaded.google.thirdparty.publicsuffix</shadedPattern> </relocation> </relocations> </configuration> </execution> </executions> </plugin> </plugins> </build> <pre>
Step 4
Execute command
mvn clean install.
Step 5
Add new antifact as dependency to your project
<dependency> <groupId>com.javacoffie</groupId> <artifactId>repacked</artifactId> <version>4.0</version> </dependency>