stephan
stephan

Reputation: 71

mapstruct 1.3.1 with boot spring &java 13

I'm trying to configure mapstrcut 1.3.1 with maven and STS (Eclipse) and based on Java 13. I've tried several configurations in my pom.xml, including the use of maven-compiler-plugin or alternatively maven-processor-plugin from org.bsc.maven. Mapstruct does not generate any source classes and therefore my test leads to a ClassNotFoundException since no implementation of the mapper interface can be found. Does anybody have a successful config running mapstruct with java 13 in spring boot?

pom.xml

...
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- Mapping von Entities in DTO -->
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency> <!-- nessecary? -->
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>${org.mapstruct.version}</version>
            <scope>compile</scope>
        </dependency>
...

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version> <!-- or newer version -->
                <configuration>
                    <source>13</source> <!-- depending on your project -->
                    <target>13</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
enter code here

Mapper Interface

@Mapper(componentModel = "spring")
public interface ProduktMapper {
    Produkt dto2entity(ProduktDTO produktDTO);
    ProduktDTO entity2dto(Produkt produkt);
}

Unit Test

@Test
public void testEntity2DtoMapper() {
    Produkt produkt = new Produkt();
    produkt.setProduktname("Testprodukt");
    ProduktDTO produktDTO = mapper.entity2dto(produkt);

    assertEquals(produkt.getProduktname(), produktDTO.getProduktname());
}

Upvotes: 1

Views: 4058

Answers (2)

funder7
funder7

Reputation: 1831

Have you tried to compile from maven first?

In my case it was working when compiled with maven, but it was failing when launched as spring boot application.

After some search I've found out that eclipse must have the correct maven connector configured.

If into Window > Prefrences > Maven the Annotation Processing entry is missing, that means that the maven connector must be downloaded.

To be safe, before to proceed check that you have this plugin installed: m2e-apt. Then open Window > Preferences > Maven > Discovery, click on "Open Catalog". Search "jdt", and you will see one connector to download from the list.

Install, reboot, and go back into Preferences > Maven > Annotation Processing (the tab that was missing!), select "Automatically configure JDT APT", and confirm all the following dialogs.

Now all the errors should be gone, and the project should compile!

Upvotes: 2

stephan
stephan

Reputation: 71

This is the configuration that works for my spring boot 2.2.5 application with Java 13, mapstruct 1.3.1 and lombok 1.18.12.

...
    <properties>
        <java.version>13</java.version>
        <mapstruct.version>1.3.1.Final</mapstruct.version>
        <lombok.version>1.18.12</lombok.version>
    </properties>
...
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${mapstruct.version}</version>
            <scope>compile</scope>
        </dependency>
...
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
...

Upvotes: 1

Related Questions