4

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
  • 93
  • 3
  • 12

3 Answers3

9

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>
5

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
  • 15,581
  • 5
  • 44
  • 51
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
  • 24,708
  • 7
  • 67
  • 95