1

I have two maven projects, lets call them master and aux. Master has a dependence on aux, and also on a specific version of org.apache.httpcomponents.httpclient. Aux has a dependence on a later version of org.apache.httpcomponents.httpclient.

e.g.

<project...>
  <artifactId>master</artifactId>
  <groupId>com.my-company</groupId>

  <dependencies>

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>earlier version</version>
    </dependency>

    <!--<uses later version of http client>-->
    <dependency>
      <groupId>com.my-company</groupId>
      <artifactId>aux</artifactId>
    </dependency>

    ...
  </dependencies>
  ...
</project>

However, aux depends on classes only found in the newer version of httpclient, and master's dependencies on httpclient aren't forwards compatible, so whichever version I exclude, REST calls fail in the expected places.

Is there a way to require aux to use the newer dependency, and master to use the older one?

I know that I can reconcile aux and master by patching them to be able to use the same dependency, but this would be far from ideal.

Chris
  • 409
  • 3
  • 17
  • Short answer: No. See https://stackoverflow.com/questions/35381533/maven-multiple-version-of-same-dependency –  Nov 15 '18 at 09:25
  • You have to make a new version of aux which uses the newer version of http otherwise you are lost... – khmarbaise Nov 15 '18 at 11:29

2 Answers2

2

You can't have multiple versions of the same library/classes on the classpath at the same time. You would have to separate 'master' and 'aux' into separate jars, embedding the needed version of httpclient, and load each jar with its own classloader.

There is a framework called OSGi that does exactly this. It could be overkill for you application, but if you'd like to get started you could take a look at OSGi enRoute.

gjoranv
  • 4,376
  • 3
  • 21
  • 37
0

You can write a custom classloader to load specified version of class, because the default classloader will just pick the first one on the classpath it can find.