Replace dependency org.apache.httpcomponents:httpclient in maven-pmd-plugin and maven-checkstyle-plugin

1.6k views Asked by At

I do use maven-pmd-plugin:3.15.0 and the maven-checkstyle-plugin:3.1.2.

Both do import transitively on an old version of the org.apache.httpcomponents:httpclient:4.0.2.

How can I configure each plugin to use the newest version of the httpclient (org.apache.httpcomponents:httpclient:4.5.13)?

Edited regarding below comment why I need to replace the dependency: My project (springboot 2.4.x application) uses camel-http-starter dependency which also transitively imports the 4.5.13 version and after calling mvn clean install or verify|test|packagerandomly I do get following exception

Exception in thread "ivy-httpclient-shutdown-handler" java.lang.NoClassDefFoundError: org/apache/http/impl/conn/PoolingHttpClientConnectionManager$2
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.shutdown(PoolingHttpClientConnectionManager.java:413)
        at org.apache.http.impl.client.HttpClientBuilder$2.close(HttpClientBuilder.java:1244)
        at org.apache.http.impl.client.InternalHttpClient.close(InternalHttpClient.java:201)
        at org.apache.ivy.util.url.HttpClientHandler.close(HttpClientHandler.java:357)
        at org.apache.ivy.util.url.HttpClientHandler$1.run(HttpClientHandler.java:84)
        at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.ClassNotFoundException: org.apache.http.impl.conn.PoolingHttpClientConnectionManager$2
        at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
        ... 6 more

As I read in other forums here old versions of the httpclient do not contain the PoolingHttpClientConnectionManager as this has been introduced in 4.3.

The build does work fine. Its just this exception, which appears from time to time just right after the BUILD SUCCESS message.

My assumption is that maven has a flat classpath with every version of the httpclient and those time when it picks an old it cant find this class PoolingHttpClientConnectionManager$2 and the exception is thrown.

2

There are 2 answers

0
khmarbaise On

In general take a look on the plugins page https://maven.apache.org/plugins/ which lists the most recent versions of plugins.

So for maven-pmd-plugin there is a version 3.16.0 which I recommend to upgrade to.

Furthermore for the maven-checkstyle-plugin there is currently no newer version availble.

1
Vishnu Singh On

Exclude the http-client dependency from maven-checkstyle-plugin:3.1.2

<dependencies>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.3</version>
        <exclusions>
            <exclusion>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

This works for me