title: Why is it impossible to include multiple versions of the same dependency in Maven?
body: <p>Imagine a are writing an application that has 2 dependencies; lets call them dependency <code>A</code> and dependency <code>B</code>. You have no control over either of those dependencies; they are not your libraries, but  your application depends on them none-the-less. It turns out that dependency <code>A</code> has a dependency on dependency <code>X</code> (version 1.0); and B has a dependency on <code>X</code> (version 2.0).</p>
  
  <p>According to the <a href="http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies" rel="nofollow">Maven documentation</a>, <em>Dependency mediation</em> will be used to decide which dependency to use:</p>
  
  <blockquote>
    <p>Dependency mediation - this determines what version of a dependency
    will be used when multiple versions of an artifact are encountered.
    Currently, Maven 2.0 only supports using the "nearest definition"
    which means that it will use the version of the closest dependency to
    your project in the tree of dependencies. You can always guarantee a
    version by declaring it explicitly in your project's POM. Note that if
    two dependency versions are at the same depth in the dependency tree,
    until Maven 2.0.8 it was not defined which one would win, but since
    Maven 2.0.9 it's the order in the declaration that counts: the first
    declaration wins.</p>
  </blockquote>
  
  <p>So, presuming we declare dependency <code>A</code> first, then <code>X</code> version 1.0 will be used in our application. This means dependency <code>B</code> will be using <code>X</code> version 1.0 at runtime, where as was compiled against <code>X</code> version 2.0. If <code>B</code> is using some 2.0 only features, then we will get a run time error (<code>NoSuchMethodError</code>, <code>ClassNotFoundException</code> etc.), that's not good.</p>
  
  <p>So to "fix" this, we can exclude dependency <code>X</code> from dependency <code>A</code>, so that <code>X</code> version 2.0 is used instead. But oh no! <code>X</code> version 2.0 isn't backwards compatible, so we end up getting run time errors from <code>A</code> instead.</p>
  
  <p>As far as I can tell, there is no way to fix this properly with Maven. All you can do is hope to get hold of the source code of one of the libraries and fix it your self. Is that correct?</p>
  
  <p>Why is it not possible for both versions of the <code>X</code> to be packaged into my application, such that <code>A</code> uses <code>X</code> version 1.0, <code>B</code> uses <code>X</code> version 2.0, and the version of <code>X</code> available in my application is whatever Maven chooses through dependency mediation. Is it a limitation of Java, or a limitation of Maven? Is this situation common? Are there any other possible solutions? Should all libraries guarantee backwards compatibility to avoid this problem?</p>