I have a Spring Boot application, which uses MapStruct. I try to use a @ValueMapping
to map String
to an enum. My String
s contain non-english characters. This characters are wrong in the generated code.
POM
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.2.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>
-Amapstruct.defaultComponentModel=spring
</arg>
</compilerArgs>
</configuration>
</plugin>
Code
@Mapper
public interface TestMapper {
@ValueMapping(source = "Menschenwürde", target = "TEST_VALUE")
TestEnum map(String value);
}
Generated Code
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2022-09-02T18:22:58+0200",
comments = "version: 1.5.2.Final, compiler: Eclipse JDT (IDE) 3.21.0.v20200304-1404, environment: Java 11.0.8 (Oracle Corporation)"
)
@Component
public class TestMapperImpl implements TestMapper {
@Override
public TestEnum map(String value) {
if ( value == null ) {
return null;
}
TestEnum testEnum;
switch ( value ) {
case "Menschenw�rde": testEnum = TestEnum.TEST_VALUE;
break;
case "TEST_VALUE": testEnum = TestEnum.TEST_VALUE;
break;
default: throw new IllegalArgumentException( "Unexpected enum constant: " + value );
}
return testEnum;
}
}
Research
My
TestMapper
class is saved in Spring Tool Suite 4 with encoding UTF-8.I checked the variable
${project.build.sourceEncoding}
in POM and the value isUTF-8
.If I run a Maven Build from command line, the encoding of the generated code is correct.
If I run a Maven Build in Spring Tool Suite 4, the encoding of the generated code is correct.
If I run a clean build in Spring Tool Suite 4, the encoding of the generated code is wrong.
I read Encoding problem when compiler is Eclipse JDT (IDE), but this issue was closed 2018 without any solution.
I read How to support UTF-8 encoding in Eclipse and tried following solutions:
- I changed Window > Preferences > General > Content Types to UTF-8.
- I changed Window > Preferences > General > Workspace to UTF-8
but none worked.
Question
Is there any way to set the encoding for the generated code?