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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><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>
</code>
<code><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> </code>
<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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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;
</code>
<code>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; </code>
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.

3

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.

3

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.

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật