javaintellij-ideaannotation-processingmapstruct

Mapstruct generated classes are not found if the application is run from IntelliJ IDEA


I have a Java 11 based application built with Gradle 4.8.1 which uses Mapstruct 1.3.0.Final. I use IntelliJ IDEA Ultimate 2019.2.3 for development.

Problem:

The application does't work if I start it from IntelliJ IDEA, however it works when it is built with Gradle.

I get the following error in run time when I start the app from IntelliJ:

ClassNotFoundException: Cannot find implementation for com.mycompany.MyMapper
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75)
    at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58)

My project setup:

I use one more annotation processing tool - Lombok which works fine. All together I have them in my dependencies like this:

compileOnly('org.projectlombok:lombok:1.18.10')
annotationProcessor('org.projectlombok:lombok:1.18.10')
compile('org.mapstruct:mapstruct:1.3.0.Final')
annotationProcessor('org.mapstruct:mapstruct-processor:1.3.0.Final')

How to reproduce: It is enough to start a unit test from IntelliJ UI that requires a class generated with Mapstruct.

Question: How to make IntelliJ IDEA correctly apply Mapstruct annotation processors?

What I have tried: I went through similar issues on StackOveflow and on GitHub. Unfortunately, they didn't help. They are either using old versions of the lib, an old JVM or simply the solutions don't work.


Solution

  • Please set "Build and Run using Gradle" in "Settings/Preferences | Build, Execution, Deployment | Build Tools | Gradle"