[[setup]] == Set up MapStruct is a Java annotation processor based on http://www.jcp.org/en/jsr/detail?id=269[JSR 269] and as such can be used within command line builds (javac, Ant, Maven etc.) as well as from within your IDE. It comprises the following artifacts: * _org.mapstruct:mapstruct_: contains the required annotations such as `@Mapping` * _org.mapstruct:mapstruct-processor_: contains the annotation processor which generates mapper implementations === Apache Maven For Maven based projects add the following to your POM file in order to use MapStruct: .Maven configuration ==== [source, xml, linenums] [subs="verbatim,attributes"] ---- ... {mapstructVersion} ... org.mapstruct mapstruct ${org.mapstruct.version} ... org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 org.mapstruct mapstruct-processor ${org.mapstruct.version} ... ---- ==== [TIP] ==== If you are working with the Eclipse IDE, make sure to have a current version of the http://www.eclipse.org/m2e/[M2E plug-in]. When importing a Maven project configured as shown above, it will set up the MapStruct annotation processor so it runs right in the IDE, whenever you save a mapper type. Neat, isn't it? To double check that everything is working as expected, go to your project's properties and select "Java Compiler" -> "Annotation Processing" -> "Factory Path". The MapStruct processor JAR should be listed and enabled there. Any processor options configured via the compiler plug-in (see below) should be listed under "Java Compiler" -> "Annotation Processing". If the processor is not kicking in, check that the configuration of annotation processors through M2E is enabled. To do so, go to "Preferences" -> "Maven" -> "Annotation Processing" and select "Automatically configure JDT APT". Alternatively, specify the following in the `properties` section of your POM file: `jdt_apt`. Also make sure that your project is using Java 1.8 or later (project properties -> "Java Compiler" -> "Compile Compliance Level"). It will not work with older versions. ==== === Gradle Add the following to your Gradle build file in order to enable MapStruct: .Gradle configuration ==== [source, groovy, linenums] [subs="verbatim,attributes"] ---- ... plugins { ... id "com.diffplug.eclipse.apt" version "3.26.0" // Only for Eclipse } dependencies { ... implementation "org.mapstruct:mapstruct:${mapstructVersion}" annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}" // If you are using mapstruct in test code testAnnotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}" } ... ---- ==== You can find a complete example in the https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-on-gradle[mapstruct-examples] project on GitHub. === Apache Ant Add the `javac` task configured as follows to your _build.xml_ file in order to enable MapStruct in your Ant-based project. Adjust the paths as required for your project layout. .Ant configuration ==== [source, xml, linenums] [subs="verbatim,attributes"] ---- ... ... ---- ==== You can find a complete example in the https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-on-ant[mapstruct-examples] project on GitHub. [[configuration-options]] === Configuration options The MapStruct code generator can be configured using _annotation processor options_. When invoking javac directly, these options are passed to the compiler in the form _-Akey=value_. When using MapStruct via Maven, any processor options can be passed using `compilerArgs` within the configuration of the Maven processor plug-in like this: .Maven configuration ==== [source, xml, linenums] [subs="verbatim,attributes"] ---- ... org.apache.maven.plugins maven-compiler-plugin 3.5.1 1.8 1.8 org.mapstruct mapstruct-processor ${org.mapstruct.version} true -Amapstruct.suppressGeneratorTimestamp=true -Amapstruct.suppressGeneratorVersionInfoComment=true -Amapstruct.verbose=true ... ---- ==== .Gradle configuration ==== [source, groovy, linenums] [subs="verbatim,attributes"] ---- ... compileJava { options.compilerArgs += [ '-Amapstruct.suppressGeneratorTimestamp=true', '-Amapstruct.suppressGeneratorVersionInfoComment=true', '-Amapstruct.verbose=true' ] } ... ---- ==== The following options exist: .MapStruct processor options [cols="1,2a,1"] |=== |Option|Purpose|Default |`mapstruct. suppressGeneratorTimestamp` |If set to `true`, the creation of a time stamp in the `@Generated` annotation in the generated mapper classes is suppressed. |`false` |`mapstruct.verbose` |If set to `true`, MapStruct in which MapStruct logs its major decisions. Note, at the moment of writing in Maven, also `showWarnings` needs to be added due to a problem in the maven-compiler-plugin configuration. |`false` |`mapstruct. suppressGeneratorVersionInfoComment` |If set to `true`, the creation of the `comment` attribute in the `@Generated` annotation in the generated mapper classes is suppressed. The comment contains information about the version of MapStruct and about the compiler used for the annotation processing. |`false` |`mapstruct.defaultComponentModel` |The name of the component model (see <>) based on which mappers should be generated. Supported values are: * `default`: the mapper uses no component model, instances are typically retrieved via `Mappers#getMapper(Class)` * `cdi`: the generated mapper is an application-scoped (from javax.enterprise.context or jakarta.enterprise.context, depending on which one is available with javax.inject having priority) CDI bean and can be retrieved via `@Inject` * `spring`: the generated mapper is a singleton-scoped Spring bean and can be retrieved via `@Autowired` * `jsr330`: the generated mapper is annotated with {@code @Named} and can be retrieved via `@Inject` (from javax.inject or jakarta.inject, depending which one is available with javax.inject having priority), e.g. using Spring * `jakarta`: the generated mapper is annotated with {@code @Named} and can be retrieved via `@Inject` (from jakarta.inject), e.g. using Spring * `jakarta-cdi`: the generated mapper is an application-scoped (from jakarta.enterprise.context) CDI bean and can be retrieved via `@Inject` If a component model is given for a specific mapper via `@Mapper#componentModel()`, the value from the annotation takes precedence. |`default` |`mapstruct.defaultInjectionStrategy` | The type of the injection in mapper via parameter `uses`. This is only used on annotated based component models such as CDI, Spring and JSR 330. Supported values are: * `field`: dependencies will be injected in fields * `constructor`: will be generated constructor. Dependencies will be injected via constructor. When CDI `componentModel` a default constructor will also be generated. If a injection strategy is given for a specific mapper via `@Mapper#injectionStrategy()`, the value from the annotation takes precedence over the option. |`field` |`mapstruct.unmappedTargetPolicy` |The default reporting policy to be applied in case an attribute of the target object of a mapping method is not populated with a source value. Supported values are: * `ERROR`: any unmapped target property will cause the mapping code generation to fail * `WARN`: any unmapped target property will cause a warning at build time * `IGNORE`: unmapped target properties are ignored If a policy is given for a specific mapper via `@Mapper#unmappedTargetPolicy()`, the value from the annotation takes precedence. If a policy is given for a specific bean mapping via `@BeanMapping#unmappedTargetPolicy()`, it takes precedence over both `@Mapper#unmappedTargetPolicy()` and the option. |`WARN` |`mapstruct.unmappedSourcePolicy` |The default reporting policy to be applied in case an attribute of the source object of a mapping method is not populated with a target value. Supported values are: * `ERROR`: any unmapped source property will cause the mapping code generation to fail * `WARN`: any unmapped source property will cause a warning at build time * `IGNORE`: unmapped source properties are ignored If a policy is given for a specific mapper via `@Mapper#unmappedSourcePolicy()`, the value from the annotation takes precedence. If a policy is given for a specific bean mapping via `@BeanMapping#ignoreUnmappedSourceProperties()`, it takes precedence over both `@Mapper#unmappedSourcePolicy()` and the option. |`IGNORE` |`mapstruct. disableBuilders` |If set to `true`, then MapStruct will not use builder patterns when doing the mapping. This is equivalent to doing `@Mapper( builder = @Builder( disableBuilder = true ) )` for all of your mappers. |`false` |`mapstruct.nullValueIterableMappingStrategy` |The strategy to be applied when `null` is passed as a source value to an iterable mapping. Supported values are: * `RETURN_NULL`: if `null` is passed as a source value, then `null` will be returned * `RETURN_DEFAULT`: if `null` is passed then a default value (empty collection) will be returned If a strategy is given for a specific mapper via `@Mapper#nullValueIterableMappingStrategy()`, the value from the annotation takes precedence. If a strategy is given for a specific iterable mapping via `@IterableMapping#nullValueMappingStrategy()`, it takes precedence over both `@Mapper#nullValueIterableMappingStrategy()` and the option. |`RETURN_NULL` |`mapstruct.nullValueMapMappingStrategy` |The strategy to be applied when `null` is passed as a source value to a map mapping. Supported values are: * `RETURN_NULL`: if `null` is passed as a source value, then `null` will be returned * `RETURN_DEFAULT`: if `null` is passed then a default value (empty map) will be returned If a strategy is given for a specific mapper via `@Mapper#nullValueMapMappingStrategy()`, the value from the annotation takes precedence. If a strategy is given for a specific map mapping via `@MapMapping#nullValueMappingStrategy()`, it takes precedence over both `@Mapper#nullValueMapMappingStrategy()` and the option. |`RETURN_NULL` |=== === Using MapStruct with the Java Module System MapStruct can be used with Java 9 and higher versions. To allow usage of the `@Generated` annotation `java.annotation.processing.Generated` (part of the `java.compiler` module) can be enabled. === IDE Integration There are optional MapStruct plugins for IntelliJ and Eclipse that allow you to have additional completion support (and more) in the annotations. ==== IntelliJ The https://plugins.jetbrains.com/plugin/10036-mapstruct-support[MapStruct IntelliJ] plugin offers assistance in projects that use MapStruct. Some features include: * Code completion in `target`, `source`, `expression` * Go To Declaration for properties in `target` and `source` * Find Usages of properties in `target` and `source` * Refactoring support * Errors and Quick Fixes ==== Eclipse The https://marketplace.eclipse.org/content/mapstruct-eclipse-plugin[MapStruct Eclipse] Plugin offers assistance in projects that use MapStruct. Some features include: * Code completion in `target` and `source` * Quick Fixes