javaspring-bootspring-data-mongodbmongock

Mongock failed: NoClassDefFoundError org/springframework/data/mongodb/MongoDatabaseFactory


I'm using the following pom in my spring boot app:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.13.RELEASE</version>
    <relativePath /><!-- resolve parent from repository, refer to http://www.tomitribe.com/blog/2016/06/i-do-not-hate-apache-maven/ -->
  </parent>

<dependencies>
        <dependency>
            <groupId>com.github.cloudyrock.mongock</groupId>
            <artifactId>mongock-bom</artifactId>
            <version>4.3.8</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>com.github.cloudyrock.mongock</groupId>
            <artifactId>mongock-spring-v5</artifactId>
            <version>4.3.8</version>
        </dependency>
        <dependency>
            <groupId>com.github.cloudyrock.mongock</groupId>
            <artifactId>mongodb-springdata-v3-driver</artifactId>
            <version>4.3.8</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongodb-driver-sync</artifactId>
            <version>4.2.2</version>
         </dependency>
         <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>2.2.12.RELEASE</version>
</dependency>
</dependencies>

when I run the app with java -jar my-app.jar I got the following error:

NoClassDefFoundError org/springframework/data/mongodb/MongoDatabaseFactory

but when I run the app with -noverify it seems everything ok

please, someone can assist me with that?


Solution

  • That's due to incompatible versions.

    As you can see in this compatibility table, the version of Mongock you are using is compatible with spring-data 3.x, but you are using 2.x instead.

    Mongock provides two drivers for spring-data, mongodb-springdata-v3-driver, for spring-data 3.x and mongodb-springdata-v2-drver for spring-data 2.2.X

    I have a couple of suggestions:

    1. Either upgrade your spring-data dependency to 3.x, or use mongodb-springdata-v2-drver(first one recommended)
    2. Use dependency management so you don't need to specify the version for each dependency. You can do it for spring and for Mongock

    With that you should fix your issue ;)