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

15,508

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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")
    }
}
Share:
15,508
Eric B.
Author by

Eric B.

Updated on June 24, 2022

Comments

  • Eric B.
    Eric B. almost 2 years

    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?

  • naXa stands with Ukraine
    naXa stands with Ukraine over 5 years
    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.
  • Pranay Kumbhalkar
    Pranay Kumbhalkar over 5 years
    Thank you @Jega. It's exactly What I need, even official docs not working properly.
  • Amarnath Reddy Dornala
    Amarnath Reddy Dornala over 2 years
    Just to add, we need to import plugin first, kotlin("kapt") - ref - kotlinlang.org/docs/kapt.html#using-in-gradle