Configuring MapStruct compiler arguments in Gradle and IntelliJ

24 Jul 2016 . Technology . Comments

#software #coding

MapStruct is a great Java library that generates object mapping code using an annotation processor. Some of the settings (i.e. the DI model) that you would otherwise apply via annotations can be applied globally using a compiler argument. Here is how you can set it up if you’re using Gradle and/or IntelliJ.

Gradle Configuration

The MapStruct documentation shows an example of how to configure the Maven apt plugin, here is how you do it with Gradle.

plugins {
    id 'net.ltgt.apt' version '0.6'
}
...
compileJava {
    options.compilerArgs = [
            '-Amapstruct.defaultComponentModel=spring'
    ]
}

dependencies {
    ...
    compile "org.mapstruct:mapstruct-jdk8:1.1.0.Beta2"
    compile "org.mapstruct:mapstruct-processor:1.1.0.Beta2"

    apt 'org.mapstruct:mapstruct-processor:1.1.0.Beta2'
}

The compiler options allow you to provide an array of compiler arguments.

You can find a list of all MapStruct configuration options here.

IntelliJ configuration

If you’re already using MapStruct then you’ve probably enabled annotation processing, but if not that is the first thing you need to do.

After that apply the same compiler arguments you set in your Gradle file to your IntelliJ config, as shown here:

IntelliJ Annotation Compiler Settings

Thats all there is to it! Now you can apply global MapStruct configurations and keep your code clean.