Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Mapstruct Java11 compatible?

I'm a bit confused. There is some documentation that says java 9 is "experimental":

https://mapstruct.org/documentation/stable/reference/html/#_using_mapstruct_on_java_9

And I found a post where a guy was having trouble in Java 10. So we are heading to java 11 and I want to know if Mapstuct will work in that environment. Specifically, will it generate the code at compile time AND does the generated code work there (I suppose the latter does).

like image 235
markthegrea Avatar asked Dec 17 '22 15:12

markthegrea


2 Answers

Yes, it works on a Java 11 / Spring Boot 2 project at work, and we use Mapstruct without issues.

like image 65
Xavier FRANCOIS Avatar answered Jan 31 '23 15:01

Xavier FRANCOIS


Yes, it is possible, although I struggled a bit with it while migrating a DropWizard project (1.3.7) to java 11. The configuration as proposed in the documentation (through the maven-compiler-plugin) didn't work for me (no error was shown, but the mapper class was not generated) so I had to use maven-processor-plugin v3.3.3.

Here is how I managed to do that:

Add the dependencies using <org.mapstruct.version>1.3.1.Final</org.mapstruct.version>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>${org.mapstruct.version}</version>
</dependency>
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>${org.mapstruct.version}</version>
    <scope>provided</scope>
</dependency>

Then configure the plugin in the submodule as follows

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>3.3.3</version>
    <executions>
        <execution>
            <id>process</id>
            <goals>
                <goal>process</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <processors>
                    <!-- list of processors to use -->
                    <processor>org.mapstruct.ap.MappingProcessor</processor>
                </processors>
                <outputDirectory>${basedir}/target/generated-sources-mappers</outputDirectory>
                <compilerArguments>-source 11 -target 11</compilerArguments>
            </configuration>
        </execution>
    </executions>
</plugin>
  • The outputDirectory is something specific to our project, but I leave there to highlight the fact that the xml tag changed from version 2.x of te plugin, in case you are migrating from that.
  • The compilerArguments portion was required because the plugin run javac passing java version 1.6 as default argument, which won't work if you are using lambda expressions or other new features from the language.

When compiling, make sure to pay attention to the output of the plugin, it should only show warnings, otherwise it won't generate you classes and you will get a generic ClassNotFound exception but the cause can be something not allowing your plugin to compile well.

[INFO] --- maven-processor-plugin:3.3.3:process
...
7 warnings

Also make sure you don't have any version of mapstruct library older than 1.3.0.Final in you classpath, that will also cause issues preventing classes from generating.

like image 23
e_liel Avatar answered Jan 31 '23 15:01

e_liel