Bruno
Bruno

Reputation: 84

How does Maven select implementations to use for a given interface?

This post is a question for my own knowledge, regarding some library code I'm reading at work. There is no code to fix. The developer who wrote the code is not in the company anymore, and I have nobody around to ask about this.

I'm reading the code for a small library project managed by Apache Maven. That library represents an API client. It consists mostly of a single class that implements a service that fetches data from a hardcoded API URL, and is supposed to be imported by a Spring server project in order to reach that API when necessary.

I see that the following dependencies had been added:

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>${jersey.version}</version>
</dependency>

The first seems to be Jakarta RESTful Web Services (formerly JAX-RS), the second is Eclipse Jersey client.

I was wondering why multiple HTTP clients had been added, so I found this article on Baeldung that states that JAX-RS is just an interface, while Jersey is an actual client.

However, I see that the client code class runs its capabilities with the following imports, where none mention Jersey, only JAX-RS.

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status.Family;

Then the client is invoked with ClientBuilder.newClient() and other capabilities all mention only the specification, not the implementation.

My question is: If only the specification/interface have been imported in the single Java class in this library, how does the Maven/Java library application know which implementation to actually use? Specially if I added multiple implementations to the same interface in the library's pom.xml?

Thanks in advance for the help!

EDIT: I've been recommended the following read What is ServiceLoader and how is it used? however I fail to see how this is specifically the engine responsible for the dependency resolution in this case. I'd like to have a discussion with the community to clarify the engines that are likely to be performing the dependency resolution.

Upvotes: 1

Views: 78

Answers (3)

Zack Macomber
Zack Macomber

Reputation: 6905

Maybe Maven has changed from how it worked in the past, but I have seen Maven (or maybe one of the underlying programs it calls like javac) load things alphabetically. I have had jar conflicts in the past and was able to resolve them via renaming alphabetically. This might not be applicable in your case but I have certainly seen first-hand jar resolutions changed via alphabetic renaming.

Again, might not be directly related to your question, but something to consider...

java.lang.NoSuchMethodError: org.springframework.http.MediaType.getCharset()Ljava/nio/charset/Charset

Upvotes: 0

jwenting
jwenting

Reputation: 5663

Maven doesn't do anything beyond supplying the build tools (compiler, packager, etc.) with the necessary files to do their work. In your case that'd be ALL the jars from both dependencies, and their underlying dependencies as well. Maven will often try to provide only the most recent viable version of a jar if multiple dependencies contain different versions of a jar, but that's about as far as its filtering goes (you can override this in the pom to a degree using things like exclusions).

What happens at runtime for the application is that both jars will be loaded as both are needed for full functionality and the classloader will select the desired class from one or the other as needed.

Upvotes: 1

J Fabian Meier
J Fabian Meier

Reputation: 35903

The imports are resolved from the dependencies. The ClientBuilder can be resolved from Jersey, or from other dependencies.

If different dependencies provide the same set of classes, it becomes essentialy random from which dependency the classes are drawn. This situation should be avoided.

Upvotes: 2

Related Questions