Mapstruct Annotation Processor does not seem to work in Intellij with Gradle project
Asked Answered
M

6

8

I'm trying to use Intellij 2017 Ultimate to build/run a Spring Boot application that uses MapStruct. It is a Gradle project. My issue is that IntelliJ does not seem to run the MapStruct Annotation Processor. I realize that I can configure IntelliJ to delegate to the Gradle build process (see this), but I am hoping to simply configure IntelliJ to use APT to generate the necessary classes itself.

I have enabled APT for my project, but my classes are still not generated.

build.gradle (applicable snippets):

ext {
    mapstructVersion = '1.2.0.Final'
}

plugins {
    id 'net.ltgt.apt' version '0.15'
}

dependencies {
    // MapStruct support
    implementation group: 'org.mapstruct', name: 'mapstruct-jdk8', version: mapstructVersion
    annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: mapstructVersion
 }

IntelliJ configuration:

enter image description here

Yet, when I do a ./gradle clean followed by a Build->Rebuild Project, my out/production/classes/generated folder is empty.

Is there something additional that I need to do to enable APT on this project? Should IntelliJ automatically detect the mapstruct annotation processor in the classpath?

Mohair answered 28/3, 2018 at 17:42 Comment(4)
It seems that IntelliJ does pickup the configuration (the default component model is there). Can you please do a change in your mapper and see if something happens. The way that IntelliJ performs the incremental compilation it might lead to not creating files if nothing was changedDavenport
@Filip, same result. it's not generating the source fileCasteel
@Casteel have you seen the answer to this question https://mcmap.net/q/1250063/-mapstruct-annotation-processor-does-not-seem-to-work-in-intellij-with-gradle-project?Davenport
yes, i tried the same still it's not working with Intellji 2018 CE. i could see only empty generate folder on "out" directory. but works fine on commandline.Casteel
C
13

Finally it is working fine 👍 with Intellji 2018.1 CE. we don't need any apt plugins.

Here is updated gradle file

plugins {
    id 'java'
}

repositories {
       mavenCentral()
       mavenLocal()
}
sourceCompatibility = JavaVersion.VERSION_1_8


dependencies {
      compile group: 'org.mapstruct', name: 'mapstruct-jdk8', version: '1.2.0.Final'
      compileOnly 'org.mapstruct:mapstruct-processor:1.2.0.Final'
      annotationProcessor 'org.mapstruct:mapstruct-processor:1.2.0.Final'
      compileOnly ("org.projectlombok:lombok")
      testCompile 'junit:junit:4.12'
}

Please make sure following things are configured properly

  1. Enable Annotations Processors( Preference->Build Execute Deployment ->Compiler->Annotations Processors )

  2. MapStruct plugin

  3. Lombok plugin

Casteel answered 23/7, 2018 at 10:16 Comment(2)
This is a correct solution, though it didn't work smoothly for me. It seems that IntelliJ did not pick up the configuration. I had to close the project in IDE, manually delete contents of "out" folder, reopen the project, and make a change in my mapper class to cause an automatic rebuild. Until I did this, my "out/production/classes/generated" folder was empty.Cacique
Thank you @Jega. It's exactly What I need, even official docs not working properly.Cider
C
7

My build.gradle:

plugins {
    id 'java'
    id 'idea'
}

ext {
    mapstructVersion = '1.2.0.Final'
}

dependencies {

    // bean mapping
    compile group: 'org.mapstruct', name: 'mapstruct-jdk8', version: mapstructVersion
    compileOnly group: 'org.mapstruct', name: 'mapstruct-processor', version: mapstructVersion
    annotationProcessor group: 'org.mapstruct', name: 'mapstruct-processor', version: mapstructVersion

}

Refresh and try to rebuild your project. If it doesn't work smoothly for you, proceed to the Troubleshooting section below.

Troubleshooting

If it doesn't work for you...

  1. Enable Annotation Processors manually.
    It seems to be a bug in IDEA related to annotationProcessor configuration.

    IntelliJ IDEA -> Configure Annotations Processors (click to enlarge the screenshot)

  2. Check out build output in "out/production/classes/generated" folder. If it's empty, try to rebuild your project. Make sure changes are applied by manually deleting contents of build output folders and modifying your mapper class before the rebuild.

     $ rm -rf out build .gradle
    
  3. Verify your Gradle build file by building and running the project outside of IntelliJ IDEA.

  4. Try to delegate build actions to Gradle. I don't use this option, because annotation processor works for me anyway.

    IntelliJ IDEA -> Delegate build/run actions to Gradle

  5. Update to IntelliJ IDEA 2018.3 or later.

Cacique answered 4/11, 2018 at 13:45 Comment(0)
L
2

if you are using kotlin, We can no longer discover the annotation processor in the classpath. You have to put the dependencies like this below in your build.gradle.kts

kapt {
    dependencies {
        kapt("org.mapstruct:mapstruct-processor:1.4.2.Final")
    }
}
Leiker answered 29/9, 2021 at 23:49 Comment(1)
Just to add, we need to import plugin first, kotlin("kapt") - ref - kotlinlang.org/docs/kapt.html#using-in-gradleTarango
D
1

You need to use net.ltgt.apt-idea plugin instead, it will automatically configure IntelliJ IDEA.

plugins {
    id 'net.ltgt.apt-idea' version '0.17'
}    
dependencies {
    compile('org.mapstruct:mapstruct-jdk8:1.2.0.Final')
    apt('org.mapstruct:mapstruct-processor:1.2.0.Final')
}

Please notice that apt is deprecated in the latest gradle version, and one should use annotationProcessor instead. However, IntelliJ still does not have support for it. See IDEA-187868.

Darfur answered 4/7, 2018 at 8:32 Comment(0)
E
0

I am running a Spring application using Tomcat EE server. For me doing the following worked:

Adding the following line in Edit Configuration > VM Options

-Xms1024m -Xmx1024m -XX:MaxPermSize=256m
Engstrom answered 25/3, 2020 at 20:57 Comment(0)
H
0

these are the kind of issues I've found just in case someone finds it useful.

issue 1 : impl class itself is not getting generated

  1. make sure that you have only one and correct dependency of mapstruct. multiple dependencies will cause issues
  2. make sure that annotation processing is enabled in IntelliJ and you have lombok and mapstruct plugins installed.

issue 2 : not getting fields in the mapstruct generated impl class

  1. when getter/setters are not added manually and lombok is used for getter setters, at that time, if mapstruct is processed before lombok, then this issue might come. so make sure that in your gradle file you have lombok processing dependency first and then mapstruct.
Hauger answered 22/8, 2022 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.