Siato
Siato

Reputation: 365

Which class is loaded in a Maven project when multiple versions of same library are declared as dependencies?

The following is clearly bad practice but my question is out of pure curiosity: If I have a Maven project with two versions of the same library in the dependencies (e.g. jetty server in the pom.xml fraction below), then I import a class in my code (e.g. java code below), which version is the class picked up from?

  <project>
   ....

   <dependency>
     <groupId>org.eclipse.jetty</groupId>
     <artifactId>jetty-server</artifactId>
     <version>9.3.0.v20150612</version>
   </dependency>
   <dependency>
     <groupId>org.eclipse.jetty</groupId>
     <artifactId>jetty-server</artifactId>
     <version>8.1.17.v20150415</version>
   </dependency>

   ....
   </project>

And the java code:

package test.hello;

import org.eclipse.jetty.server.Server;

public class Hello {

    Server server;

    public Hello() {
        server = new Server();
    }
}

Is the server object created from the old version class or the new version class? Maven does not complain about any ambiguity. It just loads a class; I don't know which.

Upvotes: 0

Views: 233

Answers (1)

Johnson Abraham
Johnson Abraham

Reputation: 781

Maven has nothing to do with loading jars or classes. Maven only gathers all the dependancy. Its upto your classloader to decide what to load when.

If you have two classes with the same binary name, and you want to know which one of them you are loading, you can only inspect the way that classloader tries to resolve a class name.

Upvotes: 1

Related Questions