9

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).

jannis
  • 4,843
  • 1
  • 23
  • 53
markthegrea
  • 3,731
  • 7
  • 55
  • 78
  • The accepted answer says it works with Spring Boot. I have a dropwizard 1.3.7 project with maven that I'm currently migrating to java 11 and it doesn't seem to work so far despite following the steps described in the documentation. Does anyone knows if this works outside of spring boot? – e_liel Nov 26 '19 at 19:40
  • @eliel.lobo: sounds like you need to describe your problem in a separate post (possibly linking to this one). Especially since "doesn't seem to work" is a very vague error description ... – Joachim Sauer Dec 02 '19 at 14:06
  • You are right, I added an answer for the specific scenario just in case someone is interested. By "doesn't seem to work" I meant the mapper annotated class was not generated, hence, I got class not found exception. There were no errors nor logs indicating any issue, I'm clueless about what that was. – e_liel Dec 11 '19 at 14:20

3 Answers3

8

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

markthegrea
  • 3,731
  • 7
  • 55
  • 78
Xavier FRANCOIS
  • 652
  • 4
  • 15
  • Can you share the config that you are using please? – Youcef LAIDANI Mar 28 '20 at 21:14
  • 1
    Yes, but my config is very basic, I doubt it will help you. org.mapstruct mapstruct 1.3.1.Final org.mapstruct mapstruct-processor 1.3.1.Final – Xavier FRANCOIS Mar 30 '20 at 08:07
  • Thank you for your response, can you share the plugin part please? – Youcef LAIDANI Mar 30 '20 at 08:08
  • I got nothing relevant in the plugin part. My project has "spring-boot-starter-parent:2.2.2.RELEASE" as a parent project. It includes maven-compiler-plugin:3.8.1. When I compile my project (or use the maven-compiler-plugin:3.8.1:compile) my Impl classes are well generated. Is your project a spring-boot one ? – Xavier FRANCOIS Mar 30 '20 at 08:38
6

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.

e_liel
  • 203
  • 5
  • 11
0

I used the following configuration for JDK11

    <properties>
    <mapstruct.version>1.3.1.Final</mapstruct.version>
    <maven.compiler.version>3.6.1</maven.compiler.version>
  </properties>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Then mvn clean install will generate the impl classes in target\generated-sources\annotations