Home > Mobile >  Maven dependencies wrong version for okhttp3 mockwebserver
Maven dependencies wrong version for okhttp3 mockwebserver

Time:04-12

I am trying to use okhttp3.mockwebserver with my Spring boot project and I find out that okhttp3:mockwebserver:jar:3.14.9 is included instead of 4.9.1.

I have created small 'mock' projects to reproduce the issue I have in my prod.

The project is here https://github.com/mkarasik/okhttp-test

It contains two folders:

lib

This is a simple library including mockwebserver as dependency

pom.xml dependency

    <dependencies>
    ...
    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>mockwebserver</artifactId>
        <version>4.9.1</version>
        <exclusions>
            <exclusion>
                <artifactId>junit</artifactId>
                <groupId>junit</groupId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

Maven dependencies tree

 \- com.squareup.okhttp3:mockwebserver:jar:4.9.1:compile
     - com.squareup.okhttp3:okhttp:jar:3.14.9:compile

This is already wrong. Mockwebserver pom contains 4.9.1 okhttp artifact, however 3.14.9 is shown in tree

project

Simple Spring Boot app including lib project

    <dependency>
        <groupId>com.example</groupId>
        <artifactId>lib</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>test</scope>
    </dependency>

Maven dependencies tree

\- com.example:lib:jar:0.0.1-SNAPSHOT:test
   \- com.squareup.okhttp3:mockwebserver:jar:3.14.9:test
      \- com.squareup.okhttp3:okhttp:jar:3.14.9:test
         \- com.squareup.okio:okio:jar:1.17.2:test

The same problem is here. okhttp3:mockwebserver:jar:3.14.9 is included instead of 4.9.1 as it is specified in my lib pom.xml.

Is there anything I am missing in my xml configuration?

CodePudding user response:

Found it it it is described in Introducing dependencies in other projects causes Maven to downgrade okhttp3 version

<properties>
    <okhttp3.version>4.9.1</okhttp3.version>
</properties>

Fixes the issue

CodePudding user response:

OkHttp provides a Maven BOM you can use to ensure a consistent version

https://github.com/square/okhttp#releases

Also, we have a bill of materials (BOM) available to help you keep OkHttp artifacts up to date and be sure about version compatibility.

This example is gradle, but you it is originally a feature from maven.

https://docs.gradle.org/6.2/userguide/platforms.html#sub:bom_import

dependencies {
   // define a BOM and its version
   implementation(platform("com.squareup.okhttp3:okhttp-bom:4.9.3"))

   // define any required OkHttp artifacts without version
   implementation("com.squareup.okhttp3:okhttp")
   implementation("com.squareup.okhttp3:logging-interceptor")
}
  • Related