Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly integrate MapStruct with Eclipse? (Including Lombok java agent)

I'm willing to use MapStruct in some official project so I decided to give it some testing first; I'd need to make it work integrated with eclipse and followed all the instructions provided on MapStruct website but ... so far no luck. Did anyone succeeded on such integration? and if yes what can I be missing?

My test started with something bigger, but when I realized it was not working I decided to use a smaller example, so this is what I did:

  • Using openjdk-10.0.1_windows-x64
  • downloaded and installed a fresh version of eclipse (eclipse-jee-2019-03-R-win32-x86_64)
  • installed latest m2e-apt (and mapstruct plugin though not needed for this exercise) as suggested in http://mapstruct.org/documentation/ide-support/
  • downloaded https://github.com/mapstruct/mapstruct-examples
  • imported from git (from within eclipse) mapstruct-clone project (looked the basic-est example)
  • checked the settings as suggested in http://mapstruct.org/documentation/dev/reference/html/ (chap 2.1) and everything looks good

... no way to persuade eclipse to auto-generate the mapper implementation, I even added the jdt_apt line to the pom.

Here is a snippet of the pom.xml - please refer to he mapstruct-clone project for the whole code.

<properties>
    <org.mapstruct.version>1.3.0.Final</org.mapstruct.version>
    <m2e.apt.activation>jdt_apt</m2e.apt.activation>
</properties>

Expected result would be:

  • to find my re-generated classes under "target/generated-sources/annotations" when I save a mapping interface
  • (as stated by http://mapstruct.org/documentation/dev/reference/html/ " 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?")
  • but do not
  • I can only get the classes generated through a "mvn clean install" which is good but not that handy

Important edit: I'm also using the lombok javaagent

like image 820
Andrea Saba Avatar asked Apr 08 '19 16:04

Andrea Saba


People also ask

Does MapStruct work with Lombok?

Can I use MapStruct together with Project Lombok? Yes, as of MapStruct 1.2. 0. Beta1 and Lombok 1.16.

How do you compile a MapStruct?

IntelliJ IDEA xml file located in the root folder. IDEA then will load MapStruct, resolve all dependencies and compile the source. If so, open the preferences window, navigate to Compiler -> Annotation processor and untick checkbox “Enable annotation processing” at “Annotation profile for mapstruct-integrationtest”.

How does MapStruct generate implementation?

MapStruct is an open-source Java-based code generator which creates code for mapping implementations. It uses annotation-processing to generate mapper class implementations during compilation and greatly reduces the amount of boilerplate code which would regularly be written by hand.

Is MapStruct open source?

MapStruct is all open source and your help is highly appreciated. MapStruct follows the Fork & Pull development approach.


1 Answers

I eventually found the solution to the issue ( =D )

  • first thanks to Sjaak Derksen from MapStruct team for supporting me
  • second thanks to Pavel Horal for posting this solution https://github.com/mapstruct/mapstruct/issues/1159
  • and finally thanks to myself for being so stubborn (well sometimes it helps)

The issue was indeed tied to an incompatibility between Lombok java agent and MapStruct. To get it to work just do what Pavel suggested in his last post and it will work:

[...] simply remove the SPI registration inside lombok.jar (by deleting META-INF/services/org.mapstruct.ap.spi.AstModifyingAnnotationProcessor) [...]

like image 106
Andrea Saba Avatar answered Sep 29 '22 02:09

Andrea Saba