Java / MapStruct Java Interview questions part 2
How do you add MapStruct to a Maven project?
MapStruct needs two things in Maven: the core dependency for compilation and runtime, and the annotation processor registered on the compiler plugin.
<dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>1.6.3</version> </dependency> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.6.3</version> </path> </annotationProcessorPaths> </configuration> </plugin>
Without registering the processor, javac never invokes MapStruct and no implementation class is produced, so calling Mappers.getMapper(...) at runtime would fail with a "no implementation found" error. Keeping the mapstruct and mapstruct-processor versions aligned avoids subtle mismatches between the annotations you compile against and the generator that reads them.
More Related questions...