Skip to content

Commit 164535e

Browse files
nikolas-charalambidisfiliphr
authored andcommitted
Add Lombok subsection in the documentation (#2266)
1 parent 628ff17 commit 164535e

File tree

2 files changed

+152
-2
lines changed

2 files changed

+152
-2
lines changed

documentation/src/main/asciidoc/chapter-14-third-party-api-integration.asciidoc

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,153 @@ public @interface Default {
3838
3939
}
4040
----
41-
====
41+
====
42+
43+
[[lombok]]
44+
=== Lombok
45+
46+
MapStruct works together with https://projectlombok.org/[Project Lombok] as of MapStruct 1.2.0.Beta1 and Lombok 1.16.14.
47+
48+
MapStruct takes advantage of generated getters, setters, and constructors and uses them to generate the mapper implementations.
49+
50+
[NOTE]
51+
====
52+
Lombok 1.18.16 introduces a breaking change (https://projectlombok.org/changelog[changelog]).
53+
The additional annotation processor `lombok-mapstruct-binding` (https://mvnrepository.com/artifact/org.projectlombok/lombok-mapstruct-binding[Maven]) must be added otherwise MapStruct stops working with Lombok.
54+
This resolves the compilation issues of Lombok and MapStruct modules.
55+
56+
[source, xml]
57+
----
58+
<path>
59+
<groupId>org.projectlombok</groupId>
60+
<artifactId>lombok-mapstruct-binding</artifactId>
61+
<version>0.1.0</version>
62+
</path>
63+
----
64+
====
65+
66+
==== Set up
67+
68+
The set up using Maven or Gradle does not differ from what is described in <<setup>>. Additionally, you need to provide Lombok dependencies.
69+
70+
.Maven configuration
71+
====
72+
[source, xml, linenums]
73+
[subs="verbatim,attributes"]
74+
----
75+
76+
<properties>
77+
<org.mapstruct.version>{mapstructVersion}</org.mapstruct.version>
78+
<org.projectlombok.version>1.18.16</org.projectlombok.version>
79+
<maven.compiler.source>1.8</maven.compiler.source>
80+
<maven.compiler.target>1.8</maven.compiler.target>
81+
</properties>
82+
83+
<dependencies>
84+
<dependency>
85+
<groupId>org.mapstruct</groupId>
86+
<artifactId>mapstruct</artifactId>
87+
<version>${org.mapstruct.version}</version>
88+
</dependency>
89+
90+
<!-- lombok dependency should not end up on classpath -->
91+
<dependency>
92+
<groupId>org.projectlombok</groupId>
93+
<artifactId>lombok</artifactId>
94+
<version>${org.projectlombok.version}</version>
95+
<scope>provided</scope>
96+
</dependency>
97+
</dependencies>
98+
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-compiler-plugin</artifactId>
104+
<version>3.8.1</version>
105+
<configuration>
106+
<source>1.8</source>
107+
<target>1.8</target>
108+
<annotationProcessorPaths>
109+
<path>
110+
<groupId>org.mapstruct</groupId>
111+
<artifactId>mapstruct-processor</artifactId>
112+
<version>${org.mapstruct.version}</version>
113+
</path>
114+
<path>
115+
<groupId>org.projectlombok</groupId>
116+
<artifactId>lombok</artifactId>
117+
<version>${org.projectlombok.version}</version>
118+
</path>
119+
120+
<!-- additional annotation processor required as of Lombok 1.18.16 -->
121+
<path>
122+
<groupId>org.projectlombok</groupId>
123+
<artifactId>lombok-mapstruct-binding</artifactId>
124+
<version>0.1.0</version>
125+
</path>
126+
</annotationProcessorPaths>
127+
</configuration>
128+
</plugin>
129+
</plugins>
130+
</build>
131+
----
132+
====
133+
134+
.Gradle configuration (3.4 and later)
135+
====
136+
[source, groovy, linenums]
137+
[subs="verbatim,attributes"]
138+
----
139+
140+
dependencies {
141+
142+
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
143+
implementation "org.projectlombok:lombok:1.18.16"
144+
annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.1.0"
145+
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
146+
annotationProcessor "org.projectlombok:lombok:1.18.16"
147+
}
148+
149+
----
150+
====
151+
152+
The usage combines what you already know from <<defining-mapper>> and Lombok.
153+
154+
.Usage of MapStruct with Lombok
155+
====
156+
[source, java, linenums]
157+
[subs="verbatim,attributes"]
158+
----
159+
@Data
160+
public class Source {
161+
162+
private String test;
163+
}
164+
165+
public class Target {
166+
167+
private Long testing;
168+
169+
public Long getTesting() {
170+
return testing;
171+
}
172+
173+
public void setTesting( Long testing ) {
174+
this.testing = testing;
175+
}
176+
}
177+
178+
@Mapper
179+
public interface SourceTargetMapper {
180+
181+
SourceTargetMapper MAPPER = Mappers.getMapper( SourceTargetMapper.class );
182+
183+
@Mapping( source = "test", target = "testing" )
184+
Target toTarget( Source s );
185+
}
186+
187+
----
188+
====
189+
190+
A working example can be found on the GitHub project https://github.com/mapstruct/mapstruct-examples/tree/master/mapstruct-lombok[mapstruct-lombok].

documentation/src/main/asciidoc/chapter-3-defining-a-mapper.asciidoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ public class PersonMapperImpl implements PersonMapper {
516516

517517
Supported builder frameworks:
518518

519-
* https://projectlombok.org/[Lombok] - requires having the Lombok classes in a separate module. See for more information https://github.com/rzwitserloot/lombok/issues/1538[rzwitserloot/lombok#1538]
519+
* https://projectlombok.org/[Lombok] - It is required to have the Lombok classes in a separate module.
520+
See for more information at https://github.com/rzwitserloot/lombok/issues/1538[rzwitserloot/lombok#1538] and to set up Lombok with MapStruct, refer to <<lombok>>.
520521
* https://github.com/google/auto/blob/master/value/userguide/index.md[AutoValue]
521522
* https://immutables.github.io/[Immutables] - When Immutables are present on the annotation processor path then the `ImmutablesAccessorNamingStrategy` and `ImmutablesBuilderProvider` would be used by default
522523
* https://github.com/google/FreeBuilder[FreeBuilder] - When FreeBuilder is present on the annotation processor path then the `FreeBuilderAccessorNamingStrategy` would be used by default.

0 commit comments

Comments
 (0)