1

Using Mongock https://github.com/cloudyrock/mongock for data migration spring boot 2.3.1.RELEASE, Previously was using Mongobee but due to this error org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'mongoTemplate' and suggestion from everyone to switch to Mongock An attempt was made to call a method that does not exist, mongo-java-driver , mongobee

Now I am using Mongock for data migration but keep getting error

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    com.github.cloudyrock.mongock.SpringMongockBuilderBase.lambda$getMongoTemplateProxySupplier$0(SpringMongockBuilderBase.java:138)

The following method did not exist:

    'org.springframework.data.mongodb.MongoDbFactory org.springframework.data.mongodb.core.MongoTemplate.getMongoDbFactory()'

The method's class, org.springframework.data.mongodb.core.MongoTemplate, is available from the following locations:

    jar:file:/Users/macbook/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-mongodb/3.0.1.RELEASE/24961d107813268bb9cd3daa8d4a9389060faea7/spring-data-mongodb-3.0.1.RELEASE.jar!/org/springframework/data/mongodb/core/MongoTemplate.class

The class hierarchy was loaded from the following locations:

    org.springframework.data.mongodb.core.MongoTemplate: file:/Users/macbook/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-mongodb/3.0.1.RELEASE/24961d107813268bb9cd3daa8d4a9389060faea7/spring-data-mongodb-3.0.1.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.data.mongodb.core.MongoTemplate

This is the error is due to the dependency I know but don't know how to resolve

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'fete.bird'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'
compileJava {
    options.compilerArgs += ["--enable-preview"]
}
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR6")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    implementation 'org.springdoc:springdoc-openapi-ui:1.4.3'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

    compile "org.mongodb:mongo-java-driver:3.12.6"
    compile("com.github.cloudyrock.mongock:mongock-spring:3.3.2") {
        exclude group: 'org.mongodb', module: 'mongo-java-driver'
    }
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}
M. Justin
  • 14,487
  • 7
  • 91
  • 130
San Jaisy
  • 15,327
  • 34
  • 171
  • 290

2 Answers2

1

The issue is with the mongock:mongock, Update to the latest Beta Version and the issue is resolved with addition work.

Remove all the mongock dependencies and add the below dependency

compile "com.github.cloudyrock.mongock:mongock-bom:4.1.2.BETA"
    compile 'com.github.cloudyrock.mongock:mongock-spring-v5:4.1.2.BETA'
    compile 'com.github.cloudyrock.mongock:mongodb-springdata-v3-driver:4.1.2.BETA'

Now the Bean is change as below

  @Bean
    public MongockSpring5.MongockInitializingBeanRunner mongockInitializingBeanRunner(ApplicationContext springContext, MongoTemplate mongoTemplate) {
        boolean migrationsEnabled = Boolean.parseBoolean(environment.getProperty("app.db.migrations.enabled"));
        return MongockSpring5.builder()
                .setDriver(new SpringDataMongo3Driver(mongoTemplate,4,4,3))
                .addChangeLogsScanPackages(List.of("fete.bird.fetebirdproduct.migration"))
                .setSpringContext(springContext)
                .setEnabled(migrationsEnabled)
                .buildInitializingBeanRunner();
    }

Reference

https://github.com/cloudyrock/mongock/issues/195

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
1

In addition to San's answer, this issue can be a bit more useful.(Please notice you should update Mongock's version to 4.1.2.BETA)

There you can see that you don't need to build the bean yourself, by annotating your Spring application with @EnableMongock, it will do job for you.

Also worthy looking at this sample project

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
Mongock team
  • 1,188
  • 5
  • 9