WAR in WebSphere producing ClassNotFoundException: "org.apache.http.client.methods.HttpUriRequest" due to Apache Http Components dependency

Issue

I’m having an issue with a Maven dependency in a Java EE project (EAR + WAR). The WAR project POM has a JAR application dependency as seen below:

<dependency>
    <groupId>JAR_APP_groupid</groupId>
    <artifactId>JAR_APP_artifactid</artifactId>
    <version>JAR_APP_version</version>
</dependency>

This JAR application has a subdependency of org.apache.httpcomponents:httpclient:4.5.3 as seen below:

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

The WAR build includes both JAR and its subdependency in the lib folder. However, when I deploy the application to WebSphere 9.0, I receive an error java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpUriRequest. I need help understanding why this is happening.

The issue is that the org.apache.httpcomponents:httpclient:4.5.3 dependency is not being included in the EAR file, and therefore not available at runtime. To fix this issue, you can add the org.apache.httpcomponents:httpclient:4.5.3 dependency to the EAR project’s POM file as a provided dependency, like this:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.3</version>
    <scope>provided</scope>
</dependency>

This will ensure that the org.apache.httpcomponents:httpclient:4.5.3 dependency is included in the WAR file for compilation, but not included in the final EAR file. Instead, it will be provided by WebSphere at runtime.