Mapstruct is not updating its getters and setters in the generated source files

纵饮孤独 提交于 2019-12-11 16:11:17

问题


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?


回答1:


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.




回答2:


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>
                        <compilerArg>
                            <arg>-Amapstruct.defaultComponentModel=spring</arg>
                        </compilerArg>
                    </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.)



来源:https://stackoverflow.com/questions/56849053/mapstruct-is-not-updating-its-getters-and-setters-in-the-generated-source-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!