Mapstruct: Consider defining a bean of type ''FooMapper" in your configuration

Viewed 603

I'm trying to get Mapstruct working with gradle. The implementations generate and work great when I run the service from the boot dashboard in eclipse, but they don't generate when run with gradlew bootrun, and the bean can't be found. Here is the error:

*************************** APPLICATION FAILED TO START


Description:

Field fooMapper in ServiceImpl required a bean of type 'FooMapper' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'FooMapper' in your configuration.

And here is my build.gradle file:

plugins {
    id "org.springframework.boot"
    id "groovy"
    id "com.diffplug.eclipse.apt" version "3.31.0" 
}

configurations {
  // configuration to enable running bootRun locally
  // with embedded h2 database
  runlocal
}

ext {
    mapstructVersion = "1.5.0.Beta1"
}

dependencies {
  implementation "org.mapstruct:mapstruct:${mapstructVersion}"
  annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
  testImplementation "org.testng:testng:6.10", "org.easytesting:fest-assert:1.4"
  implementation project(":common")
  implementation "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
  implementation "org.springframework.boot:spring-boot-starter-jdbc:$springBootVersion"
  implementation "org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion"
  implementation "org.springframework.boot:spring-boot-starter-actuator:$springBootVersion"
  implementation "org.springframework.boot:spring-boot-starter-validation:$springBootVersion"
  implementation "org.codehaus.gpars:gpars:1.2.1"

  developmentOnly "org.springframework.boot:spring-boot-devtools:$springBootVersion"
  
  runlocal project(":test-db")
  
  testImplementation project(":test-db")
}

sourceSets.main.groovy.srcDirs += ["build/generated/sources"]

bootRun {
  classpath = sourceSets.main.runtimeClasspath + configurations.runlocal
}

tasks.withType(JavaCompile) {
    options.compilerArgs = [
        "-Amapstruct.suppressGeneratorTimestamp=true",
        "-Amapstruct.defaultComponentModel=spring",
        "-Amapstruct.verbose=true"
    ]
}

Is it because my code is written in groovy? But then why would it work with the boot dashboard in eclipse? I am using @Mapper(componentModel = 'spring'), but that hasn't fixed the issue. Using version 1.4.2.FINAL also doesn't work. Any help would be really appreciated!

1 Answers

had the same problem in eclipse, solved configuring my project properties:

Go to: Java Compiler > Annotation Processing

Set Enable Project specific settings to true

Go to: Java Compiler > Annotation Processing > Factory Path

Add external jar: Here find the location of your mapstruct-processor-1.4.2-Final.jar on the gradle cache directory, i am using debian 11, my path is on:

/.gradle/caches/modules-2/files-2.1/org.mapstruct/mapstruct-processor/1.4.2.Final/e55bd90d51cddd638c07d5bd89fc7535d4e3d069/mapstruct-processor-1.4.2.Final.jar

Apply and close, and gradle should rebuild the project, generating two new folders .apt_generated and .apt_generated_tests on the package explorer with the mapper code

Related