Phate
Phate

Reputation: 6622

Mapstruct only works via maven package..is maven compiler plugin really necessary?

I'm trying setting up mapstruct with my project, I'm used to lombok that does it via a simple jvm agent so I really can't understand how to make mapstruct work.

Here's my pom:

<properties>
    <m2e.apt.activation>jdt_apt</m2e.apt.activation>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-configuration-processor</artifactId>
                        <version>${springboot.version}</version>
                    </path>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${springboot.version}</version>
                </plugin>
    </plugins>
</build>

Please mind that, before mapstruct entered the game, I didn't need this whole maven compile plugin block: everything was working fine. I could build my springboot fat jar with no problems at all, no need to explicitely specify spring and lombok annotation processing...they were very good times.

Now I'm not even sure that I didn't introduce some regressions with the above code but, anyhow, I noticed that mapstruct classes are only generated when I do "mvn package". I would have expected, like for lombok, for them to be generated automatically each time I saved an object but this does not happen.

Do you have any idea? And can you assure me that that specific build block does not change anything in my spring boot project?

Upvotes: 2

Views: 4950

Answers (2)

roudlek
roudlek

Reputation: 385

    <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>1.5.5.Final</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.5.5.Final</version>
    </dependency>

these two dependencies are working perfectly fine for me !

Upvotes: 3

Filip
Filip

Reputation: 21413

For the code generation to work automatically make sure that you have the m2e-apt plugin installed.

As for having or not having the maven-compiler-plugin block. It is a flavour that the MapStruct teams like to recommend.

MapStruct is an annotation processor, which means that you don't need it during runtime. Therefore, you need 2 dependencies mapstruct where the annotations are located and the mapstruct-processor where the processor is located. The processor has some extra dependencies and contains shaded Freemarker for the code generation.

The use of annotationProcessorPaths allows you to have annotation processor paths and not have conflicts on your classpath, i.e. not accidentally using some internal MapStruct classes in your production code. On top of that, you don't need Spring Boot to package the mapstruct-processor provided dependency for your runtime application.

From the maven-compiler documentation the annotationProcessorPaths means the following:

Classpath elements to supply as annotation processor path. If specified, the compiler will detect annotation processors only in those classpath elements. If omitted, the default classpath is used to detect annotation processors. The detection itself depends on the configuration of annotationProcessors.

So if you really want you can add the mapstruct-processor to your normal dependencies.

Upvotes: 2

Related Questions