javadependenciesdependency-management

What are the preferred steps to avoid Dependency Hell in Java?


Recently I was in a pretty much trouble with a dependency hell problem in Java. Are there any proper set of procedures to avoid it in future?


Solution

  • First, use dependency manager like maven or gradle. No lib folder that contains third party jars, obviously no copying classes or *.java files from other project to yours. Sorry if this is obvious but I saw a lot of projects built using such technique.

    The next phase however is optimization of dependencies. You can use library A and B that both use library C. It is fine if both use library C of the same version. The hell starts if they depend on different versions of library C. In most cases this does not cause any problem and you can be even not aware on this fact. However sometimes it may cause problems. To avoid this I'd recommend you from time to time check the dependency tree of your project, find duplicates and if they exist use exclude instruction of your dependency manager to exclude older version.

    This however can fail because, of incompatibility of these versions of the library. In this case there is no general way to solve the problem. Sometimes you have to downgrade one (or several) of your dependencies in order to make them to work together and wait for the newer version that uses newer version of library C (in our example).

    The luck of java programmers in 2016 is that most of tools are typically open source and a lot of them are available for contributions (e.g. via github), so sometimes you can contribute your fix and help yourself and to other developers to get newer version faster.