5

I have an entity with a property I used to write like this private Long ICU;

I'm using mapstruct:

Here is my mapper for said entity:

@Mapper(componentModel = "spring")
public interface ProtectionQueryMapper extends EntityMapper<ProtectionQueryDto, Protection> {

    ProtectionQueryDto toDto(Protection protection);

    Protection toEntity(ProtectionQueryDto protectionQueryDto);

    List<Protection> toEntity(List<ProtectionQueryDto> protectionQueryDtos);

    List<ProtectionQueryDto> toDto(List<Protection> protections);

}
public interface EntityMapper<D, E> {

    E toEntity(D dto);

    D toDto(E entity);

    List<E> toEntity(List<D> dtoList);

    List<D> toDto(List<E> entityList);
}

The problem I have is that I want to change the property from ICU go icu, which I did and it resulted in this error:

nested exception is java.lang.NoSuchMethodError:

Protection.getICU()Ljava/lang/Long;

It would seem that mapstruct generated its getters and setters based on: private Long ICU; generating method like setICU and getICU. But now that I have changed the property from ICU to icu mapstruct is not updating its method to setIcu and getIcu.

I cannot change the mapstruct generated file manually.

Also here is my pom.xml (at least the part regarding mapstruct)

<dependency>
  <groupId>org.mapstruct</groupId>
  <artifactId>mapstruct</artifactId>  
  <version>1.3.0.Final</version>
</dependency>

              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>1.3.0.Final</version>
                        </path>
                    </annotationProcessorPaths>
                    <compilerArgs>
                        <compilerArg>
                            <arg>-Amapstruct.defaultComponentModel=spring</arg>
                        </compilerArg>
                    </compilerArgs>
                </configuration>
            </plugin>

Any idea how to have mapstruct update its generated source file?

Community
  • 1
  • 1
Nicolas
  • 103
  • 3
  • 12

5 Answers5

14

You need to provide the lombok plugin first in order, then the mapstruct-processor plugin just like this.

<configuration>
    <source>1.8</source>
    <target>1.8</target>
    <annotationProcessorPaths>
        <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </path>
        <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.4.2.Final</version>
        </path>
    </annotationProcessorPaths>
</configuration>
8

For some reason the recompile of the project didn't run the annotation processor. MapStruct is invoked by the Java compiler and the maven-compiler-plugin is responsible for cleaning up the folder with the generated classes.

Doing mvn clean compile will always work. However, if doing a change and then doing mvn compile doesn't, I would try with the latest version of the maven-compiler-plugin and if that still doesn't work create a bug report for the plugin.

Filip
  • 19,269
  • 7
  • 51
  • 60
2

Thanks to https://stackoverflow.com/users/10467757/adil-aslam-sachwani for his answer about setting mapstruct processor after lombok's one. It worked for me, but I have it with gradle, so in that case, it should be like this:

implementation 'org.mapstruct:mapstruct:1.5.2.Final'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.2.Final'
1

If you use Lombok for your entities and DTOs you have to update your pom.xml like this:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <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>
                    <compilerArgs>
                        <arg>-Amapstruct.defaultComponentModel=spring</arg>
                    </compilerArgs>
                </configuration>
            </plugin>

Then Mupstruct will be able to see their getters and setters.

(You can check my project and its demo to see this in action.)

Dan Dragut
  • 41
  • 1
  • 5
Cepr0
  • 28,144
  • 8
  • 75
  • 101
0

I know this is an old question, but after editing your pom.xml like Dan Dragut, and if you use intellij, try clicking on the hammer that is located on the left side of the "run" button, that will cause the project to be rebuilt, then your generated sources will be up to date again

  • The toolbar icons in IntelliJ and other IDEs can be customized, so please provide better description how to find the rebuild button or you can just suggest to use the rebuild functionality. – woytech Nov 13 '22 at 12:37